didDiscoverServices is not being called after a BLE connection

情到浓时终转凉″ 提交于 2019-12-25 01:46:07

问题


I'm working with a BLE device which I need to verify. The BLE code I'm using is below

//Pragma Bluetooth Methods
func centralManagerDidUpdateState(_ central: CBCentralManager) {
    if central.state == .poweredOn {
        central.scanForPeripherals(withServices: nil, options: nil)
    } else {
        print("Bluetooth not available.")
    }
}

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber){
    if let peripheralName = advertisementData[CBAdvertisementDataLocalNameKey] as? String {
        if peripheralName == "test-device1" {
            self.manager.stopScan()

            self.peripheral = peripheral
            self.peripheral.delegate = self

            self.manager.connect(peripheral, options: nil)
        }
    }

}

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
    peripheral.discoverServices(nil)
}

private func peripheral(peripheral: CBPeripheral, didDiscoverServices error: Error?) {
    for service in peripheral.services! {
        let thisService = service as CBService

        if service.uuid == SERVICE_UUID {
            peripheral.discoverCharacteristics(
                nil,
                for: thisService
            )
        }
    }
}

The process follows the anticipated route by passing through didDiscover and verifying the name as 'test-device1'. However, although it goes through the didConnect method and runs peripheral.discoverServices(nil) it never reaches the didDiscoverServices method. I've stepped through it several times and it always stops at didConnect().

What am I missing?


回答1:


Method from the doc:

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)

Yours:

func peripheral(peripheral: CBPeripheral, didDiscoverServices error: Error?)

Erase it and rewrite it letting XCode autocompletion helps you, copy/paste it from the doc, or just add a _ that is missing.

The method is optional, internally CoreBluetooth framework check if the delegates responds to the selector (respondsToSelector()) and the selector includes that "_" which yours doesn't have. So it won't match and it won't call it because it's not the same.




回答2:


if this is not working

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)

then first try to scan BLE device using BLE Scanner iOS, if in that app it shows the services then the issue in your code otherwise if BLE Scanner shows 0 service then the issue is inside the Peripheral device, or you can check the services by using print(peripheral.services) after connecting to peripheral if it print nil then discoverServices not called, other wise it call.

or you can restart your iPhone sometime data stored into cache.



来源:https://stackoverflow.com/questions/49305420/diddiscoverservices-is-not-being-called-after-a-ble-connection

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