iOS 中的蓝牙 - 如果未找到外围设备,何时停止扫描

Bluetooth in iOS - when to stop scanning if no peripherals found

我在 iOS 中使用 'scanForPeripheralsWithServices' 并成功连接到我想要的设备。

但Apple 的文档并未涵盖未找到有效外围设备的情况。确定没有可用外围设备、停止扫描并通知应用程序用户的最佳做法是什么?

您可以使用 NSTimer 来实现。并且从蓝牙扫描(初始化)功能,也安排那个定时器。

@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