I am working on a project to display the list of all Bluetooth LE devises with services with specific UUIDs. In method didDiscoverPeripheral
, I save the discovered peripherals that are advertising. I use the option dictionaryWithObjectsAndKeys:[NSNumber numberWithBool:NO]
when scanning for peripherals. I have an NSTimer
to wake up every 30 seconds to update the list of discovered peripherals and see if all peripherals are still advertising. I use retrievePeripheralsWithIdentifiers
method that I pass in an array of NSUUID
of discovered and saved peripherals. This method is supposed to return an array of CBPeripherals
that are still advertising. But it returns the original array of all the peripherals that I pass in as an argument, and it never sorts out the peripherals that are no longer advertising. Am I using this method incorrectly?
savedPeripherals
is an NSDictionary
where device ID is the Key, and CBPeripheral
is the Value.
NSTimer *timer = [NSTimer scheduledTimerWithTimeInterval:30
target:self
selector:@selector(updateActivePeripherals:)
userInfo:nil
repeats:YES];
- (void) updateActivePeripherals:(NSTimer *)timer
{
NSMutableArray *peripherals = [[NSMutableArray alloc]init];
if (self.savedPeripherals.count > 0)
{
for(id key in self.savedPeripherals)
{
CBPeripheral *item = [self.savedPeripherals objectForKey:key];
NSString *identifier=[NSString stringWithFormat:@"%@", [[item identifier] UUIDString]];
NSUUID *id=[[NSUUID alloc]initWithUUIDString:identifier];
if (id)
[peripherals addObject:id];
}
}
if (peripherals.count > 0)
{
NSArray *list =[_centralManager retrievePeripheralsWithIdentifiers:peripherals];
}
}
}