【问题标题】:Bluetooth in iOS - when to stop scanning if no peripherals foundiOS 中的蓝牙 - 如果未找到外围设备,何时停止扫描
【发布时间】:2015-01-16 04:04:01
【问题描述】:

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

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

【问题讨论】:

  • 我不明白,你想达到什么目的?停止扫描或其他什么?
  • 如果在合理的时间窗口内没有找到有效的外设,我想停止扫描并通知用户。

标签: ios bluetooth cbperipheralmanager


【解决方案1】:

您可以使用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

【讨论】:

  • 太棒了。我添加了一个布尔变量来监控是否在该时间间隔内找到了我想要的外围设备。
猜你喜欢
  • 1970-01-01
  • 2021-06-08
  • 2021-12-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-06-08
  • 1970-01-01
相关资源
最近更新 更多