Android implementation of Bluetooth file send instance code, support a variety of models _android

Source: Internet
Author: User

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:

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.