notification if the Bluetooth is turned off in android app

前端 未结 2 1243
甜味超标
甜味超标 2021-02-04 05:06

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

2条回答
  •  灰色年华
    2021-02-04 05:14

    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
    }
    

提交回复
热议问题