我正在尝试通过在scanForPeripheralsWithServices()
中指定CBUUID来扫描我的应用中的特定BLE设备,但如果我提到它,则从不处理扫描。
这是我的CBCentralManagerDelegate
代码:
func centralManagerDidUpdateState(central: CBCentralManager) {
switch (central.state) {
case CBCentralManagerState.PoweredOff:
print("BLE powered off")
self.clearDevices()
case CBCentralManagerState.Unauthorized:
// Indicate to user that the iOS device does not support BLE.
print("BLE not supported")
break
case CBCentralManagerState.Unknown:
// Wait for another event
print("BLE unknown event")
break
case CBCentralManagerState.PoweredOn:
print("BLE powered on")
self.startScanning()
break
case CBCentralManagerState.Resetting:
print("BLE reset")
self.clearDevices()
case CBCentralManagerState.Unsupported:
print("BLE unsupported event")
break
}
}
func startScanning() {
print("\(NSDate()) Start scanning...")
if let central = centralManager {
let dictionaryOfOptions = [CBCentralManagerScanOptionAllowDuplicatesKey : true]
let ble = [CBUUID(string: "B737D0FF-AF53-9B83-E5D2-922140A9FFFF")]
central.scanForPeripheralsWithServices(ble, options: nil)
}
}
func centralManager(central: CBCentralManager, didDiscoverPeripheral peripheral: CBPeripheral, advertisementData: [String : AnyObject], RSSI: NSNumber) {
print("Discovered peripheral \(RSSI) dBM name: \(peripheral.name)")
print("UUID: \(peripheral.identifier.UUIDString)")
print("Peripheral \(peripheral.identifier)")
.....send event to server....
sleep(delayPolling)
self.startScanning()
}
如果我更改scanForPeripheralsWithServices()
没有任何CBUUID字符串,它可以正常工作:
central.scanForPeripheralsWithServices(nil, options: dictionaryOfOptions)
我主要担心如果能够在后台和Apple文档中启动此线程,我们需要通过指定CBUUID来扫描特定设备以便能够运行我的任务。
你能告诉我我哪里错了吗?