[Android Pro] 监听Blutooth打开广播

时间:2023-03-09 18:38:38
[Android Pro]    监听Blutooth打开广播
   <uses-permission android:name="android.permission.BLUETOOTH"/>
<uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
        IntentFilter filter = new IntentFilter();
filter.setPriority(2147483647);
filter.addAction(BluetoothAdapter.ACTION_STATE_CHANGED);
registerReceiver(mBluetoothReceiver, filter);
private BroadcastReceiver mBluetoothReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
String action = intent.getAction();
if (BluetoothAdapter.ACTION_STATE_CHANGED.equals(action)) {
int bluetoothState = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1);
switch (bluetoothState) {
case BluetoothAdapter.STATE_OFF :
if (FeatureConfig.DEBUG_LOG) {
Log.e("H3c", "bluetoothState STATE_OFF");
}
break;
case BluetoothAdapter.STATE_ON :
if (FeatureConfig.DEBUG_LOG) {
Log.e("H3c", "bluetoothState STATE_ON ");
}
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
              .getDefaultAdapter();
                 boolean result = false;
                 if (mBluetoothAdapter != null) {
                 result = mBluetoothAdapter.disable();
                 }
break;
}
}
}
}
};

open bluetooth

 BluetoothAdapter mBluetoothAdapter = BluetoothAdapter
.getDefaultAdapter();
if (mBluetoothAdapter != null) {
mBluetoothAdapter.enable();
}