无法将数据发送到特征

当远程设备更改特征值时,我可以在应用程序上得到通知,但是当应用程序更新值时,远程设备将不会看到它。

所以,我知道我的特点是有效的,我也知道其他应用程序能够回写到这个特性(实际的设备打印它)

所以问题当然是在应用程序

更有趣的是,每个特征都有句柄 ,每个特征都有自己的UUID。

iOS只会让我看到我的特征UUID,不能访问它的句柄,尽pipe每个特征有两个句柄,不同的UUID

无论如何,在这里我得到callback,并试图回信:

func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) { print("UPDATE FROM:" , characteristic) //good! sendData(data: "check") //not good if characteristic.uuid.uuidString == characteristicUUID { if let str = NSString(data: characteristic.value!, encoding: String.Encoding.utf8.rawValue) { print("BLE:GOT:",str ) NotificationCenter.default.post(name: Notification.Name(rawValue: "Bluetooth"), object: str) } } } 

和写作:

  func sendData(data:String) { if(peripheral != nil) { //happens for sure var bytesData = [UInt8](data.utf8) let writeData = NSData (bytes: &bytesData, length: bytesData.count) peripheral.writeValue(writeData as Data, for: characteristic, type: CBCharacteristicWriteType.withResponse) } } 

并列出我的特征(打印1 UUID – 这是真的,我们只有1,但它有两个不同的UUID句柄)

  func peripheral(_ peripheral: CBPeripheral, didDiscoverCharacteristicsFor service: CBService, error: Error?) { for charateristic in service.characteristics! { print("Found CHARACTERISTIC:",charateristic as CBCharacteristic) } 

你应该检查为你的特性设置了什么特性。 如果设置了CBCharacteristicPropertyWrite则可以执行写入响应。 如果只设置CBCharacteristicPropertyWriteWithoutResponse则不能执行type: CBCharacteristicWriteType.withResponse set的writeValue