3
我正在嘗試開發一個Android應用程序,該應用程序使用BLE連接到基於NRF51822的系統。目的是爲我的自定義特性寫一個3字節的值(RGB)。Android BLE特徵setValue無法寫入正確的數據
Android是GATT客戶端,基於NRF51的設備是GATT服務器。
我能夠建立ble連接併成功發現我的特徵。
但是數據發送部分(setValue)給我帶來麻煩。不管我寫什麼3個字節,我上NRF51側
下同常量數據是我rellevant碼(安卓)
@Override
public void onServicesDiscovered(BluetoothGatt gatt, int status) {
Log.d("BLENotifier", "BLE GAT : SERVICES DISCOVERED");
for(BluetoothGattService gs: gatt.getServices()) {
Log.d("BLENotifier", "SERVICE = " + gs.getUuid().toString());
}
//SELECT MY CHARACTERSTIC
ble_my_characterstic = gatt.getService(ble_service_uuid).getCharacteristic(ble_characterstic_uuid);
Log.d("BLENotifier", "BLE SELECTED CHARACTERSTIC " + ble_my_characterstic.getUuid().toString());
ble_connected = true;
}
public void writedata(String data){
//WRITE DATA TO MY CHARACTERSTIC
if(ble_my_characterstic != null && ble_connected == true){
my_gatt_handle.executeReliableWrite();
//ble_my_characterstic.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
ble_my_characterstic.setValue(hexStringToByteArray(data));
my_gatt_handle.writeCharacteristic(ble_my_characterstic);
Log.d("BLENotifier", "BLE WRITE DATA " + data);
}
public static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len/2];
for (int i = 0; i < len; i += 2) {
data[i/2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
Log.d("BLENotifier", "hexStringToByteArray " + Integer.toString((int)data[0]) + " " + Integer.toString((int)data[1]) + " " + Integer.toString((int)data[2]));
return data;
}
我調用writeData
方法ble_handle.writedata("0000FF")
這就是我得到的NRF51側
R = 4 | G = 239 | B= 1
感謝
任何特別的原因用於複製的字節1:1從byteData []到寫數據[]? – Ankit