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

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

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

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

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

解决方法

我这样用,
连接按钮单击事件
并使用CBCentralManagerDelegate,CBPeripheralDelegate Delegate
func connectDevice(sender:UIButton){


                if peripheral != nil {
                    manager.cancelPeripheralConnection(peripheral)
                    manager = CBCentralManager(delegate: self,queue: nil)
                }
        }



 func centralManagerDidUpdateState(central: CBCentralManager) {
        if central.state == CBCentralManagerState.PoweredOn {
            central.scanForPeripheralsWithServices(nil,options: nil)
        } else {
            self.showAlert(Messages().alert,message: "Bluetooth is not on.")
        }
    }



 func centralManager(central: CBCentralManager,didDiscoverPeripheral peripheral: CBPeripheral,advertisementData: [String : AnyObject],RSSI: NSNumber) {
        let device = (advertisementData as NSDictionary).objectForKey(CBAdvertisementDataLocalNameKey) as? NSString
        print(device)

        if device?.containsString(BEAN_NAME) == true {
            self.manager.stopScan()
            self.peripheral = peripheral
            self.peripheral.delegate = self
            manager.connectPeripheral(peripheral,options: nil)
        }
    }@H_404_3@
原文链接:https://www.f2er.com/iOS/335332.html

猜你在找的iOS相关文章