【问题标题】:BLE scan timeout in iOS appiOS应用程序中的BLE扫描超时
【发布时间】:2015-06-25 11:21:32
【问题描述】:

我有一个应用程序,我可以在其中启动并执行 BLE 扫描。一切都很好。我只想知道是否有为扫描过程创建超时的功能。 这样,如果 BLE 扫描在一段时间内找不到任何设备,我可以向用户显示警报或弹出窗口。

欢迎提出任何建议。

【问题讨论】:

  • 可以设置一个NSTimer,一个performSelector afterDelay,查看找到的CBPeripheral列表。
  • 有没有办法通过 Core Bluetooth APIs 做到这一点?
  • 不,您只需在确定扫描时间足够长时停止扫描。
  • 谢谢保罗和拉姆

标签: ios objective-c timeout bluetooth-lowenergy core-bluetooth


【解决方案1】:

斯威夫特 3

var timer: Timer!
var countTime: Int = 0
var bleCentralManager: CBCentralManager!

override func viewWillAppear(_ animated: Bool) {

    self.timer = Timer.scheduledTimer(timeInterval: 1, target: self, selector: #selector(self.updateScanningStatus), userInfo: nil, repeats: true)
}

func updateScanningStatus(){

    if self.count >= 60{

        bleCentralManager.stopScan()

        timer.invalidate()

        timer = nil
    } else {
        count += 1
    }
}

1 分钟后 BLE 扫描停止。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-16
    相关资源
    最近更新 更多