Not able to scan Through CBCentral Manager with service ID for iBecon Signal

独自空忆成欢 提交于 2019-12-18 09:09:10

问题


Working on iBecon signal using Core Bluetooth i am able to search with CBCentralManager scan optionn nil :-

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

But when i provide my desirable service ID i.e :-

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

it never calls didDiscoverPeripheral Delegate method, I need to scan the peripheral in background mode too and according to apple documentation you need to provide service id explicitly whenever you need to scan in background mode. Anyone can help what i am doing wrong here.


回答1:


I am using like this, connect on button click event and use 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)
        }
    }


来源:https://stackoverflow.com/questions/41899838/not-able-to-scan-through-cbcentral-manager-with-service-id-for-ibecon-signal

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!