I am writing an broadcast receiver that detects whether or not a bluetooth device is connected. here is my bluetooth broadcast receiver :
val filter = IntentFilter().apply {
addAction(BluetoothDevice.ACTION_ACL_CONNECTED)
addAction(BluetoothDevice.ACTION_ACL_DISCONNECTED)
}
this.registerReceiver(broadCastReceiver, filter)
private val broadCastReceiver = object : BroadcastReceiver() {
override fun onReceive(contxt: Context?, intent: Intent?) {
val action = intent?.action
when (action) {
BluetoothDevice.ACTION_ACL_CONNECTED -> {
coroutineScope.launch {
delay(6000)
deviceHelper.configAudioManager(true, true)
}
}
BluetoothDevice.ACTION_ACL_DISCONNECTED -> {
deviceHelper.configAudioManager(false, true)
}
}
}
}
This code works on all Androids without any problems. But in Android version 12, if a Bluetooth handsfree is connected to the Android phone, the broadcast receiver is not called at all, while it works well in all other Androids.
CodePudding user response:
May require BLUETOOTH_CONNECT permission. Reference: https://developer.android.com/reference/android/bluetooth/BluetoothDevice?hl=zh-cn#ACTION_ACL_CONNECTED