通知如果蓝牙在Android应用程序关闭蓝牙、应用程序、通知、Android

2023-09-05 06:18:25 作者:天空

即时通讯目前正在对Android应用程序.. 我必须通知用户只要蓝牙 该装置的被关断,而应用程序是 当前正在运行的.. 如何通知远程设备达BT是 关掉? 在此先感谢

Im currently working on a android application.. I have to notify the user whenever the bluetooth of the device is turned off while the application is currently running.. How to notify the remote device tat BT is turned off? Thanks in advance

推荐答案

注册的BroadcastReceiver 意图的行动 BluetoothAdapter.ACTION_STATE_CHANGED ,拨动您的notifiyng code转换的onReceive 方法。不要忘记检查,如果新的状态为OFF

Register BroadcastReceiver with intent action BluetoothAdapter.ACTION_STATE_CHANGED and move your notifiyng code into onReceive method. Don't forget to check if new state is OFF

if(BluetoothAdapter.ACTION_STATE_CHANGED.equals(intent.getAction())) {
    if(intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, -1) 
        == BluetoothAdapter.STATE_OFF)
        // Bluetooth was disconnected
}