1

My application evokes the android PackageManager when a file is chosen and the user is presented with a choice of applications to handle how the file should be dealt with. I want to limit this choice to Bluetooth. Currently Bluetooth comes up as the first option which is fine and this all works. I was wondering if its possible to only present the user with this single option.

    case REQUEST_FILE_SELECT:
        if (requestCode == REQUEST_FILE_SELECT) {
            // Get the Uri of the selected file
            Uri uri = data.getData();
            Log.d(TAG, "File Uri: " + uri.toString());
            // Get the path
            String path = null;
            try {
                path = FileUtils.getPath(this, uri);
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            Log.d(TAG, "File Path: " + path);
            // Get the file instance
            File mFile = new File(path);
            // Evoke the file chooser
            List<Intent> targetedShareIntents = new ArrayList<Intent>();
            Intent shareIntent = new Intent(
                    android.content.Intent.ACTION_SEND);
            shareIntent.setType("*/*");
            // Evoke the package manager
            List<ResolveInfo> resInfo = getPackageManager()
                    .queryIntentActivities(shareIntent,
                            PackageManager.GET_ACTIVITIES);
            if (!resInfo.isEmpty()) {

                for (ResolveInfo resolveInfo : resInfo) {
                    String packageName = resolveInfo.activityInfo.packageName;
                    if (packageName.equals("com.android.bluetooth")) {

                        Intent targetedShareIntent = new Intent(
                                android.content.Intent.ACTION_SEND);
                        shareIntent.putExtra(Intent.EXTRA_STREAM,
                                Uri.fromFile(mFile));
                        targetedShareIntent.setPackage(packageName);
                        targetedShareIntents.add(targetedShareIntent);
                        startActivity(Intent.createChooser(shareIntent,
                                "Share File"));

                    }

                }
            }
        }
3laz3r
  • 83
  • 9
  • 2
    The point behind `ACTION_SEND` is for the *user* to control where and how the data is sent. – CommonsWare Dec 27 '12 at 21:31
  • Yes I understand this but my question was asking for alternatives to this. – 3laz3r Dec 27 '12 at 21:47
  • The most likely alternative is for you to not use `ACTION_SEND`. After all, "Bluetooth" will not be on all devices and may not have the same functionality on those devices it is on. So, if you really want to do something with your data via Bluetooth, do it yourself. – CommonsWare Dec 27 '12 at 22:05
  • Thanks but this is a bluetooth app. Its not intended for devices which don't have bluetooth. – 3laz3r Dec 27 '12 at 22:57
  • I was not referring to Bluetooth, the technology. I was referring to "Bluetooth", the entry you are seeing in your chooser. There does not have to be a "Bluetooth" activity on any given Android device that will show up in the chooser, and that "Bluetooth" activity does not necessarily do the same thing on every device (let alone what you think it does or what the user wants it to do). Either allow your *users* to choose where to "send" your data, or write the Bluetooth handling code yourself to ensure it does what you expect it to do. – CommonsWare Dec 27 '12 at 23:01
  • Perhaps I should have been more clear. I'm trying to avoid having to write my own handler at this stage. Thanks – 3laz3r Dec 27 '12 at 23:17

1 Answers1

1

Solution: find out which apps does the device support for your intent, find the one that is bluetooh, invoke it directly.

This article answers your question: http://tsicilian.wordpress.com/2012/11/06/bluetooth-data-transfer-with-android/

From the article: We can see that the BT application is among those handlers. We could of course let the user pick that application from the list and be done with it. But if we feel we should be a tad more user-friendly, we need to go further and start the application ourselves, instead of simply displaying it in a midst of other unnecessary options…But how?

One way to do that would be to use Android’s PackageManager this way:

//list of apps that can handle our intent
PackageManager pm = getPackageManager();
List appsList = pm.queryIntentActivities( intent, 0);

if(appsList.size() > 0 {
   // proceed
}

The above PackageManager method returns the list we saw earlier of all activities susceptible to handle our file transfer intent, in the form of a list of ResolveInfo objects that encapsulate information we need:

//select bluetooth
String packageName = null;
String className = null;
boolean found = false;

for(ResolveInfo info: appsList){
  packageName = info.activityInfo.packageName;
  if( packageName.equals("com.android.bluetooth")){
     className = info.activityInfo.name;
     found = true;
     break;// found
  }
}

if(! found){

  Toast.makeText(this, R.string.blu_notfound_inlist,
  Toast.LENGTH_SHORT).show();
  // exit
}

We now have the necessary information to start BT ourselves:

//set our intent to launch Bluetooth
intent.setClassName(packageName, className);
startActivity(intent);

What we did was to use the package and its corresponding class retrieved earlier. Since we are a curious bunch, we may wonder what the class name for the “com.android.bluetooth” package is. This is what we would get if we were to print it out: com.broadcom.bt.app.opp.OppLauncherActivity. OPP stands for Object Push Profile, and is the Android component allowing to wirelessly share files.

Also in the article, how to enable the bluetooth from your application.

Raanan
  • 4,777
  • 27
  • 47
  • Posting links to other sites only is discouraged as they do not pertain directly to the question, and if the link goes dead, this answer becomes worthless. You should post at least the parts of it that are relevant to the question, as well as an explanation, in addition to the link. – Cat Dec 27 '12 at 21:39