坚持打开蓝牙

时间:2015-08-05 07:52:34

标签: android bluetooth

我正在尝试以编程方式打开蓝牙:

startActivityForResult( new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE) , 1);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
}

有时可行,但有时它会卡在“打开蓝牙”屏幕上。发生这种情况时,它永远不会达到onActivityResult()

可能导致问题的原因是什么?

我正在使用API​​ 10

2 个答案:

答案 0 :(得分:0)

使用defaultAdapter,然后在Intent调用中调用这些函数

BluetoothAdapter adapter=BluetoothAdapter.getDefaultAdapter();
//and to enable bluetooth just use
adapter.enable();
//and to disable bluetooth just use
adapter.disable();

答案 1 :(得分:0)

我猜这段代码可能有用,

public void enableBT(View view){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()){
    Intent intentBtEnabled = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE); 
    // The REQUEST_ENABLE_BT constant passed to startActivityForResult() is a locally defined integer (which must be greater than 0), that the system passes back to you in your onActivityResult()  
    // implementation as the requestCode parameter.  
    int REQUEST_ENABLE_BT = 1;
    startActivityForResult(intentBtEnabled, REQUEST_ENABLE_BT);
    } }

不要忘记在android_manifest.xml中添加以下权限

<uses-permission android:name="android.permission.BLUETOOTH"/>