Skip to content

Commit

Permalink
Merge pull request #984 from anees17861/fix/ios-types/2023-03-17
Browse files Browse the repository at this point in the history
fix: conform ios native data to match the typescript model
  • Loading branch information
marcosinigaglia authored Mar 31, 2023
2 parents 86fb860 + d3ae8a9 commit 37de7d0
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 32 deletions.
20 changes: 10 additions & 10 deletions ios/BleManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:(C
[self invokeAndClearDictionary:readCallbacks withKey:key usingParameters:@[[NSNull null], ([characteristic.value length] > 0) ? [characteristic.value toArray] : [NSNull null]]];
} else {
if (hasListeners) {
[self sendEventWithName:@"BleManagerDidUpdateValueForCharacteristic" body:@{@"peripheral": peripheral.uuidAsString, @"characteristic":characteristic.UUID.UUIDString, @"service":characteristic.service.UUID.UUIDString, @"value": ([characteristic.value length] > 0) ? [characteristic.value toArray] : [NSNull null]}];
[self sendEventWithName:@"BleManagerDidUpdateValueForCharacteristic" body:@{@"peripheral": peripheral.uuidAsString, @"characteristic":characteristic.UUID.UUIDString.lowercaseString, @"service":characteristic.service.UUID.UUIDString.lowercaseString, @"value": ([characteristic.value length] > 0) ? [characteristic.value toArray] : [NSNull null]}];
}
}
}
Expand Down Expand Up @@ -149,7 +149,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForChara
return;
}
if (hasListeners) {
[self sendEventWithName:@"BleManagerDidUpdateNotificationStateFor" body:@{@"peripheral": peripheral.uuidAsString, @"characteristic": characteristic.UUID.UUIDString, @"isNotifying": @(false), @"domain": [error domain], @"code": @(error.code)}];
[self sendEventWithName:@"BleManagerDidUpdateNotificationStateFor" body:@{@"peripheral": peripheral.uuidAsString, @"characteristic": characteristic.UUID.UUIDString.lowercaseString, @"isNotifying": @(false), @"domain": [error domain], @"code": @(error.code)}];
}
}

Expand Down Expand Up @@ -178,7 +178,7 @@ - (void)peripheral:(CBPeripheral *)peripheral didUpdateNotificationStateForChara
}
}
if (hasListeners) {
[self sendEventWithName:@"BleManagerDidUpdateNotificationStateFor" body:@{@"peripheral": peripheral.uuidAsString, @"characteristic": characteristic.UUID.UUIDString, @"isNotifying": @(characteristic.isNotifying)}];
[self sendEventWithName:@"BleManagerDidUpdateNotificationStateFor" body:@{@"peripheral": peripheral.uuidAsString, @"characteristic": characteristic.UUID.UUIDString.lowercaseString, @"isNotifying": @(characteristic.isNotifying)}];
}
}

