I'm using 'scanForPeripheralsWithServices' in iOS and successfully connecting to my intended device.
But Apple's documentation doesn't cover the case where no valid peripheral is found. What is the best practice for determining no peripherals are available, stopping the scan, and notify app users?
You can achieve that using a NSTimer
. And from the Bluetooth scanning (initializing) function, schedule that timer too.
@implementation YourClass
{
NSTimer *timer;
}
- (void)startScan
{
// Bluetooth related code
timer = [NSTimer scheduledTimerWithTimeInterval:25 target:self selector:@selector(stopScan) userInfo:nil repeats:YES];
}
// Stops the Scanning and Timer
- (void)stopScan
{
[yourCBCentralManager stopScan];
[timer invalidate];
timer = nil;
}
@end