Skip to content

Commit 83186c0

Browse files
feat(data): It is now possible to send and receive data
I still need to check out the encoding though
1 parent 931e254 commit 83186c0

File tree

1 file changed

+113
-13
lines changed

1 file changed

+113
-13
lines changed

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

+113-13
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import android.bluetooth.BluetoothGatt;
77
import android.bluetooth.BluetoothGattCallback;
88
import android.bluetooth.BluetoothGattCharacteristic;
9+
import android.bluetooth.BluetoothGattDescriptor;
910
import android.bluetooth.BluetoothGattService;
1011
import android.bluetooth.BluetoothProfile;
1112
import android.bluetooth.le.BluetoothLeScanner;
@@ -30,11 +31,13 @@
3031
import org.godotengine.godot.plugin.GodotPlugin;
3132
import org.godotengine.godot.plugin.SignalInfo;
3233

34+
import java.nio.charset.StandardCharsets;
3335
import java.util.Arrays;
3436
import java.util.HashMap;
3537
import java.util.List;
3638
import java.util.Map;
3739
import java.util.Set;
40+
import java.util.UUID;
3841

3942
public class BluetoothManager extends GodotPlugin {
4043

@@ -101,7 +104,11 @@ public List<String> getPluginMethods() {
101104
"locationStatus",
102105
"connect",
103106
"disconnect",
104-
"listServicesAndCharacteristics");
107+
"listServicesAndCharacteristics",
108+
"subscribeToCharacteristic",
109+
"unsubscribeToCharacteristic",
110+
"writeToCharacteristic",
111+
"readFromCharacteristic");
105112
}
106113

107114
public void sendDebugSignal(String s) {
@@ -131,6 +138,7 @@ public Set<SignalInfo> getPluginSignals() {
131138
signals.add(new SignalInfo("_on_bluetooth_status_change", String.class));
132139
signals.add(new SignalInfo("_on_location_status_change", String.class));
133140
signals.add(new SignalInfo("_on_connection_status_change", String.class));
141+
signals.add(new SignalInfo("_on_characteristic_reading", String.class));
134142
signals.add(new SignalInfo("_on_characteristic_read", org.godotengine.godot.Dictionary.class));
135143

136144
return signals;
@@ -292,8 +300,11 @@ public void onConnectionStateChange(final BluetoothGatt gatt, final int status,
292300

293301
break;
294302
case BluetoothProfile.STATE_CONNECTED:
295-
emitSignal("_on_connection_status_change", "connected");
296303
connected = true;
304+
// Read services and characteristics
305+
listServicesAndCharacteristics();
306+
307+
emitSignal("_on_connection_status_change", "connected");
297308

298309
break;
299310
}
@@ -321,6 +332,9 @@ public void onCharacteristicRead(BluetoothGatt gatt,
321332

322333
if (status == BluetoothGatt.GATT_SUCCESS) {
323334
sendDebugSignal("onCharacteristicRead: SUCCESS");
335+
} else {
336+
337+
sendDebugSignal("onCharacteristicRead: " + Integer.toString(status));
324338
}
325339
}
326340

@@ -342,26 +356,43 @@ public void onCharacteristicWrite(BluetoothGatt gatt,
342356
public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteristic characteristic
343357
//,byte[] value, For Android Tiramisu we need this
344358
) {
345-
sendDebugSignal("onCharacteristicChanged");
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);
363+
364+
sendDebugSignal("onCharacteristicChanged " + uuid);
365+
sendDebugSignal("onCharacteristicChanged " + Integer.toString(value.length));
366+
sendDebugSignal("onCharacteristicChanged " + s);
367+
sendDebugSignal("onCharacteristicChanged " + s1);
368+
346369
}
347370
};
348371

349372
@SuppressLint("MissingPermission")
350373
public void connect(String address) {
351-
sendDebugSignal("Connecting to device with address " + address);
352-
stopScan();
353-
bluetoothGatt = devices.get(address).getDevice().connectGatt(context, false, btleGattCallback);
374+
375+
if (!connected) {
376+
sendDebugSignal("Connecting to device with address " + address);
377+
stopScan();
378+
bluetoothGatt = devices.get(address).getDevice().connectGatt(context, false, btleGattCallback);
379+
}
354380
}
355381

