|
8 | 8 | NSString *const keyMessage = @"message"; |
9 | 9 | NSString *const keyName = @"name"; |
10 | 10 | NSString *const keyAddress = @"address"; |
| 11 | +NSString *const keyAddresses = @"addresses"; |
11 | 12 | NSString *const keyProperties = @"properties"; |
12 | 13 | NSString *const keyRssi = @"rssi"; |
13 | 14 | NSString *const keyAdvertisement = @"advertisement"; |
@@ -842,6 +843,43 @@ - (void)retrieveConnected:(CDVInvokedUrlCommand *)command { |
842 | 843 | [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; |
843 | 844 | } |
844 | 845 |
|
| 846 | +- (void)retrievePeripheralsByAddress:(CDVInvokedUrlCommand *)command { |
| 847 | + //Ensure Bluetooth is enabled |
| 848 | + if ([self isNotInitialized:command]) { |
| 849 | + return; |
| 850 | + } |
| 851 | + |
| 852 | + //Get an array of addresses to filter by |
| 853 | + NSDictionary *obj = [self getArgsObject:command.arguments]; |
| 854 | + NSMutableArray* addresses = nil; |
| 855 | + if (obj != nil) { |
| 856 | + addresses = [self getAddresses:obj forType:keyAddresses]; |
| 857 | + } |
| 858 | + |
| 859 | + //retrievePeripheralsWithIdentifiers doesn't like nil UUID array |
| 860 | + if (addresses == nil) { |
| 861 | + addresses = [NSMutableArray array]; |
| 862 | + } |
| 863 | + |
| 864 | + //Get paired peripherals with specified addresses/identifiers |
| 865 | + NSArray* peripherals = [centralManager retrievePeripheralsWithIdentifiers:addresses]; |
| 866 | + |
| 867 | + //Array to store returned peripherals |
| 868 | + NSMutableArray* peripheralsOut = [[NSMutableArray alloc] init]; |
| 869 | + |
| 870 | + //Create an object from each peripheral containing connection ID and name, and add to array |
| 871 | + for (CBPeripheral* peripheral in peripherals) { |
| 872 | + NSMutableDictionary* peripheralOut = [NSMutableDictionary dictionary]; |
| 873 | + [self addDevice:peripheral :peripheralOut]; |
| 874 | + [peripheralsOut addObject:peripheralOut]; |
| 875 | + } |
| 876 | + |
| 877 | + //Return the array |
| 878 | + CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_OK messageAsArray:peripheralsOut]; |
| 879 | + [pluginResult setKeepCallbackAsBool:false]; |
| 880 | + [self.commandDelegate sendPluginResult:pluginResult callbackId:command.callbackId]; |
| 881 | +} |
| 882 | + |
845 | 883 | - (void)bond:(CDVInvokedUrlCommand *)command { |
846 | 884 | NSDictionary* returnObj = [NSDictionary dictionaryWithObjectsAndKeys: errorBond, keyError, logOperationUnsupported, keyMessage, nil]; |
847 | 885 | CDVPluginResult *pluginResult = [CDVPluginResult resultWithStatus:CDVCommandStatus_ERROR messageAsDictionary:returnObj]; |
@@ -3450,6 +3488,38 @@ -(NSUUID*) getAddress:(NSDictionary *)obj { |
3450 | 3488 | return [[NSUUID UUID] initWithUUIDString:addressString]; |
3451 | 3489 | } |
3452 | 3490 |
|
| 3491 | +-(NSMutableArray*) getAddresses:(NSDictionary *) dictionary forType:(NSString*) type { |
| 3492 | + NSMutableArray* addresses = [[NSMutableArray alloc] init]; |
| 3493 | + |
| 3494 | + NSArray* addressStrings = [dictionary valueForKey:type]; |
| 3495 | + |
| 3496 | + if (addressStrings == nil) { |
| 3497 | + return nil; |
| 3498 | + } |
| 3499 | + |
| 3500 | + if (![addressStrings isKindOfClass:[NSArray class]]) { |
| 3501 | + return nil; |
| 3502 | + } |
| 3503 | + |
| 3504 | + for (NSString* addressString in addressStrings) { |
| 3505 | + if (![addressString isKindOfClass:[NSString class]]) { |
| 3506 | + continue; |
| 3507 | + } |
| 3508 | + |
| 3509 | + NSUUID* address = [[NSUUID UUID] initWithUUIDString:addressString]; |
| 3510 | + |
| 3511 | + if (address != nil) { |
| 3512 | + [addresses addObject:address]; |
| 3513 | + } |
| 3514 | + } |
| 3515 | + |
| 3516 | + if (addresses.count == 0) { |
| 3517 | + return nil; |
| 3518 | + } |
| 3519 | + |
| 3520 | + return addresses; |
| 3521 | +} |
| 3522 | + |
3453 | 3523 | -(NSNumber*) getRequest:(NSDictionary *)obj { |
3454 | 3524 | NSNumber* request = [obj valueForKey:keyRequest]; |
3455 | 3525 |
|
|
0 commit comments