2016-09-20 75 views
0

当远程设备更改特征值时,我可以在应用程序上收到通知,但是当应用程序更新值时,远程设备将看不到它。无法将数据发送到特征

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

所以问题当然的应用

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

的iOS只会让我看看我的特点UUID,没有进入它的手柄,虽然有2个把手,用不同的UUID的

各具特色无论如何,在这里我得到了回调,并尝试写回:

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个,但它有2个手柄不同的UUID)

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

回答

2

你应该检查什么电学性能的研究是为您提供个性化设置。如果设置了CBCharacteristicPropertyWrite,则可以执行写入响应。如果只设置了CBCharacteristicPropertyWriteWithoutResponse,则不能执行writeValue并设置为type: CBCharacteristicWriteType.withResponse

+0

谢谢,我不确定我是否得到你。你如何检查/设置?我不确定您是否完全阅读我的问题,但我确实表示其他应用程序能够写入同一设备 – Curnelious

+0

如果您无法访问BLE设备上的固件或文档,只需尝试更改CBCharacteristicWriteType.withResponse到CBCharacteristicWriteType.withoutResponse。如果有作品,就是这样。为了改变你需要访问固件。 –

+0

哦,男人,你解决我的问题,WriteWithoutResponse!谢谢。 – Curnelious