@@ -107,7 +107,8 @@ public List<String> getPluginMethods() {
107
107
"listServicesAndCharacteristics" ,
108
108
"subscribeToCharacteristic" ,
109
109
"unsubscribeToCharacteristic" ,
110
- "writeToCharacteristic" ,
110
+ "writeBytesToCharacteristic" ,
111
+ "writeStringToCharacteristic" ,
111
112
"readFromCharacteristic" );
112
113
}
113
114
@@ -138,7 +139,8 @@ public Set<SignalInfo> getPluginSignals() {
138
139
signals .add (new SignalInfo ("_on_bluetooth_status_change" , String .class ));
139
140
signals .add (new SignalInfo ("_on_location_status_change" , String .class ));
140
141
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 ));
142
144
signals .add (new SignalInfo ("_on_characteristic_read" , org .godotengine .godot .Dictionary .class ));
143
145
144
146
return signals ;
@@ -356,16 +358,20 @@ public void onCharacteristicWrite(BluetoothGatt gatt,
356
358
public void onCharacteristicChanged (BluetoothGatt gatt , BluetoothGattCharacteristic characteristic
357
359
//,byte[] value, For Android Tiramisu we need this
358
360
) {
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
361
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 ();
368
363
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 );
369
375
}
370
376
};
371
377
@@ -391,7 +397,7 @@ public void disconnect() {
391
397
private void sendServicesAndCharacteristics (List <BluetoothGattService > gattServices ) {
392
398
if (gattServices == null ) return ;
393
399
394
- emitSignal ("_on_characteristic_reading " , "processing" );
400
+ emitSignal ("_on_characteristic_finding " , "processing" );
395
401
396
402
// Loops through available GATT Services.
397
403
for (BluetoothGattService gattService : gattServices ) {
@@ -432,11 +438,11 @@ private void sendServicesAndCharacteristics(List<BluetoothGattService> gattServi
432
438
characteristicData .put ("writable_no_response" , true );
433
439
}
434
440
435
- emitSignal ("_on_characteristic_read " , characteristicData );
441
+ emitSignal ("_on_characteristic_found " , characteristicData );
436
442
}
437
443
}
438
444
439
- emitSignal ("_on_characteristic_reading " , "done" );
445
+ emitSignal ("_on_characteristic_finding " , "done" );
440
446
}
441
447
442
448
// Read from characteristic
@@ -454,9 +460,34 @@ private void readFromCharacteristic(String serviceUUID, String characteristicUUI
454
460
}
455
461
}
456
462
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
458
489
@ SuppressLint ("MissingPermission" )
459
- private void writeToCharacteristic (String serviceUUID , String characteristicUUID , String data ) {
490
+ private void writeStringToCharacteristic (String serviceUUID , String characteristicUUID , String data ) {
460
491
461
492
if (connected ) {
462
493
0 commit comments