Android 12设备BLE连接无法彻底断开问题求助
Android 12 BLE设备断开后无法重新发现的问题
我遇到了一个BLE连接相关的问题:
- 首次可正常连接BLE设备,但断开后扫描无法再找到该设备
- 完全关闭应用后设备仍处于不可发现状态,仅重启手机后设备才恢复可被扫描状态
- 该问题仅出现在Android 12的Pixel、华为、小米设备上,运行Android 12的三星设备无此异常
目前我使用以下代码断开连接:
gatt.close()
我推测Android 12存在某种独立于应用的连接维持机制,想请教是否有其他方法能确保设备被彻底断开?
编辑1
调用close()断开后,执行以下代码仍返回已连接的设备:
bluetoothManager.getConnectedDevices(BluetoothProfile.GATT)
编辑2
我可以通过以下代码复现问题:
private var gatt: BluetoothGatt? = null @SuppressLint("MissingPermission") fun onDeviceClick(macAddress: String) { logger.i(TAG, "onDeviceClick(macAddress=$macAddress)") val bluetoothManager: BluetoothManager = context.getSystemService(Context.BLUETOOTH_SERVICE) as BluetoothManager if (gatt != null) { logger.i(TAG, "Disconnecting") gatt?.close() gatt = null printConnectedDevices(bluetoothManager) return } printConnectedDevices(bluetoothManager) val btDevice = bluetoothManager.adapter.getRemoteDevice(macAddress) logger.d(TAG, "Device to connect: $btDevice") gatt = btDevice.connectGatt(context, false, object : BluetoothGattCallback() { override fun onConnectionStateChange(gatt: BluetoothGatt?, status: Int, newState: Int) { super.onConnectionStateChange(gatt, status, newState) logger.d(TAG, "Connection state changed to status: $status, sate: $newState") when (newState) { BluetoothProfile.STATE_CONNECTED -> { logger.d(TAG, "Connected") printConnectedDevices(bluetoothManager) } BluetoothProfile.STATE_DISCONNECTED -> { logger.d(TAG, "Disconnected") printConnectedDevices(bluetoothManager) } } } }) } @SuppressLint("MissingPermission") private fun printConnectedDevices(bluetoothManager: BluetoothManager) { val btDevices = bluetoothManager.getConnectedDevices(BluetoothProfile.GATT) logger.d(TAG, "Currently connected devices: $btDevices") }
操作步骤:调用一次onDeviceClick连接设备,再次调用断开。断开后Pixel设备的日志显示蓝牙适配器仍标记该设备为已连接:
I/SelectDeviceViewModel: onDeviceClick(macAddress=00:1E:42:35:F0:4D) D/SelectDeviceViewModel: Currently connected devices: [] D/SelectDeviceViewModel: Device to connect: 00:1E:42:35:F0:4D D/BluetoothGatt: connect() - device: 00:1E:42:35:F0:4D, auto: false D/BluetoothGatt: registerApp() D/BluetoothGatt: registerApp() - UUID=ae98a387-cfca-43db-82f0-45fd141979ee D/BluetoothGatt: onClientRegistered() - status=0 clientIf=12 D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=12 device=00:1E:42:35:F0:4D D/SelectDeviceViewModel: Connection state changed to status: 0, sate: 2 D/SelectDeviceViewModel: Connected D/SelectDeviceViewModel: Currently connected devices: [00:1E:42:35:F0:4D] D/BluetoothGatt: onConnectionUpdated() - Device=00:1E:42:35:F0:4D interval=6 latency=0 timeout=500 status=0 D/BluetoothGatt: onConnectionUpdated() - Device=00:1E:42:35:F0:4D interval=36 latency=0 timeout=500 status=0 D/BluetoothGatt: onConnectionUpdated() - Device=00:1E:42:35:F0:4D interval=9 latency=0 timeout=600 status=0 I/SelectDeviceViewModel: onDeviceClick(macAddress=00:1E:42:35:F0:4D) I/SelectDeviceViewModel: Disconnecting D/BluetoothGatt: close() D/BluetoothGatt: unregisterApp() - mClientIf=12 D/SelectDeviceViewModel: Currently connected devices: [00:1E:42:35:F0:4D]
编辑3
正常工作的三星Android 12设备日志:
I/SelectDeviceViewModel: onDeviceClick(macAddress=00:1E:42:35:F0:4D) D/SelectDeviceViewModel: Currently connected devices: [] D/SelectDeviceViewModel: Device to connect: 00:1E:42:35:F0:4D I/BluetoothAdapter: STATE_ON D/BluetoothGatt: connect() - device: 00:1E:42:35:F0:4D, auto: false I/BluetoothAdapter: isSecureModeEnabled D/BluetoothGatt: registerApp() D/BluetoothGatt: registerApp() - UUID=931b9526-ffae-402a-a4b4-3f01edc76e46 D/BluetoothGatt: onClientRegistered() - status=0 clientIf=17 D/BluetoothGatt: onTimeSync() - eventCount=0 offset=346 D/BluetoothGatt: onClientConnectionState() - status=0 clientIf=17 device=00:1E:42:35:F0:4D D/SelectDeviceViewModel: Connection state changed to status: 0, sate: 2 D/SelectDeviceViewModel: Connected D/SelectDeviceViewModel: Currently connected devices: [00:1E:42:35:F0:4D] D/BluetoothGatt: onConnectionUpdated() - Device=00:1E:42:35:F0:4D interval=6 latency=0 timeout=500 status=0 D/BluetoothGatt: onConnectionUpdated() - Device=00:1E:42:35:F0:4D interval=38 latency=0 timeout=500 status=0 D/BluetoothGatt: onConnectionUpdated() - Device=00:1E:42:35:F0:4D interval=9 latency=0 timeout=600 status=0 I/SelectDeviceViewModel: onDeviceClick(macAddress=00:1E:42:35:F0:4D) I/SelectDeviceViewModel: Disconnecting D/BluetoothGatt: close() D/BluetoothGatt: unregisterApp() - mClientIf=17 D/SelectDeviceViewModel: Currently connected devices: []
编辑4
我尝试修改断开逻辑,先调用disconnect(),等待连接状态变为断开后再调用close(),但问题仍然存在。
内容的提问来源于stack exchange,提问作者SMGhost
相关产品推荐
相关产品推荐

