Skip to content

Commit fe274ac

Browse files
feat(read_data): We now send the byte[] data to the device
1 parent 83186c0 commit fe274ac

File tree

1 file changed

+46
-15
lines changed

1 file changed

+46
-15
lines changed

GodotBluetooth344/src/main/java/com/example/godotbluetooth344/BluetoothManager.java

+46-15
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ public List<String> getPluginMethods() {
107107
"listServicesAndCharacteristics",
108108
"subscribeToCharacteristic",
109109
"unsubscribeToCharacteristic",
110-
"writeToCharacteristic",
110+
"writeBytesToCharacteristic",
111+
"writeStringToCharacteristic",
111112
"readFromCharacteristic");
112113
}
113114

@@ -138,7 +139,8 @@ public Set<SignalInfo> getPluginSignals() {
138139
signals.add(new SignalInfo("_on_bluetooth_status_change", String.class));
139140
signals.add(new SignalInfo("_on_location_status_change", String.class));
140141
signals.add(new SignalInfo("_on_connection_status_change", String.class));
141-
signals.add(new SignalInfo("_on_characteristic_reading", String.class));
142+
signals.add(new SignalInfo("_on_characteristic_finding", String.class));
143+
signals.add(new SignalInfo("_on_characteristic_found", org.godotengine.godot.Dictionary.class));
142144
signals.add(new SignalInfo("_on_characteristic_read", org.godotengine.godot.Dictionary.class));
143145

144146
return signals;
@@ -356,16 +358,20 @@ public void onCharacteristicWrite(BluetoothGatt gatt,
356358
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic
357359
//,byte[] value, For Android Tiramisu we need this
358360
) {
359-
String uuid = characteristic.getUuid().toString();
360-
byte[] value = characteristic.getValue();
361-
String s = Arrays.toString(value);
362-
String s1 = new String(value, StandardCharsets.ISO_8859_1);
363361

364-
sendDebugSignal("onCharacteristicChanged " + uuid);
365-
sendDebugSignal("onCharacteristicChanged " + Integer.toString(value.length));
366-
sendDebugSignal("onCharacteristicChanged " + s);
367-
sendDebugSignal("onCharacteristicChanged " + s1);
362+
org.godotengine.godot.Dictionary data = new org.godotengine.godot.Dictionary();
368363

364+
String characteristic_uuid = characteristic.getUuid().toString();
365+
String service_uuid = characteristic.getService().getUuid().toString();
366+
byte[] bytes = characteristic.getValue();
367+
368+
sendDebugSignal("onCharacteristicChanged " + characteristic_uuid);
369+
370+
data.put("service_uuid", service_uuid);
371+
data.put("characteristic_uuid", characteristic_uuid);
372+
data.put("bytes", bytes);
373+
374+
emitSignal("_on_characteristic_read", data);
369375
}
370376
};
371377

@@ -391,7 +397,7 @@ public void disconnect() {
391397
private void sendServicesAndCharacteristics(List<BluetoothGattService> gattServices) {
392398
if (gattServices == null) return;
393399

394-
emitSignal("_on_characteristic_reading", "processing");
400+
emitSignal("_on_characteristic_finding", "processing");
395401

396402
// Loops through available GATT Services.
397403
for (BluetoothGattService gattService : gattServices) {
@@ -432,11 +438,11 @@ private void sendServicesAndCharacteristics(List<BluetoothGattService> gattServi
432438
characteristicData.put("writable_no_response", true);
433439
}
434440

435-
emitSignal("_on_characteristic_read", characteristicData);
441+
emitSignal("_on_characteristic_found", characteristicData);
436442
}
437443
}
438444

439-
emitSignal("_on_characteristic_reading", "done");
445+
emitSignal("_on_characteristic_finding", "done");
440446
}
441447

442448
// Read from characteristic
@@ -454,9 +460,34 @@ private void readFromCharacteristic(String serviceUUID, String characteristicUUI
454460
}
455461
}
456462

457-
// Write to characteristic, automatically detects the write type
463+
// Write bytes to characteristic, automatically detects the write type
464+
@SuppressLint("MissingPermission")
465+
private void writeBytesToCharacteristic(String serviceUUID, String characteristicUUID, byte[] data) {
466+
467+
if (connected) {
468+
469+
UUID service = UUID.fromString(serviceUUID);
470+
UUID characteristic = UUID.fromString(characteristicUUID);
471+
472+
BluetoothGattCharacteristic c = bluetoothGatt.getService(service).getCharacteristic(characteristic);
473+
c.setValue(data);
474+
475+
if (c.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT) {
476+
477+
c.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
478+
479+
} else if (c.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE ) {
480+
481+
c.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
482+
}
483+
484+
bluetoothGatt.writeCharacteristic(c);
485+
}
486+
}
487+
488+
// Write bytes to characteristic, automatically detects the write type
458489
@SuppressLint("MissingPermission")
459-
private void writeToCharacteristic(String serviceUUID, String characteristicUUID, String data) {
490+
private void writeStringToCharacteristic(String serviceUUID, String characteristicUUID, String data) {
460491

461492
if (connected) {
462493

0 commit comments

Comments
 (0)