A recent project needs to implement a Bluetooth transmission APK feature that can search around the Bluetooth phone and share files. From the need to talk about the Android phone with the Bluetooth transmission module can meet the needs, the implementation is also very simple. However, the headache is that although the general mainstream models are configured with Bluetooth module, but the Android model is too fragmented, different versions of Android mobile phone Bluetooth function is not the same. 4.0.3 the following version and the above version of the Bluetooth package used are different, respectively, "Com.android.bluetooth" and "Com.mediatek.bluetooth." Some manufacturers have customized changes to the Bluetooth module, such as Huawei's c8812. In addition, the Android Bluetooth feature is api5 above, so Android Minsdkversion is less than 5.
Many examples of Android calling Bluetooth send modules on the Web are very few examples of complete and compatible multiple systems, and here is an example I used in a project.
Copy Code code as follows:
/**
* Send files via Bluetooth
*/
private void SendFile (activity activity) {
Packagemanager Localpackagemanager = Activity.getpackagemanager ();
Intent localintent = null;
hashmap<string, activityinfo> localhashmap = null;
try {
Localintent = new Intent ();
Localintent.setaction (Intent.action_send);
File File = new file Taexternaloverfroyoutils.getdiskcachedir (this,
Constants.download_dir). GetAbsolutePath (),
Textutils.genapkname (Worm.getwormid ()));
Localintent.putextra (Intent.extra_stream, Uri.fromfile (file));
Localintent.putextra (Intent.extra_stream,
Uri.fromfile (New File (Localapplicationinfo.sourcedir));
Localintent.settype ("*/*");
list<resolveinfo> locallist = localpackagemanager.queryintentactivities (
Localintent, 0);
Localhashmap = new hashmap<string, activityinfo> ();
iterator<resolveinfo> LocalIterator1 = Locallist.iterator ();
while (Localiterator1.hasnext ()) {
Resolveinfo Resolveinfo = (resolveinfo) localiterator1.next ();
Activityinfo LocalActivityInfo2 = Resolveinfo.activityinfo;
String str = localActivityInfo2.applicationInfo.processName;
if (Str.contains ("Bluetooth"))
Localhashmap.put (str, LOCALACTIVITYINFO2);
}
catch (Exception localexception) {
Toasthelper.showbluetoothsupporterr (activity);
}
if (localhashmap.size () = = 0)
Toasthelper.showbluetoothsupporterr (activity);
Activityinfo localActivityInfo1 = (activityinfo) localhashmap
. Get ("Com.android.bluetooth");
if (localActivityInfo1 = = null) {
LocalActivityInfo1 = (activityinfo) localhashmap
. Get ("Com.mediatek.bluetooth");
}
if (localActivityInfo1 = = null) {
iterator<activityinfo> LocalIterator2 = Localhashmap.values (). iterator ();
if (Localiterator2.hasnext ())
LocalActivityInfo1 = (activityinfo) localiterator2.next ();
}
if (localActivityInfo1!= null) {
Localintent.setcomponent (New ComponentName (
Localactivityinfo1.packagename, Localactivityinfo1.name));
Activity.startactivityforresult (Localintent, 4098);
Return
}
Toasthelper.showbluetoothsupporterr (activity);
}
Effect: