我有一个用于BluetoothDevice.ACTION_BOND_STATE_CHANGED的广播接收器,看起来如下所示:
fun Int.toBondStateDescription() = when (this) {
BluetoothDevice.BOND_BONDED -> "BONDED"
BluetoothDevice.BOND_BONDING -> "BONDING"
BluetoothDevice.BOND_NONE -> "NOT BONDED"
else -> "ERROR: $this"
}
when (intent?.action) {
BluetoothDevice.ACTION_BOND_STATE_CHANGED -> {
val device = intent.getParcelableExtra<BluetoothDevice>(BluetoothDevice.EXTRA_DEVICE)
val previousBondState = intent.getIntExtra(BluetoothDevice.EXTRA_PREVIOUS_BOND_STATE, -1)
val bondState = intent.getIntExtra(BluetoothDevice.EXTRA_BOND_STATE, -1)
val bondTransition = "${previousBondState.toBondStateDescription()} to " + bondState.toBondStateDescription()
Log.w("Bond state change", "${device?.address} bond state changed | $bondTransition")
}
}当状态从BOND_NONE到BOND_BONDING,或者相反的情况下,这是很好的工作。然而,当它从BOND_BONDING到BOND_BONDED时,我没有得到任何东西。如果我手动获取设备的键合状态,我可以看到它是绑定的,但是从来没有通知过我。
我是不是遗漏了什么?
示例日志:
// Connection begins
D/BluetoothGatt: connect() - device: XX:XX:XX:XX:XX:XX, auto: false
D/BluetoothGatt: registerApp()
D/BluetoothGatt: registerApp() - UUID=1b5f1492-f53f-4d06-8704-6de7b0222769
D/BluetoothGatt: onClientRegistered() - status=0 clientIf=9
D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=9 device=XX:XX:XX:XX:XX:XX
D/BluetoothGatt: onConnectionUpdated() - Device=XX:XX:XX:XX:XX:XX interval=36 latency=0 timeout=500 status=42
D/MyBroadcastReceiver: ACTION_BOND_STATE_CHANGED
D/Bond state change: XX:XX:XX:XX:XX:XX bond state changed | NOT BONDED to BONDING
// Android stuff
V/FA: Connecting to remote service
V/FA: Connection attempt already in progress
V/FA: Activity paused, time: 1735662355
D/FA: Connected to remote service
V/FA: Processing queued up service tasks: 2
V/FA: Activity resumed, time: 1735663677
// Then I press a button that prints out the bond state, I can see that it is bonded but I was never notified
D/BOND STATE: BONDED发布于 2022-06-21 23:07:04
https://stackoverflow.com/questions/72609717
复制相似问题