BLE外围代表未调用

时间:2017-11-17 01:18:06

标签: swift bluetooth-lowenergy

我最近从Swift 3升级到Swift 4,iOS 10.3.3升级到iOS 11.1。我正在开发一个使用BLE进行双向通信的应用程序。工作流程如下:

1)外围 - 广告身份

2)CENTRAL - 接收身份(处理它......)

3)CENTRAL - 回应外围设备

4)外围 - 接收来自中央的回复

5)完成

我的代码在更新之前完美运行但现在却没有。在步骤4结束时,我执行以下行:

peripheral.writeValue(encryptedData!, for: characteristic, type: .withResponse)

这应该调用以下委托方法,但它不会:

public func peripheral(_ peripheral: CBPeripheral, didWriteValueFor descriptor: CBDescriptor, error: Error?) {
        print("Did Write")
        print("Error=\(error?.localizedDescription)")
    }

它也应该(并且正在调用)PERIPHERAL设备上的以下委托方法,但它不会:

public func peripheralManager(_ peripheral: CBPeripheralManager, didReceiveWrite requests: [CBATTRequest]) {
        print("did receive write request")
}

服务和特征设置如下:

let prefs = Preferences()
            let strServiceUUID = prefs.GetString(key: Preferences.PREF_IDENTITY_SERVICE_UUID, defaultVal: "")!
            let strCharacteristicUUID = prefs.GetString(key: Preferences.PREF_IDENTITY_CHARACTERISTIC_UUID, defaultVal: "")!
            print("ServiceUUID=\(strServiceUUID)")
            print("CharacteristicUUID=\(strCharacteristicUUID)")
            mServiceUUID = CBUUID(string: strServiceUUID)
            mCharacterUUID = CBUUID(string: strCharacteristicUUID)
            mCBBluetoothServices = CBMutableService(type: mServiceUUID, primary: true)

            //lets configure the data we want to advertise for
            var characteristics : [CBCharacteristic] = []

            //let strData : String = "933911"
            //let data = strData.data(using: .utf8)
            let cbProperties: CBCharacteristicProperties = [.read, .write, .notify]
            let cbPermissions: CBAttributePermissions = [.readable, .writeable]
            mIdentityObjectCharacteristic = CBMutableCharacteristic(type: mCharacterUUID,
                                                                    properties: cbProperties,
                                                                    value: nil,
                                                                    permissions: cbPermissions)


            characteristics.append(mIdentityObjectCharacteristic)
            mCBBluetoothServices.characteristics = characteristics
            mCBPeripheralManager.add(mCBBluetoothServices)

3 个答案:

答案 0 :(得分:1)

我不确定为什么升级操作系统和Swift版本会破坏您的代码,但是我觉得您可能使用了错误的委托方法?

尝试使用此

func peripheral(CBPeripheral, didWriteValueFor: CBCharacteristic, error: Error?)

而不是

func peripheral(CBPeripheral, didWriteValueFor: CBDescriptor, error: Error?)

答案 1 :(得分:0)

Swift 4

如果有任何更新特性(例如读/写特性)。 然后' didUpdateValueFor'代表打来电话。 所以首先检查下面的委托方法。

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

    print("didUpdateValueForChar", characteristic)

    if let error1 = error{

        alertMSG(titleString: "Error", subTitleString: "Found error while read characteristic data, Plase try again", buttonTitle: "OK")

        print(error1)
    }
    else{

        print("Update Characteristic: ", characteristic)
    }
}

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

    print("Write Characteristic :", characteristic)
}

答案 2 :(得分:0)

雨燕5
iOS 13

一些要检查的东西:

  1. 请确保将外设的delegate设置为符合CBPeripheralDelegate协议的控制器(该控制器也应与实现peripheral(_:didWriteValueFor:error:)方法的控制器相同)。
  2. 确保您未指定.withoutResponse写入类型。
  3. this other answer所述,有两种非常相似的具有签名peripheral(_:didWriteValueFor:error:)的委托方法。确保您实施的是正确的。

很容易混淆两组写方法和委托方法。

由于您正在使用:

peripheral.writeValue(encryptedData!, for: characteristic, type: .withResponse) 

写入和委托对的代码应如下所示:

class BluetoothController: CBCentralManagerDelegate, CBPeripheralDelegate {

    ...
   
    func writeSomething(to characteristic: CBCharacteristic, of peripheral: CBPeripheral) {
        let something = "1234"
        
        NSLog("Writing \(something) to \(characteristic.uuid.uuidString)")
        
        peripheral.delegate = self  // <===== You may have forgotten this?
        peripheral.writeValue(something.data(using: .utf8)!,
                              for: characteristic,
                              type: .withResponse)
    }

    ...

    func peripheral(_ peripheral: CBPeripheral, didWriteValueFor characteristic: CBCharacteristic, error: Error?) {
        if error != nil {
            NSLog("Write error: \(String(describing: error))")
        } else {
            NSLog("Wrote value to \(characteristic.uuid.uuidString)")
        }
    }
}