2016-09-20 74 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

相關問題