It’s my first time posting questions, so if it’s not enough, I’ll post more.
I wrote the code writeCharacteristic using Java.
I get a return if I sent it normally, but there is no response from the device.
Map<String, Object> data = new LinkedHashMap<>();
Map<String, Object> desired = new LinkedHashMap<>();
desired.put("power", true);
desired.put("fan1", 3);
data.put("desired", desired);
JSONObject json = null;
json = new JSONObject(data);
String data2 = json.toString();
BluetoothGatt mBG = bluetoothGatt;
BluetoothGattService mSVC = bluetoothGatt.getService(java.util.UUID.fromString(WriteServiceUUID));
BluetoothGattCharacteristic mCH = mSVC.getCharacteristic(java.util.UUID.fromString(WriteCharacteristcUUID));
byte[] bytes;
try {
bytes = data2.getBytes("UTF-8");
} catch (UnsupportedEncodingException e) {
throw new RuntimeException(e);
}
mCH.setValue(bytes);
mBG.writeCharacteristic(mCH);
I want to know if I wrote something wrong or if the order is wrong.
enter image description here
@Override
public void onCharacteristicWrite(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic, int status) {
super.onCharacteristicWrite(gatt, characteristic, status);
Log.i("BLETEST", "onCharacteristicWrite");
try {
Log.i("BLETEST", "characteristic: " + characteristic.getUuid() + ", value: " + new String(characteristic.getValue(), StandardCharsets.UTF_8) + " status:" + status);
} catch (Exception e) { // UnsupportedEncodingException
Log.i("BLETEST", "characteristic: " + characteristic.getUuid() + " status:" + status);
}
The log came out like this.
New contributor
포카리tea is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.