Android allows sending of Bluetooth files and supports multiple models

Source: Internet
Author: User

In recent projects, you need to implement the function of transmitting the apk via Bluetooth, which allows you to search for nearby bluetooth mobile phones and share files. From the perspective of demand, the Bluetooth transmission module provided by the android mobile phone can meet the needs, and the implementation is also very simple. However, although Bluetooth modules are usually configured for mainstream models, the fragmentation of android models is too serious, and the Bluetooth functions of mobile phones of different android versions are also different. The versions earlier than 4.0.3 and later use different bluetooth packages: "com. android. bluetooth" and "com. mediatek. bluetooth ". Some manufacturers also customize and modify the bluetooth module, such as Huawei's c8812. In addition, the android Bluetooth function is available only after api5, so the minSdkVersion of android is no less than 5. There are a lot of examples on the Internet for android to call the Bluetooth sending module. There are few examples that are complete and compatible with multiple systems. Here is an example of my project. /*** Send a file 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 (), TextUt Ils. 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> local Iterator1 = 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. showBlueToothSupportEr R (activity);} if (localHashMap. size () = 0) ToastHelper. showdomainthsupporterr (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 (). it Erator (); if (localIterator2.hasNext () localActivityInfo1 = (ActivityInfo) localIterator2.next ();} if (localActivityInfo1! = Null) {localIntent. setComponent (new ComponentName (component, localActivityInfo1.name); activity. startActivityForResult (localIntent, 4098); return;} ToastHelper. show1_thsupporterr (activity);} effect:

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.