android - 通过蓝牙发送文件

标签 android bluetooth

实际上我正在开发一个免费的应用程序,当按下某个按钮时需要通过蓝牙分享自己并且我已经使用了这段代码(我尝试从 sd 卡获取文件):

BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
        if (mBluetoothAdapter == null) {
            // Device does not support Bluetooth
        }

        if (!mBluetoothAdapter.isEnabled()) {
            Toast.makeText(getApplicationContext(), "Bluetooth is turned off, please enable it to proceed!", Toast.LENGTH_LONG).show();
        }
        else {
            File sourceFile = findFile(Environment.getExternalStorageDirectory(),"E-charge.apk");
            Intent intent = new Intent();  
            intent.setAction(Intent.ACTION_SEND);  
            intent.setType("application/vnd.android.package-archive");
            intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(sourceFile) );  
            startActivity(intent);
        }

这是与此按钮所在的 Activity 相关的 list :

<activity
        android:name=".main.MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.SEND" />
            <data android:mimeType="*/*" />
            <data android:host="*"/>
            <data android:pathPattern="*.*\\.apk" />
        </intent-filter>
    </activity>

但是,当我按下按钮(在 android 2.3.5 中)时,它让我可以选择仅通过电子邮件而不是蓝牙发送,所以我可以请求您的帮助以使其正常工作吗?

我还为蓝牙用户添加了权限,所以情况并非如此!

最佳答案

您只需要更改以下行:

intent.setType("application/vnd.android.package-archive");

intent.setType("application/zip");

关于android - 通过蓝牙发送文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22795232/

相关文章:

android - 如何在单击按钮时从微调器中获取下一个/上一个项目?

android - android webview中的Google登录卡在提示 "One Moment Please"和加载进度条的页面中

android - 谷歌分析,安装跟踪安卓

ios - 当我在 watch 套件扩展或 iPhone 蓝牙中使用 corebluetooth 框架时,Apple Watch 是否使用自己的蓝牙?

android - android 5.0( Lollipop )中的蓝牙连接问题

java - 没有输入流数据到达事件?

java - 无法将蓝牙设备列为列表

android - 在 RelativeLayout 中居中 textView

android - 计算等于 firebase 中某个值的变量数

android - React Native - 模块生命周期 - 在 "Reload"上处置资源