1
1
package com .example .godotbluetooth344 ;
2
2
3
3
import android .Manifest ;
4
- import android .annotation .SuppressLint ;
5
4
import android .bluetooth .BluetoothAdapter ;
6
5
import android .bluetooth .BluetoothGatt ;
7
6
import android .bluetooth .BluetoothGattCallback ;
21
20
import android .os .Build ;
22
21
import android .os .Handler ;
23
22
import android .util .ArraySet ;
24
-
25
23
import androidx .annotation .NonNull ;
26
24
import androidx .annotation .RequiresApi ;
27
25
import androidx .core .app .ActivityCompat ;
31
29
import org .godotengine .godot .plugin .GodotPlugin ;
32
30
import org .godotengine .godot .plugin .SignalInfo ;
33
31
34
- import java .nio .charset .StandardCharsets ;
35
32
import java .util .Arrays ;
36
33
import java .util .HashMap ;
37
34
import java .util .List ;
@@ -75,7 +72,7 @@ public BluetoothManager(Godot godot) {
75
72
this .context = getActivity ().getApplicationContext ();
76
73
77
74
// Get the location manager
78
- this .locationManager = (LocationManager )this .context .getSystemService (Context .LOCATION_SERVICE );
75
+ this .locationManager = (LocationManager ) this .context .getSystemService (Context .LOCATION_SERVICE );
79
76
80
77
// Register the listener to the Bluetooth Status
81
78
IntentFilter filter = new IntentFilter (BluetoothAdapter .ACTION_STATE_CHANGED );
@@ -132,7 +129,7 @@ public void sendNewDevice(ScanResult newDevice) {
132
129
@ NonNull
133
130
@ Override
134
131
public Set <SignalInfo > getPluginSignals () {
135
- Set <SignalInfo > signals = new ArraySet <>();
132
+ Set <SignalInfo > signals = new ArraySet <>();
136
133
137
134
signals .add (new SignalInfo ("_on_debug_message" , String .class ));
138
135
signals .add (new SignalInfo ("_on_device_found" , org .godotengine .godot .Dictionary .class ));
@@ -146,7 +143,6 @@ public Set<SignalInfo> getPluginSignals() {
146
143
return signals ;
147
144
}
148
145
149
- @ SuppressLint ("MissingPermission" )
150
146
public void scan () {
151
147
152
148
if (hasLocationPermissions ()) {
@@ -160,6 +156,11 @@ public void run() {
160
156
scanning = false ;
161
157
sendDebugSignal ("Stopping scan" );
162
158
159
+ if (ActivityCompat .checkSelfPermission (context , Manifest .permission .BLUETOOTH_SCAN ) != PackageManager .PERMISSION_GRANTED ) {
160
+
161
+ sendDebugSignal ("Cannot stop a scan because you do not have Manifest.permission.BLUETOOTH_SCAN" );
162
+ return ;
163
+ }
163
164
bluetoothLeScanner .stopScan (leScanCallback );
164
165
}
165
166
}, SCAN_PERIOD );
@@ -174,11 +175,15 @@ public void run() {
174
175
}
175
176
}
176
177
177
- @ SuppressLint ("MissingPermission" )
178
178
public void stopScan () {
179
179
180
180
if (scanning ) {
181
181
scanning = false ;
182
+ if (ActivityCompat .checkSelfPermission (context , Manifest .permission .BLUETOOTH_SCAN ) != PackageManager .PERMISSION_GRANTED ) {
183
+
184
+ sendDebugSignal ("Cannot stop a scan because you do not have Manifest.permission.BLUETOOTH_SCAN" );
185
+ return ;
186
+ }
182
187
bluetoothLeScanner .stopScan (leScanCallback );
183
188
}
184
189
}
@@ -228,11 +233,16 @@ public boolean locationStatus() {
228
233
return true ;
229
234
}
230
235
231
- @ SuppressLint ("MissingPermission" )
232
236
public void listServicesAndCharacteristics () {
233
237
234
238
if (connected ) {
235
239
// Discover services and characteristics for this device
240
+ if (ActivityCompat .checkSelfPermission (context , Manifest .permission .BLUETOOTH_CONNECT ) != PackageManager .PERMISSION_GRANTED ) {
241
+ sendDebugSignal ("Cannot list services because you do not have Manifest.permission.BLUETOOTH_CONNECT" );
242
+
243
+ return ;
244
+ }
245
+
236
246
bluetoothGatt .discoverServices ();
237
247
}
238
248
}
@@ -275,9 +285,14 @@ public void onReceive(Context context, Intent intent) {
275
285
boolean isGpsEnabled = locationManager .isProviderEnabled (LocationManager .GPS_PROVIDER );
276
286
boolean isNetworkEnabled = locationManager .isProviderEnabled (LocationManager .NETWORK_PROVIDER );
277
287
278
- if (isGpsEnabled || isNetworkEnabled ) {
288
+ if (isGpsEnabled ) {
289
+
290
+ emitSignal ("_on_location_status_change" , "on" );
291
+ } else if (isNetworkEnabled ) {
292
+
279
293
emitSignal ("_on_location_status_change" , "on" );
280
294
} else {
295
+
281
296
emitSignal ("_on_location_status_change" , "off" );
282
297
}
283
298
}
@@ -287,7 +302,6 @@ public void onReceive(Context context, Intent intent) {
287
302
// Device connect call back
288
303
private final BluetoothGattCallback btleGattCallback = new BluetoothGattCallback () {
289
304
290
- @ SuppressLint ("MissingPermission" )
291
305
@ Override
292
306
// Called when a devices connects or disconnects
293
307
public void onConnectionStateChange (final BluetoothGatt gatt , final int status , final int newState ) {
@@ -343,8 +357,8 @@ public void onCharacteristicRead(BluetoothGatt gatt,
343
357
@ Override
344
358
// Result of a characteristic read operation
345
359
public void onCharacteristicWrite (BluetoothGatt gatt ,
346
- BluetoothGattCharacteristic characteristic ,
347
- int status ) {
360
+ BluetoothGattCharacteristic characteristic ,
361
+ int status ) {
348
362
sendDebugSignal ("onCharacteristicWrite" );
349
363
350
364
if (status == BluetoothGatt .GATT_SUCCESS ) {
@@ -375,21 +389,29 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
375
389
}
376
390
};
377
391
378
- @ SuppressLint ("MissingPermission" )
379
392
public void connect (String address ) {
380
393
381
394
if (!connected ) {
382
395
sendDebugSignal ("Connecting to device with address " + address );
383
396
stopScan ();
397
+ if (ActivityCompat .checkSelfPermission (context , Manifest .permission .BLUETOOTH_CONNECT ) != PackageManager .PERMISSION_GRANTED ) {
398
+ sendDebugSignal ("Cannot connect because you do not have Manifest.permission.BLUETOOTH_CONNECT" );
399
+
400
+ return ;
401
+ }
384
402
bluetoothGatt = devices .get (address ).getDevice ().connectGatt (context , false , btleGattCallback );
385
403
}
386
404
}
387
405
388
- @ SuppressLint ("MissingPermission" )
389
406
public void disconnect () {
390
407
391
408
if (connected ) {
392
409
sendDebugSignal ("Disconnecting device" );
410
+ if (ActivityCompat .checkSelfPermission (context , Manifest .permission .BLUETOOTH_CONNECT ) != PackageManager .PERMISSION_GRANTED ) {
411
+ sendDebugSignal ("Cannot disconnect because you do not have Manifest.permission.BLUETOOTH_CONNECT" );
412
+
413
+ return ;
414
+ }
393
415
bluetoothGatt .disconnect ();
394
416
}
395
417
}
@@ -446,7 +468,6 @@ private void sendServicesAndCharacteristics(List<BluetoothGattService> gattServi
446
468
}
447
469
448
470
// Read from characteristic
449
- @ SuppressLint ("MissingPermission" )
450
471
private void readFromCharacteristic (String serviceUUID , String characteristicUUID ) {
451
472
452
473
if (connected ) {
@@ -456,12 +477,17 @@ private void readFromCharacteristic(String serviceUUID, String characteristicUUI
456
477
457
478
BluetoothGattCharacteristic c = bluetoothGatt .getService (service ).getCharacteristic (characteristic );
458
479
480
+ if (ActivityCompat .checkSelfPermission (context , Manifest .permission .BLUETOOTH_CONNECT ) != PackageManager .PERMISSION_GRANTED ) {
481
+ sendDebugSignal ("Cannot read characteristics because you do not have Manifest.permission.BLUETOOTH_CONNECT" );
482
+
483
+ return ;
484
+ }
485
+
459
486
bluetoothGatt .readCharacteristic (c );
460
487
}
461
488
}
462
489
463
490
// Write bytes to characteristic, automatically detects the write type
464
- @ SuppressLint ("MissingPermission" )
465
491
private void writeBytesToCharacteristic (String serviceUUID , String characteristicUUID , byte [] data ) {
466
492
467
493
if (connected ) {
@@ -476,17 +502,21 @@ private void writeBytesToCharacteristic(String serviceUUID, String characteristi
476
502
477
503
c .setWriteType (BluetoothGattCharacteristic .WRITE_TYPE_DEFAULT );
478
504
479
- } else if (c .getWriteType () == BluetoothGattCharacteristic .WRITE_TYPE_NO_RESPONSE ) {
505
+ } else if (c .getWriteType () == BluetoothGattCharacteristic .WRITE_TYPE_NO_RESPONSE ) {
480
506
481
507
c .setWriteType (BluetoothGattCharacteristic .WRITE_TYPE_NO_RESPONSE );
482
508
}
483
509
510
+ if (ActivityCompat .checkSelfPermission (context , Manifest .permission .BLUETOOTH_CONNECT ) != PackageManager .PERMISSION_GRANTED ) {
511
+ sendDebugSignal ("Cannot write characteristic because you do not have Manifest.permission.BLUETOOTH_CONNECT" );
512
+
513
+ return ;
514
+ }
484
515
bluetoothGatt .writeCharacteristic (c );
485
516
}
486
517
}
487
518
488
519
// Write bytes to characteristic, automatically detects the write type
489
- @ SuppressLint ("MissingPermission" )
490
520
private void writeStringToCharacteristic (String serviceUUID , String characteristicUUID , String data ) {
491
521
492
522
if (connected ) {
@@ -501,17 +531,21 @@ private void writeStringToCharacteristic(String serviceUUID, String characterist
501
531
502
532
c .setWriteType (BluetoothGattCharacteristic .WRITE_TYPE_DEFAULT );
503
533
504
- } else if (c .getWriteType () == BluetoothGattCharacteristic .WRITE_TYPE_NO_RESPONSE ) {
534
+ } else if (c .getWriteType () == BluetoothGattCharacteristic .WRITE_TYPE_NO_RESPONSE ) {
505
535
506
536
c .setWriteType (BluetoothGattCharacteristic .WRITE_TYPE_NO_RESPONSE );
507
537
}
508
538
539
+ if (ActivityCompat .checkSelfPermission (context , Manifest .permission .BLUETOOTH_CONNECT ) != PackageManager .PERMISSION_GRANTED ) {
540
+ sendDebugSignal ("Cannot write characteristic because you do not have Manifest.permission.BLUETOOTH_CONNECT" );
541
+
542
+ return ;
543
+ }
509
544
bluetoothGatt .writeCharacteristic (c );
510
545
}
511
546
}
512
547
513
548
// Subscribe to characteristic
514
- @ SuppressLint ("MissingPermission" )
515
549
private void subscribeToCharacteristic (String serviceUUID , String characteristicUUID ) {
516
550
517
551
if (connected ) {
@@ -520,8 +554,12 @@ private void subscribeToCharacteristic(String serviceUUID, String characteristic
520
554
UUID characteristic = UUID .fromString (characteristicUUID );
521
555
522
556
BluetoothGattCharacteristic c = bluetoothGatt .getService (service ).getCharacteristic (characteristic );
523
- bluetoothGatt .setCharacteristicNotification (c ,true );
557
+ if (ActivityCompat .checkSelfPermission (context , Manifest .permission .BLUETOOTH_CONNECT ) != PackageManager .PERMISSION_GRANTED ) {
558
+ sendDebugSignal ("Cannot subscribe to characteristic because you do not have Manifest.permission.BLUETOOTH_CONNECT" );
524
559
560
+ return ;
561
+ }
562
+ bluetoothGatt .setCharacteristicNotification (c , true );
525
563
526
564
// Set the Client Characteristic Config Descriptor to allow server initiated updates
527
565
UUID CONFIG_DESCRIPTOR = UUID .fromString ("00002902-0000-1000-8000-00805f9b34fb" );
@@ -532,16 +570,27 @@ private void subscribeToCharacteristic(String serviceUUID, String characteristic
532
570
}
533
571
}
534
572
535
- @ SuppressLint ("MissingPermission" )
536
573
private void unsubscribeToCharacteristic (String serviceUUID , String characteristicUUID ) {
537
574
538
575
if (connected ) {
539
576
540
577
UUID service = UUID .fromString (serviceUUID );
541
578
UUID characteristic = UUID .fromString (characteristicUUID );
542
579
580
+ // Set the Client Characteristic Config Descriptor to disable server initiated updates
581
+ UUID CONFIG_DESCRIPTOR = UUID .fromString ("00002902-0000-1000-8000-00805f9b34fb" );
543
582
BluetoothGattCharacteristic c = bluetoothGatt .getService (service ).getCharacteristic (characteristic );
544
- bluetoothGatt .setCharacteristicNotification (c ,false );
583
+
584
+ BluetoothGattDescriptor desc = c .getDescriptor (CONFIG_DESCRIPTOR );
585
+ desc .setValue (BluetoothGattDescriptor .DISABLE_NOTIFICATION_VALUE );
586
+
587
+ if (ActivityCompat .checkSelfPermission (context , Manifest .permission .BLUETOOTH_CONNECT ) != PackageManager .PERMISSION_GRANTED ) {
588
+ sendDebugSignal ("Cannot unsubscribe from characteristic because you do not have Manifest.permission.BLUETOOTH_CONNECT" );
589
+
590
+ return ;
591
+ }
592
+
593
+ bluetoothGatt .writeDescriptor (desc );
545
594
}
546
595
}
547
596
}
0 commit comments