Skip to content

Commit

Permalink
Harden up iOS service discovery (#741)
Browse files Browse the repository at this point in the history
  • Loading branch information
peitschie committed Jul 13, 2024
1 parent c77a0fd commit b11e69c
Showing 1 changed file with 36 additions and 11 deletions.
47 changes: 36 additions & 11 deletions src/ios/BLECentralPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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 {
Expand Down

0 comments on commit b11e69c

Please sign in to comment.