如何等到蓝牙在 android 后打开

how to wait till bluetooth turn on in android

我需要以编程方式在 android 设备中打开蓝牙并等待它打开以继续下一行代码。

我的代码如下

if (!mBluetoothAdapter.isEnabled()) {
                Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                ctx.startActivity(enableBtIntent);
}

这样做时,代码会从下一行继续执行,而无需等待蓝牙完全打开。有什么办法可以解决这个问题吗?我可以添加外观以检查蓝牙是否打开吗?

使用此代码 清单文件的权限

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

和代码

if (!mBluetoothAdapter.isEnabled()) {
     Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
     startActivityForResult(enableBtIntent , 0);
  } else
  {
     Toast.makeText(getApplicationContext(),"Already on", Toast.LENGTH_LONG).show();
  }

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(resultCode == RESULT_OK){
    Toast.makeText(getApplicationContext(),"Turned on",Toast.LENGTH_LONG).show();
}
if(resultCode == RESULT_CANCELED){

}

您可以使用 startActivityForResult() 并检查 resultCode 是否为 RESULT_OK in onActivityResult()在您的清单文件中具有蓝牙许可,例如..

mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
if (!mBluetoothAdapter.isEnabled()) {
    Intent enableIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
    startActivityForResult(enableIntent, 0);
}

onActivityResult

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    if(resultCode == RESULT_OK){
        // bluetooth enabled                      
    }else{
        // show error
    }       
}

ACTION_REQUEST_ENABLE

startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    //do your things
}

您可以注册一个 BroadcastReceiver 来监听 BluetoothAdapter 上的状态变化。

首先创建一个 BroadcastReceiver 来监听状态变化

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    @Override
    public void onReceive(Context context, Intent intent) {
        final String action = intent.getAction();

        if (action.equals(BluetoothAdapter.ACTION_STATE_CHANGED)) {
            final int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
                                                BluetoothAdapter.ERROR);
            switch (bluetoothState) {
            case BluetoothAdapter.STATE_ON:
                //Bluethooth is on, now you can perform your tasks
                break;
            }
        }
    }
};

然后注册 BroadcastReceiver 创建Activity 以便它可以开始接收事件。

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //Set a filter to only receive bluetooth state changed events.
    IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
    registerReceiver(mReceiver, filter);
}

记得在Activity销毁时注销监听器。

@Override
public void onDestroy() {
    super.onDestroy();
    unregisterReceiver(mReceiver);
}