From 28a610382a4fa2200bd71ff14e224fd6cc236fca Mon Sep 17 00:00:00 2001 From: Philip Peitsch Date: Sat, 13 Jul 2024 15:04:08 +1000 Subject: [PATCH] Harden up iOS service discovery (#741) --- src/ios/BLECentralPlugin.m | 47 +++++++++++++++++++++++++++++--------- 1 file changed, 36 insertions(+), 11 deletions(-) diff --git a/src/ios/BLECentralPlugin.m b/src/ios/BLECentralPlugin.m index a1bb08a1..086de15c 100644 --- a/src/ios/BLECentralPlugin.m +++ b/src/ios/BLECentralPlugin.m @@ -780,6 +780,17 @@ - (void)centralManager:(CBCentralManager *)central didFailToConnectPeripheral:(C - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error { NSLog(@"didDiscoverServices"); + if (error) { + NSLog(@"%@", error); + NSString *connectCallbackId = [connectCallbacks valueForKey:[peripheral uuidAsString]]; + if (connectCallbackId) { + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]]; + [pluginResult setKeepCallbackAsBool:TRUE]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:connectCallbackId]; + } + return; + } + // save the services to tell when all characteristics have been discovered NSMutableSet *servicesForPeriperal = [NSMutableSet new]; [servicesForPeriperal addObjectsFromArray:peripheral.services]; @@ -794,25 +805,39 @@ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForServi NSLog(@"didDiscoverCharacteristicsForService"); NSString *peripheralUUIDString = [peripheral uuidAsString]; - NSString *connectCallbackId = [connectCallbacks valueForKey:peripheralUUIDString]; - NSMutableSet *latch = [connectCallbackLatches valueForKey:peripheralUUIDString]; - - [latch removeObject:service]; - - if ([latch count] == 0) { - // Call success callback for connect + + if (error) { + NSLog(@"%@", error); + [connectCallbackLatches removeObjectForKey:peripheralUUIDString]; + NSString *connectCallbackId = [connectCallbacks valueForKey:peripheralUUIDString]; if (connectCallbackId) { - CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[peripheral asDictionary]]; + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:[error localizedDescription]]; [pluginResult setKeepCallbackAsBool:TRUE]; [self.commandDelegate sendPluginResult:pluginResult callbackId:connectCallbackId]; } - [connectCallbackLatches removeObjectForKey:peripheralUUIDString]; - } - + return; + } + NSLog(@"Found characteristics for service %@", service); for (CBCharacteristic *characteristic in service.characteristics) { NSLog(@"Characteristic %@", characteristic); } + + NSMutableSet *latch = [connectCallbackLatches valueForKey:peripheralUUIDString]; + if (latch) { + [latch removeObject:service]; + + if ([latch count] == 0) { + [connectCallbackLatches removeObjectForKey:peripheralUUIDString]; + // Call success callback for connect + NSString *connectCallbackId = [connectCallbacks valueForKey:peripheralUUIDString]; + if (connectCallbackId) { + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsDictionary:[peripheral asDictionary]]; + [pluginResult setKeepCallbackAsBool:TRUE]; + [self.commandDelegate sendPluginResult:pluginResult callbackId:connectCallbackId]; + } + } + } } - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic error:(NSError *)error {