我正在学习如何在 android 上使用蓝牙,并且我为 BluetoothAdapter.ACTION_STATE_CHANGED
注册了一个 BroadCastReceiver
。
在文档中,它说
Broadcast Action: The state of the local Bluetooth adapter has been changed. For example, Bluetooth has been turned on or off. Always contains the extra fields EXTRA_STATE and EXTRA_PREVIOUS_STATE
containing the new and old states respectively.
现在,我如何使用 EXTRA_STATE 和 EXTRA_PREVIOUS_STATE?
最佳答案
来自 here :
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 state = intent.getIntExtra(BluetoothAdapter.EXTRA_STATE,
BluetoothAdapter.ERROR);
switch (state) {
case BluetoothAdapter.STATE_OFF:
//Bluetooth off
break;
case BluetoothAdapter.STATE_TURNING_OFF:
//Turning Bluetooth off...
break;
case BluetoothAdapter.STATE_ON:
//Bluetooth on
break;
case BluetoothAdapter.STATE_TURNING_ON:
//Turning Bluetooth on...
break;
}
}
}
};
关于android - 如何使用 Intent 中的附加功能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32168169/