To develop an Android application making use of data transfers via Bluetooth (BT), one would logically start at the Android Developer’s Bluetooth page, where all the required steps are described in details: device discovery, pairing, client/server sockets, RFCOMM channels, etc. But before jumping into sockets and threads programming just to perform a basic BT operation, let’s consider a simpler alternative, based on one of Android’s most important features: the ability for a given application to send the user to another one, which, in this case, would be the device’s default BT application. Doing so will have the Android OS itself do all the low-level work for us. First things first, a bit of defensive programming:
import android.bluetooth.BluetoothAdapter; //. // inside method // Check if bluetooth is supported BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter(); if (btAdapter == null) < // Device does not support Bluetooth // Inform user that we're done. >
The above is the first check we need to perform. Done that, let’s see how he can start BT from within our own application. In a previous post on SMS programming, we talked about implicit intents, which basically allow us to specify the action we would like the system to handle for us. Android will then display all the activities that are able to complete the action we want, in a chooser list. Here’s an example:
// bring up Android chooser Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(file_to_transfer) ); //. startActivity(intent);
In the code snippet above, we are letting the Android system know that we intend to send a text file. The system then displays all installed applications capable of handling that action: 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 Java Rockstar?