In my app, I'm trying to react to the situation when my device's Bluetooth (BT) is disabled. The ACTION_BOND_STATE_CHANGED (check this and this answers ) though seems to be reported when another device connected to my phone is disconnected or turned off, at least in my case. If I register ACTION_ACL_DISCONNECTED and then turn my devices BT off, then the onReceive() is called.
init {
fetchDevices()
val mBTPairedReceiver = object : BroadcastReceiver() {
override fun onReceive(context: Context, intent: Intent) {
val action = intent.action
val device = intent.getParcelableExtra<Parcelable>(BluetoothDevice.EXTRA_DEVICE)
val previousStates = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, BluetoothAdapter.ERROR)
val states = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, BluetoothAdapter.ERROR)
when (action) {
BluetoothDevice.ACTION_FOUND -> if (device != null) {
fetchDevices()
deviceListener?.let { it(devices) }
}
BluetoothDevice.ACTION_BOND_STATE_CHANGED -> {
when (states) {
BluetoothAdapter.STATE_TURNING_OFF -> {
Log.d("MESSAGE", "BT is turning OFF")
}
BluetoothAdapter.STATE_ON -> Log.d("MESSAGE", "BT is ON now")
}
}
}
}
}
application.registerReceiver(mBTPairedReceiver, IntentFilter(BluetoothDevice.ACTION_BOND_STATE_CHANGED))
}
I want to be informed if BT is going to be turned off/disabled on my device so that I can send a final message to another device I am connected to before losing the connection .