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

使用Core Bluetooth处理iBecon信号我能够使用CBCentralManager扫描选项搜索nil: –
Shared.sharedInstance.centralManager?.scanForperipherals(withServices: nil,options:[CBCentralManagerScanoptionAllowDuplicatesKey:true])

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

Shared.sharedInstance.centralManager?.scanForperipherals(withServices: [serviceID],options:[CBCentralManagerScanoptionAllowDuplicatesKey:true])

它从不调用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)
        }
    }

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...