【问题标题】:iOS Swift - didDiscover peripheral not called in background [duplicate]iOS Swift - didDiscover 外围设备未在后台调用[重复]
【发布时间】:2020-07-24 13:43:00
【问题描述】:

我想在后台扫描蓝牙外围设备并使用找到的外围设备进行 API 调用。我正在这样做。

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
    let theViewController = loginVC()
    APIManager.sharedInstance.pushtracerData(vc: theViewController, user_id: USERID.sharedInstance.getUserID(), SSID: peripheral.name ?? "", MAC: peripheral.identifier.uuidString, RSSI: "\(RSSI)", TST: "", tracer_or_mobile: "2") {
        
    }
}
func centralManagerDidUpdateState(_ central: CBCentralManager) {
    if central.state == .poweredOn {
        let options: [String: Any] = [CBCentralManagerScanOptionAllowDuplicatesKey:NSNumber(value: false)]
        centralManager?.scanForPeripherals(withServices: nil, options: options)
    }
}
func application(_ application: UIApplication, performFetchWithCompletionHandler completionHandler: @escaping (UIBackgroundFetchResult) -> Void) {
  
    self.centralManager = CBCentralManager.init(delegate: self, queue: DispatchQueue.main)
    completionHandler(.newData)
    
}

但是在后台,并没有调用这个方法。请帮帮我

【问题讨论】:

    标签: ios swift bluetooth


    【解决方案1】:
        centralManager?.scanForPeripherals(withServices: nil, options: options)
    

    来自docs

    您的应用可以通过指定 bluetooth-central 后台模式在后台扫描蓝牙设备。 为此,您的应用必须通过在 serviceUUIDs 参数中指定一项或多项服务来显式扫描它们。

    建议您在所有情况下扫描特定服务,不要在此处传递nil,但在后台是必需的。

    【讨论】:

    • 从哪里可以获得 serviceUUID?
    • 来自您正在扫描的设备的文档。他们应该告诉您他们宣传哪些服务。或者,您可以通过检查要发现的设备的广告数据包直接收集信息。
    • 但确实发现方法没有在后台调用。我应该如何获取广告包?
    • 通过在前台扫描您的测试设备(或者更普遍地使用像 LightBlue 这样的扫描仪)。您在搜索什么类型的设备?
    • 我正在搜索移动设备
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-03
    • 1970-01-01
    • 2014-12-06
    • 1970-01-01
    相关资源
    最近更新 更多