Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix support for 16 & 32-bit UUIDs on iOS (#1031) #1032

Merged
merged 1 commit into from
Sep 10, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
64 changes: 46 additions & 18 deletions src/ios/BLECentralPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -1110,6 +1110,21 @@ -(NSUUID*) getUUID:(CDVInvokedUrlCommand*)command argumentAtIndex:(NSUInteger)in
return uuid;
}

-(CBUUID*) getCBUUID:(CDVInvokedUrlCommand*)command argumentAtIndex:(NSUInteger)index {
NSLog(@"getCBUUID");

NSString *uuidString = [command argumentAtIndex:index withDefault:@"" andClass:[NSString class]];
CBUUID *uuid = [self uuidStringToCBUUID:uuidString];
if (uuid == nil) {
NSString *errorMessage = [NSString stringWithFormat:@"Malformed UUID: %@", [command argumentAtIndex:index]];
NSLog(@"%@", errorMessage);
CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
[self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId];
return nil;
}
return uuid;
}

// expecting deviceUUID, serviceUUID, characteristicUUID in command.arguments
-(BLECommandContext*) getData:(CDVInvokedUrlCommand*)command prop:(CBCharacteristicProperties)prop {
NSLog(@"getData");
Expand All @@ -1121,18 +1136,15 @@ -(BLECommandContext*) getData:(CDVInvokedUrlCommand*)command prop:(CBCharacteris
return nil;
}

NSUUID *serviceNSUUID = [self getUUID:command argumentAtIndex:1];
if (serviceNSUUID == nil) {
CBUUID *serviceUUID = [self getCBUUID:command argumentAtIndex:1];
if (serviceUUID == nil) {
return nil;
}

NSUUID *characteristicNSUUID = [self getUUID:command argumentAtIndex:2];
if (characteristicNSUUID == nil) {
CBUUID *characteristicUUID = [self getCBUUID:command argumentAtIndex:2];
if (characteristicUUID == nil) {
return nil;
}

CBUUID *serviceUUID = [CBUUID UUIDWithNSUUID:serviceNSUUID];
CBUUID *characteristicUUID = [CBUUID UUIDWithNSUUID:characteristicNSUUID];

CBPeripheral *peripheral = [self findPeripheralByUUID:deviceUUID];

Expand All @@ -1151,7 +1163,7 @@ -(BLECommandContext*) getData:(CDVInvokedUrlCommand*)command prop:(CBCharacteris

if (!service) {
NSString *errorMessage = [NSString stringWithFormat:@"Could not find service with UUID %@ on peripheral with UUID %@",
serviceNSUUID.UUIDString,
serviceUUID.UUIDString,
peripheral.identifier.UUIDString];
NSLog(@"%@", errorMessage);
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
Expand All @@ -1175,8 +1187,8 @@ -(BLECommandContext*) getData:(CDVInvokedUrlCommand*)command prop:(CBCharacteris
if (!characteristic) {
NSString *errorMessage = [NSString stringWithFormat:
@"Could not find characteristic with UUID %@ on service with UUID %@ on peripheral with UUID %@",
characteristicNSUUID.UUIDString,
serviceNSUUID.UUIDString,
characteristicUUID.UUIDString,
serviceUUID.UUIDString,
peripheral.identifier.UUIDString];
NSLog(@"%@", errorMessage);
pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsString:errorMessage];
Expand Down Expand Up @@ -1281,22 +1293,38 @@ - (NSString*) centralManagerStateToString: (CBManagerState)state {
return @"Unknown state";
}

- (CBUUID *) uuidStringToCBUUID: (id)uuidString {
if (![uuidString isKindOfClass:[NSString class]]) {
NSLog(@"Malformed UUID found: %@", uuidString);
return nil;
}

if ([uuidString length] == 4 || [uuidString length] == 8) {
// For 16 & 32-bit uuids, attempt to convert directly
// This throws an unhandled internal inconsistency error if the format is not right
// that will crash the app
return [CBUUID UUIDWithString:uuidString];
}

NSUUID *nsuuid = [[NSUUID alloc]initWithUUIDString:uuidString];
if (nsuuid == nil) {
NSLog(@"Malformed UUID found: %@", uuidString);
return nil;
}

return [CBUUID UUIDWithNSUUID:nsuuid];
}

- (NSArray<CBUUID *> *) uuidStringsToCBUUIDs: (NSArray<NSString *> *)uuidStrings {
NSMutableArray *uuids = [NSMutableArray new];
for (int i = 0; i < [uuidStrings count]; i++) {
NSString *uuidString = [uuidStrings objectAtIndex: i];
if (![uuidString isKindOfClass:[NSString class]]) {
NSLog(@"Malformed UUID found: %@", uuidString);
return nil;
}

NSUUID *nsuuid = [[NSUUID alloc]initWithUUIDString:uuidString];
if (nsuuid == nil) {
NSLog(@"Malformed UUID found: %@", uuidString);
CBUUID *uuid = [self uuidStringToCBUUID:uuidString];
if (uuid == nil) {
return nil;
}

CBUUID *uuid = [CBUUID UUIDWithNSUUID:nsuuid];
[uuids addObject:uuid];
}
return uuids;
Expand Down
Loading