亲宝软件园·资讯

展开

android实现蓝牙文件发送 android实现蓝牙文件发送的实例代码,支持多种机型

人气:0
想了解android实现蓝牙文件发送的实例代码,支持多种机型的相关内容吗,在本文为您仔细讲解android实现蓝牙文件发送的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:android,蓝牙文件发送,下面大家一起来学习吧。

最近项目上需要实现蓝牙传输apk的一个功能,能够搜索周围的蓝牙手机并分享文件。从需求上讲android手机自带的蓝牙传输模块就可以满足需要了,实现也很简单。不过让人头疼的是,虽然说一般的主流机型都配置有蓝牙模块,但是android机型碎片化太严重,不同android版本手机蓝牙功能也不一样。4.0.3以下版本和以上版本使用的蓝牙包是不同的,分别是"com.android.bluetooth"和"com.mediatek.bluetooth"。还有一些厂商对蓝牙模块进行的定制修改,比如华为的c8812。另外,android蓝牙功能是api5以上才有的,所以android的minSdkVersion要不低于5才行。

网上很多android调用蓝牙发送模块的例子,完整并且能够兼容多系统的例子很少,这里发一个我用在项目中的例子。

复制代码 代码如下:

     /**
     * 通过蓝牙发送文件
     */
    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);
    }

效果:

加载全部内容

相关教程
猜你喜欢
用户评论