356382
@SuppressLint("MissingPermission")
357383
public void disconnect() {
358-
sendDebugSignal("Disconnecting device");
359-
bluetoothGatt.disconnect();
384+
385+
if (connected) {
386+
sendDebugSignal("Disconnecting device");
387+
bluetoothGatt.disconnect();
388+
}
360389
}
361390

362391
private void sendServicesAndCharacteristics(List<BluetoothGattService> gattServices) {
363392
if (gattServices == null) return;
364393

394+
emitSignal("_on_characteristic_reading", "processing");
395+
365396
// Loops through available GATT Services.
366397
for (BluetoothGattService gattService : gattServices) {
367398

@@ -404,13 +435,82 @@ private void sendServicesAndCharacteristics(List<BluetoothGattService> gattServi
404435
emitSignal("_on_characteristic_read", characteristicData);
405436
}
406437
}
438+
439+
emitSignal("_on_characteristic_reading", "done");
440+
}
441+
442+
// Read from characteristic
443+
@SuppressLint("MissingPermission")
444+
private void readFromCharacteristic(String serviceUUID, String characteristicUUID) {
445+
446+
if (connected) {
447+
448+
UUID service = UUID.fromString(serviceUUID);
449+
UUID characteristic = UUID.fromString(characteristicUUID);
450+
451+
BluetoothGattCharacteristic c = bluetoothGatt.getService(service).getCharacteristic(characteristic);
452+
453+
bluetoothGatt.readCharacteristic(c);
454+
}
407455
}
408-
}
409456

410-
/*
457+
// Write to characteristic, automatically detects the write type
458+
@SuppressLint("MissingPermission")
459+
private void writeToCharacteristic(String serviceUUID, String characteristicUUID, String data) {
460+
461+
if (connected) {
462+
463+
UUID service = UUID.fromString(serviceUUID);
464+
UUID characteristic = UUID.fromString(characteristicUUID);
411465

466+
BluetoothGattCharacteristic c = bluetoothGatt.getService(service).getCharacteristic(characteristic);
467+
c.setValue(data);
412468

413-
Received F3:60:26:8A:AD:78
414-
Received Take
469+
if (c.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT) {
415470

416-
*/
471+
c.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
472+
473+
} else if (c.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE ) {
474+
475+
c.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
476+
}
477+
478+
bluetoothGatt.writeCharacteristic(c);
479+
}
480+
}
481+
482+
// Subscribe to characteristic
483+
@SuppressLint("MissingPermission")
484+
private void subscribeToCharacteristic(String serviceUUID, String characteristicUUID) {
485+
486+
if (connected) {
487+
488+
UUID service = UUID.fromString(serviceUUID);
489+
UUID characteristic = UUID.fromString(characteristicUUID);
490+
491+
BluetoothGattCharacteristic c = bluetoothGatt.getService(service).getCharacteristic(characteristic);
492+
bluetoothGatt.setCharacteristicNotification(c,true);
493+
494+
495+
// Set the Client Characteristic Config Descriptor to allow server initiated updates
496+
UUID CONFIG_DESCRIPTOR = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
497+
BluetoothGattDescriptor desc = c.getDescriptor(CONFIG_DESCRIPTOR);
498+
desc.setValue(BluetoothGattDescriptor.ENABLE_NOTIFICATION_VALUE);
499+
bluetoothGatt.writeDescriptor(desc);
500+
501+
}
502+
}
503+
504+
@SuppressLint("MissingPermission")
505+
private void unsubscribeToCharacteristic(String serviceUUID, String characteristicUUID) {
506+
507+
if (connected) {
508+
509+
UUID service = UUID.fromString(serviceUUID);
510+
UUID characteristic = UUID.fromString(characteristicUUID);
511+
512+
BluetoothGattCharacteristic c = bluetoothGatt.getService(service).getCharacteristic(characteristic);
513+
bluetoothGatt.setCharacteristicNotification(c,false);
514+
}
515+
}
516+
}

0 commit comments

Comments
 (0)