fix(ble): discover all services then filter; some firmware rejects scoped discovery
When discoverServices is called with a specific UUID list, certain firmware returns an empty service array even though the requested service exists on the device. Match expo-duooomi-app's behavior (discoverAllServicesAndCharacteristics) by passing nil and filtering in the delegate callback. Also log discovered service UUIDs to aid future debugging.
This commit is contained in:
@@ -136,7 +136,9 @@ extension BleClient: CBCentralManagerDelegate {
|
||||
connectTimeoutWork = nil
|
||||
connectedPeripheral = peripheral
|
||||
peripheral.delegate = self
|
||||
peripheral.discoverServices([BleUUIDs.service])
|
||||
// 发现全部 service(与 expo 端 discoverAllServicesAndCharacteristics 行为对齐)。
|
||||
// 某些固件传入指定 UUID 列表时返回空,需在 didDiscoverServices 自行过滤。
|
||||
peripheral.discoverServices(nil)
|
||||
}
|
||||
|
||||
func centralManager(_ central: CBCentralManager, didFailToConnect peripheral: CBPeripheral, error: Error?) {
|
||||
@@ -177,7 +179,9 @@ extension BleClient: CBPeripheralDelegate {
|
||||
cb?(.failure(error))
|
||||
return
|
||||
}
|
||||
guard let service = peripheral.services?.first(where: { $0.uuid == BleUUIDs.service }) else {
|
||||
let services = peripheral.services ?? []
|
||||
BleLog.d("Discovered \(services.count) service(s): \(services.map { $0.uuid.uuidString })", "BLE")
|
||||
guard let service = services.first(where: { $0.uuid == BleUUIDs.service }) else {
|
||||
let cb = connectCompletion
|
||||
connectCompletion = nil
|
||||
cb?(.failure(DuooomiBleError.serviceNotFound))
|
||||
|
||||
Reference in New Issue
Block a user