通过蓝牙发送文件

3

实际上我正在开发一个免费应用,需要在特定按钮按下时通过蓝牙分享自己。我已经使用了下面的代码(我试图从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
        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中)时,它只给我发送电子邮件的选项,而不是蓝牙,所以我可以请您帮助使其正常工作吗?
另外,我已经为蓝牙添加了权限,所以这不是问题的原因!
1个回答

4

您只需要更改以下行:

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

intent.setType("application/zip");

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接