Expand Down Expand Up @@ -253,7 +253,7 @@ - (CBPeripheral*)findPeripheralByUUID:(NSString*)uuid {
@synchronized(peripherals) {
for (CBPeripheral *p in peripherals) {

NSString* other = p.identifier.UUIDString;
NSString* other = p.identifier.UUIDString.lowercaseString;

if ([uuid isEqualToString:other]) {
peripheral = p;
Expand Down Expand Up @@ -981,7 +981,7 @@ -(CBCharacteristic *) findCharacteristicFromUUID:(CBUUID *)UUID service:(CBServi
for(int i=0; i < service.characteristics.count; i++)
{
CBCharacteristic *c = [service.characteristics objectAtIndex:i];
if ((c.properties & prop) != 0x0 && [c.UUID.UUIDString isEqualToString: UUID.UUIDString]) {
if ((c.properties & prop) != 0x0 && [c.UUID.UUIDString.lowercaseString isEqualToString: UUID.UUIDString.lowercaseString]) {
NSLog(@"Found %@", UUID);
return c;
}
Expand All @@ -996,7 +996,7 @@ -(CBCharacteristic *) findCharacteristicFromUUID:(CBUUID *)UUID service:(CBServi
for(int i=0; i < service.characteristics.count; i++)
{
CBCharacteristic *c = [service.characteristics objectAtIndex:i];
if ([c.UUID.UUIDString isEqualToString: UUID.UUIDString]) {
if ([c.UUID.UUIDString.lowercaseString isEqualToString: UUID.UUIDString.lowercaseString]) {
NSLog(@"Found %@", UUID);
return c;
}
Expand Down Expand Up @@ -1048,10 +1048,10 @@ -(BLECommandContext*) getData:(NSString*)deviceUUIDString serviceUUIDString:(NS
{
NSString* err = [NSString stringWithFormat:@"Could not find service with UUID %@ on peripheral with UUID %@",
serviceUUIDString,
peripheral.identifier.UUIDString];
peripheral.identifier.UUIDString.lowercaseString];
NSLog(@"Could not find service with UUID %@ on peripheral with UUID %@",
serviceUUIDString,
peripheral.identifier.UUIDString);
peripheral.identifier.UUIDString.lowercaseString);
callback(@[err]);
return nil;
}
Expand All @@ -1070,11 +1070,11 @@ -(BLECommandContext*) getData:(NSString*)deviceUUIDString serviceUUIDString:(NS

if (!characteristic)
{
NSString* err = [NSString stringWithFormat:@"Could not find characteristic with UUID %@ on service with UUID %@ on peripheral with UUID %@", characteristicUUIDString,serviceUUIDString, peripheral.identifier.UUIDString];
NSString* err = [NSString stringWithFormat:@"Could not find characteristic with UUID %@ on service with UUID %@ on peripheral with UUID %@", characteristicUUIDString,serviceUUIDString, peripheral.identifier.UUIDString.lowercaseString];
NSLog(@"Could not find characteristic with UUID %@ on service with UUID %@ on peripheral with UUID %@",
characteristicUUIDString,
serviceUUIDString,
peripheral.identifier.UUIDString);
peripheral.identifier.UUIDString.lowercaseString);
callback(@[err]);
return nil;
}
Expand Down
46 changes: 24 additions & 22 deletions ios/CBPeripheral+Extensions.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ @implementation CBPeripheral(com_megster_ble_extension)

-(NSString *)uuidAsString {
if (self.identifier.UUIDString) {
return self.identifier.UUIDString;
return [self.identifier.UUIDString lowercaseString];
} else {
return @"";
}
Expand All @@ -18,7 +18,7 @@ -(NSString *)uuidAsString {
-(NSDictionary *)asDictionary {
NSString *uuidString = NULL;
if (self.identifier.UUIDString) {
uuidString = self.identifier.UUIDString;
uuidString = [self.identifier.UUIDString lowercaseString];
} else {
uuidString = @"";
}
Expand Down Expand Up @@ -112,7 +112,7 @@ - (NSDictionary *) serializableAdvertisementData: (NSDictionary *) advertisement

for (CBUUID *key in [serviceData allKeys]) {
if ([serviceData objectForKey:key]) {
[serviceData setObject:dataToArrayBuffer([serviceData objectForKey:key]) forKey:[key UUIDString]];
[serviceData setObject:dataToArrayBuffer([serviceData objectForKey:key]) forKey:[[key UUIDString] lowercaseString]];
[serviceData removeObjectForKey:key];
}
}
Expand All @@ -129,7 +129,7 @@ - (NSDictionary *) serializableAdvertisementData: (NSDictionary *) advertisement
serviceUUIDStrings = [[NSMutableArray alloc] initWithCapacity:serviceUUIDs.count];

for (CBUUID *uuid in serviceUUIDs) {
[serviceUUIDStrings addObject:[uuid UUIDString]];
[serviceUUIDStrings addObject:[[uuid UUIDString] lowercaseString]];
}

// replace the UUID list with list of strings
Expand All @@ -145,7 +145,7 @@ - (NSDictionary *) serializableAdvertisementData: (NSDictionary *) advertisement
solicitiedServiceUUIDStrings = [[NSMutableArray alloc] initWithCapacity:solicitiedServiceUUIDs.count];

for (CBUUID *uuid in solicitiedServiceUUIDs) {
[solicitiedServiceUUIDStrings addObject:[uuid UUIDString]];
[solicitiedServiceUUIDStrings addObject:[[uuid UUIDString] lowercaseString]];
}

// replace the UUID list with list of strings
Expand All @@ -161,7 +161,7 @@ - (NSDictionary *) serializableAdvertisementData: (NSDictionary *) advertisement
overflowServiceUUIDStrings = [[NSMutableArray alloc] initWithCapacity:overflowServiceUUIDs.count];

for (CBUUID *uuid in overflowServiceUUIDs) {
[overflowServiceUUIDStrings addObject:[uuid UUIDString]];
[overflowServiceUUIDStrings addObject:[[uuid UUIDString] lowercaseString]];
}

// replace the UUID list with list of strings
Expand All @@ -188,11 +188,13 @@ - (void) serviceAndCharacteristicInfo: (NSMutableDictionary *) info {

// This can move into the CBPeripherial Extension
for (CBService *service in [self services]) {
[serviceList addObject:[[service UUID] UUIDString]];
NSMutableDictionary *serviceDictionary = [NSMutableDictionary new];
[serviceDictionary setObject:[[[service UUID] UUIDString] lowercaseString] forKey:@"uuid"];
[serviceList addObject:serviceDictionary];
for (CBCharacteristic *characteristic in service.characteristics) {
NSMutableDictionary *characteristicDictionary = [NSMutableDictionary new];
[characteristicDictionary setObject:[[service UUID] UUIDString] forKey:@"service"];
[characteristicDictionary setObject:[[characteristic UUID] UUIDString] forKey:@"characteristic"];
[characteristicDictionary setObject:[[[service UUID] UUIDString] lowercaseString] forKey:@"service"];
[characteristicDictionary setObject:[[[characteristic UUID] UUIDString] lowercaseString] forKey:@"characteristic"];

if ([characteristic value] && [[characteristic value] length] > 0) {
[characteristicDictionary setObject:dataToArrayBuffer([characteristic value]) forKey:@"value"];
Expand All @@ -209,7 +211,7 @@ - (void) serviceAndCharacteristicInfo: (NSMutableDictionary *) info {
NSMutableArray *descriptorList = [NSMutableArray new];
for (CBDescriptor *descriptor in characteristic.descriptors) {
NSMutableDictionary *descriptorDictionary = [NSMutableDictionary new];
[descriptorDictionary setObject:[[descriptor UUID] UUIDString] forKey:@"uuid"];
[descriptorDictionary setObject:[[[descriptor UUID] UUIDString] lowercaseString] forKey:@"uuid"];
if ([descriptor value]) { // should always have a value?
[descriptorDictionary setObject:[descriptor value] forKey:@"value"];
}
Expand All @@ -227,50 +229,50 @@ - (void) serviceAndCharacteristicInfo: (NSMutableDictionary *) info {

}

-(NSArray *) decodeCharacteristicProperties: (CBCharacteristic *) characteristic {
NSMutableArray *props = [NSMutableArray new];
-(NSDictionary *) decodeCharacteristicProperties: (CBCharacteristic *) characteristic {
NSMutableDictionary *props = [NSMutableDictionary new];

CBCharacteristicProperties p = [characteristic properties];

// NOTE: props strings need to be consistent across iOS and Android
if ((p & CBCharacteristicPropertyBroadcast) != 0x0) {
[props addObject:@"Broadcast"];
[props setValue:@"Broadcast" forKey:@"Broadcast"];
}

if ((p & CBCharacteristicPropertyRead) != 0x0) {
[props addObject:@"Read"];
[props setValue:@"Read" forKey:@"Read"];
}

if ((p & CBCharacteristicPropertyWriteWithoutResponse) != 0x0) {
[props addObject:@"WriteWithoutResponse"];
[props setValue:@"WriteWithoutResponse" forKey:@"WriteWithoutResponse"];
}

if ((p & CBCharacteristicPropertyWrite) != 0x0) {
[props addObject:@"Write"];
[props setValue:@"Write" forKey:@"Write"];
}

if ((p & CBCharacteristicPropertyNotify) != 0x0) {
[props addObject:@"Notify"];
[props setValue:@"Notify" forKey:@"Notify"];
}

if ((p & CBCharacteristicPropertyIndicate) != 0x0) {
[props addObject:@"Indicate"];
[props setValue:@"Indicate" forKey:@"Indicate"];
}

if ((p & CBCharacteristicPropertyAuthenticatedSignedWrites) != 0x0) {
[props addObject:@"AuthenticateSignedWrites"];
[props setValue:@"AuthenticateSignedWrites" forKey:@"AuthenticateSignedWrites"];
}

if ((p & CBCharacteristicPropertyExtendedProperties) != 0x0) {
[props addObject:@"ExtendedProperties"];
[props setValue:@"ExtendedProperties" forKey:@"ExtendedProperties"];
}

if ((p & CBCharacteristicPropertyNotifyEncryptionRequired) != 0x0) {
[props addObject:@"NotifyEncryptionRequired"];
[props setValue:@"NotifyEncryptionRequired" forKey:@"NotifyEncryptionRequired"];
}

if ((p & CBCharacteristicPropertyIndicateEncryptionRequired) != 0x0) {
[props addObject:@"IndicateEncryptionRequired"];
[props setValue:@"IndicateEncryptionRequired" forKey:@"IndicateEncryptionRequired"];
}

return props;
Expand Down

0 comments on commit 37de7d0

Please sign in to comment.