Android 13(API33)蓝牙BLE写入Descriptor后无GATT事件问题排查
Android 13蓝牙BLE连接后反复断开:开启通知时GATT回调无响应
问题现象
- Android 13以下版本,应用可正常连接蓝牙BLE设备并运行所有功能;升级至Android 13后,设备虽能连接,但很快陷入断开重连的循环
- 问题触发于服务发现后调用
writeDescriptor开启通知的操作:此时GATTCallback的onDescriptorWrite和onCharacteristicChanged均未触发,超时后触发断开操作 - 已更新
GATTCallback适配Android 13的新回调函数,但问题仍存在
关键代码
触发问题的核心代码
byte[] newDescriptorValue = enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE; int r = bluetoothGatt.writeDescriptor(descriptor1, newDescriptorValue); result = r == BluetoothGatt.GATT_SUCCESS;
服务发现后的回调逻辑
this.setOnServicesFound(()-> { BluetoothGattCharacteristic characteristic = getNotifiable(serviceUUID, characteristicUUID, true); setNotificationState(characteristic, true); });
开启/关闭通知的完整方法
public boolean setNotificationState(BluetoothGattCharacteristic characteristic, boolean enable) { if (!this.gatt.setCharacteristicNotification(characteristic, enable)) { return false; } UUID GATT_CLIENT_CHARACTERISTIC_CONFIGURATION = uuidFromString("2902"); final BluetoothGattDescriptor descriptor = characteristic.getDescriptor(GATT_CLIENT_CHARACTERISTIC_CONFIGURATION); if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) { int res = this.gatt.writeDescriptor(descriptor, enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); return res == GATT_SUCCESS; } else { descriptor.setValue(enable ? BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE : BluetoothGattDescriptor.DISABLE_NOTIFICATION_VALUE); return this.gatt.writeDescriptor(descriptor); } }
日志信息
smp_proc_pairing_cmpl: Pairing process has failed to remote:xx:xx:xx:xx:21:3e smp_reason:UNKNOWN[113] sec_level:0x0 BTM_ReadDevInfo: Determining device_type:BLE addr_type:public btif_storage_remove_ble_bonding_keys: Removing bonding keys for bd addr:00:00:00:00:00:00 get_cod: get_cod remote_cod = 0x00001f00 bond_state_changed: Bond state changed to state=0 [0:none, 1:bonding, 2:bonded], prev_state=0, sdp_attempts = 0 [ERROR:metric_id_allocator.cc(184)] BluetoothMetricIdAllocatorFailed to forget device because device is not in paired_device_cache_ [INFO:gatt_utils.cc(1500)] gatt_end_operation status=137 op=3 subtype=2 onConnectionUpdated() - Device=xx:xx:xx:xx:21:3E interval=39 latency=0 timeout=500 status=0
疑问
请问我遗漏了什么配置或操作?
内容的提问来源于stack exchange,提问作者tabina
相关产品推荐
相关产品推荐

