ios – 无法通过CBCentral Manager扫描iBecon信号的服务ID

前端之家收集整理的这篇文章主要介绍了ios – 无法通过CBCentral Manager扫描iBecon信号的服务ID前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
使用Core Bluetooth处理iBecon信号我能够使用CBCentralManager扫描选项搜索nil: –
  1. Shared.sharedInstance.centralManager?.scanForPeripherals(withServices: nil,options:[CBCentralManagerScanOptionAllowDuplicatesKey:true])

但是,当我提供我理想的服务ID,即: –

  1. Shared.sharedInstance.centralManager?.scanForPeripherals(withServices: [serviceID],options:[CBCentralManagerScanOptionAllowDuplicatesKey:true])

它从不调用didDiscoverPeripheral Delegate方法,我也需要在后台模式下扫描外设,并且根据Apple文档,您需要在需要在后台模式下扫描时明确提供服务ID.任何人都可以帮助我在这里做错事.

解决方法

我这样用,
连接按钮单击事件
并使用CBCentralManagerDelegate,CBPeripheralDelegate Delegate
  1. func connectDevice(sender:UIButton){
  2.  
  3.  
  4. if peripheral != nil {
  5. manager.cancelPeripheralConnection(peripheral)
  6. manager = CBCentralManager(delegate: self,queue: nil)
  7. }
  8. }
  9.  
  10.  
  11.  
  12. func centralManagerDidUpdateState(central: CBCentralManager) {
  13. if central.state == CBCentralManagerState.PoweredOn {
  14. central.scanForPeripheralsWithServices(nil,options: nil)
  15. } else {
  16. self.showAlert(Messages().alert,message: "Bluetooth is not on.")
  17. }
  18. }
  19.  
  20.  
  21.  
  22. func centralManager(central: CBCentralManager,didDiscoverPeripheral peripheral: CBPeripheral,advertisementData: [String : AnyObject],RSSI: NSNumber) {
  23. let device = (advertisementData as NSDictionary).objectForKey(CBAdvertisementDataLocalNameKey) as? NSString
  24. print(device)
  25.  
  26. if device?.containsString(BEAN_NAME) == true {
  27. self.manager.stopScan()
  28. self.peripheral = peripheral
  29. self.peripheral.delegate = self
  30. manager.connectPeripheral(peripheral,options: nil)
  31. }
  32. }

猜你在找的iOS相关文章