2017-06-06 33 views
0

我需要BLE的setValue()發送類似爲0x0001,爲0x0002值,的Android BLE的setValue像×0001

有誰知道如何傳輸?

例如:

private void command() { 
    ongoing.setValue(0x0001); //error 
    mBluetoothLeService.writeCharacteristic(ongoing); 
    BluetoothLeService.setCharacteristicNotification(ongoing, true); 
    ... 
    ... 
    ... 
    mBluetoothLeService.readCharacteristic(ongoing); 
} 

Thanks. 

在DeviceControlActivity.java:

private void displayGattServices(List<BluetoothGattService> gattServices){ 
    UUID UBI_COMMAND = UUID.fromString(SampleGattAttributes.UBI_ONGOING_INFO_SERVICE_UUID); 
    ... 
    ... 
    if (uuid.equals(SampleGattAttributes.UBI_ONGOING_INFO_SERVICE_UUID)){ 
     ongoing = gattService.getCharacteristic(UBI_ONGOING); 
    } 
} 
+0

「進行中」的類型是什麼? – Sweeper

+0

它應該是BluetoothGattCharacteristic,因爲它被傳遞給需要它的方法? – CmosBattery

+0

我只讀過,但看到這裏寫作:https://stackoverflow.com/questions/36163998/writing-data-to-bluetooth-le-characteristic-in-android ...值得注意的是,你可以使用像@Override的實際方法public void onCharacteristicWrite(BluetoothGatt gatt,....寫入數據,但你可能沒有按照鏈接正確寫入。 – CmosBattery

回答

1

首先定義輸入格式(字節),然後設置characterstics的值。請檢查下面的代碼段。 「writeCharactertics」功能在BluetoothGatt中可用。如果你在你的服務中沒有定義方法,那麼你也可以將「mBluetoothLeService」對象改爲「gatt」對象來寫入值。

  byte[] value = {(byte) 0x01}; 
      characteristic.setValue(value); 
      mBluetoothLeService.writeCharacteristic(characteristic); 
+0

非常感謝。 –