检测 Enable/Disable 蓝牙

Detect Enable/Disable Bluetooth

我申请了 on/off 蓝牙。要转动 on/off,我使用了一个开关按钮。这是代码:

public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private final static int REQUEST_ENABLE_BT = 1;
BluetoothAdapter mBluetoothAdapter = null;

private final BroadcastReceiver mReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (action.equals(mBluetoothAdapter.ACTION_STATE_CHANGED)) {
            final int state = intent.getIntExtra(mBluetoothAdapter.EXTRA_STATE, mBluetoothAdapter.ERROR);

            switch (state){
                case BluetoothAdapter.STATE_OFF:

                    break;
                case BluetoothAdapter.STATE_ON:
                    break;
            }
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Switch bluetoothSwitch = (Switch) findViewById(R.id.bluetoothSwitch);

    mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
    if (mBluetoothAdapter == null){
        //Device does not support bluetooth
    }
    else{
        bluetoothSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                if(bluetoothSwitch.isChecked() && !mBluetoothAdapter.isEnabled()){
                    Intent enableBtIntent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
                    startActivityForResult(enableBtIntent, REQUEST_ENABLE_BT);

                    IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
                    registerReceiver(mReceiver, BTIntent);
                }
                if(!bluetoothSwitch.isChecked() && mBluetoothAdapter.isEnabled()){
                    mBluetoothAdapter.disable();
                    IntentFilter BTIntent = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
                    registerReceiver(mReceiver, BTIntent);
                }
            }
        });
    }
}

}

问题是,如果我从设置而不是从我的应用程序打开 on/off 蓝牙,开关不会改变。 我已经实现了一个广播接收器,但我无法从它访问开关。 我试过了:

bluetoothSwich.setChecked(true) 

在广播接收器中,但它不起作用。

编辑: 我使用全局开关部分解决了这个问题,但是要首先从设置中捕获 on/off 操作,我必须至少从我的应用程序中单击 on/off 按钮一次。有什么建议吗?

要检测蓝牙的状态变化,您需要向 AndroidManifest.xml 添加以下权限。

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

最好使用本地广播。您不需要在 Manifest 中注册它。在运行时在您需要的地方注册它。(如果需要在整个应用程序中然后在清单中注册)

private final BroadcastReceiver bStateReceiver = new BroadcastReceiver() {
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
            if (intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) == BluetoothAdapter.STATE_OFF) {
                // Bluetooth is disconnected, do handling here 
            }
        }
    }

};

运行时寄存器:

LocalBroadcastManager.getInstance(this).registerReceiver(bStateReceiver, new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));

运行时注销:不要忘记注销广播。

LocalBroadcastManager.getInstance(this).unregisterReceiver(bStateReceiver);

静态寄存器:

<receiver
    android:name=".FullyQualifiedBroadcastReceiverClassName"
    android:enabled="true">
<intent-filter>
    <action android:name="android.bluetooth.adapter.action.STATE_CHANGED"/>
</intent-filter>