Swift BLE" didDiscoverServices"没有执行。我错过了什么吗?

时间:2017-09-11 21:39:15

标签: ios swift xcode bluetooth-lowenergy

我一直在阅读教程,使用Adafruit的nRF8001并通过Arduino将其连接到iOS设备。到目前为止,我已经正确设置了所有内容,并且在应用程序中运行良好 我正在尝试编写我自己的应用程序(现在)做同样的事情,我阅读他们的教程,据我所知,我尽可能地复制代码,到目前为止,我可以得到连接和大多数事情似乎正在发挥作用(用户界面),但是我似乎无法做任何事情来连接到设备:
这是我的AFTER连接代码:

func centralManager(_ central: CBCentralManager, didDiscover peripheral: CBPeripheral, advertisementData: [String : Any], rssi RSSI: NSNumber) {
        //What to do when it discovers a peripheral, add it to the array list
        print("Peripheral found: " + (peripheral.name ?? "Unknown Name"))
        peripheralsFoundNames.append((peripheral.name ?? "Unknown Name"))
        peripheralsFoundData.append((advertisementData.description ))
        peripheralsFoundCB.append(peripheral)
        peripheralsFoundRSSIs.append(RSSI)
    }

func centralManager(_ central: CBCentralManager, didConnect peripheral: CBPeripheral) {
        print("Connected to device!")
        displayStatusAlert(localmsg: "Connection Succesful!")
        NotificationCenter.default.post(name: Notification.Name(rawValue: DEVICE_READY_KEY), object: self)
        data?.length = 0 //clear any data that might be stored
        peripheral.discoverServices([BLETemperatureService])
        print("Here at didConnect, connected to:" + peripheral.name!)
        // Here needs to add code to check if it's a single or multi-channel device via the advertisement data or some other constant, maybe the name?
    }

正如您所看到的,我正在显式调用peripheral.discoverServices,然后我有一个执行的print语句。然后,我有以下内容(请注意以下版本无法在任何时候执行(至少不是打印声明):

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?) {
    print("here at diddisoverservices")
        if ((error) != nil){
            displayStatusAlert(localmsg: "Error: \n" + (error?.localizedDescription ?? "Error Unknown" ))
        }

        guard let services = peripheral.services
            else{
                return
        }

        for service in services {
            peripheral.discoverCharacteristics(nil, for: service)

        }
        print ("Discovered!")
    }


    func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) {
        if ((error) != nil){
            displayStatusAlert(localmsg: "Error: \n" + (error?.localizedDescription ?? "Error Unknown" ))
        }

        guard let characteristics = service.characteristics
            else{
                return
        }
        for characteristic in characteristics {
            //looks for the right characteristic
            print("looking for characteristic")


            if characteristic.uuid.isEqual(BLERXCharacteristic)  {
                deviceConnectedRXChar = characteristic

                //Once found, subscribe to the this particular characteristic
                peripheral.setNotifyValue(true, for: deviceConnectedRXChar!)

                peripheral.readValue(for: characteristic)
                print("Rx Characteristic: \(characteristic.uuid)")
            }
            if characteristic.uuid.isEqual(BLETXCharacteristic){
                deviceConnectedTXChar = characteristic
                print("Tx Characteristic: \(characteristic.uuid)")
            }
            peripheral.discoverDescriptors(for: characteristic)
        }

        print ("Characteristic discovered")
    }

    func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {

        if characteristic == deviceConnectedRXChar {
            if let ASCIIstring = NSString(data: characteristic.value!, encoding: String.Encoding.utf8.rawValue) {
                receivedDataString = ASCIIstring
                print("Value Recieved: \((receivedDataString as String))")
                NotificationCenter.default.post(name: Notification.Name(rawValue: DEVICE_SENT_DATA), object: nil)

            }
        }

1 个答案:

答案 0 :(得分:0)

func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?)CBPeripheralDelegate方法。

所以你缺少的是设置CBPeripheral对象delegate

因此,在执行peripheral.discoverServices([BLETemperatureService])之前,您需要执行peripheral.delegate = self