0
我在iOS中使用'scanForPeripheralsWithServices'併成功連接到我的預期設備。iOS中的藍牙 - 如果沒有找到外設,何時停止掃描
但是Apple的文檔沒有涵蓋沒有找到有效外設的情況。確定沒有外設可用的最佳做法是什麼,停止掃描並通知應用程序用戶?
我在iOS中使用'scanForPeripheralsWithServices'併成功連接到我的預期設備。iOS中的藍牙 - 如果沒有找到外設,何時停止掃描
但是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
太好了。我添加了一個布爾變量來監視在該時間間隔內是否找到了我想要的外設。 – Brendenw
我不明白,你想達到什麼目的?停止掃描或其他任何東西? –
如果在合理的時間窗口內未找到有效的外圍設備,我想停止掃描並通知用戶。 – Brendenw