Skip to content

Commit 435480d

Browse files
feat(all): Adding checks, cleaning code
1 parent fe274ac commit 435480d

File tree

4 files changed

+81
-25
lines changed

4 files changed

+81
-25
lines changed

GodotBluetooth344/build.gradle

+3-1
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,10 @@ dependencies {
2929

3030
implementation 'androidx.appcompat:appcompat:1.4.1'
3131
implementation 'com.google.android.material:material:1.5.0'
32-
compileOnly files('libs\\godot-lib.3.4.4.stable.release.aar')
32+
compileOnly files('libs/godot-lib.3.4.4.stable.release.aar')
3333
testImplementation 'junit:junit:4.13.2'
3434
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
3535
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
36+
implementation "androidx.activity:activity:1.4.0"
37+
implementation "androidx.fragment:fragment:1.4.1"
3638
}

GodotBluetooth344/src/main/AndroidManifest.xml

+3
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77
android:name="org.godotengine.plugin.v1.GodotBluetooth344"
88
android:value="com.example.godotbluetooth344.BluetoothManager" />
99
/>
10+
11+
<activity android:name="com.example.godotbluetooth344.PermissionsActivity"
12+
android:launchMode="singleTask"/>
1013
</application>
1114

1215
<!-- Request legacy Bluetooth permissions on older devices. -->

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

+72-23
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package com.example.godotbluetooth344;
22

33
import android.Manifest;
4-
import android.annotation.SuppressLint;
54
import android.bluetooth.BluetoothAdapter;
65
import android.bluetooth.BluetoothGatt;
76
import android.bluetooth.BluetoothGattCallback;
@@ -21,7 +20,6 @@
2120
import android.os.Build;
2221
import android.os.Handler;
2322
import android.util.ArraySet;
24-
2523
import androidx.annotation.NonNull;
2624
import androidx.annotation.RequiresApi;
2725
import androidx.core.app.ActivityCompat;
@@ -31,7 +29,6 @@
3129
import org.godotengine.godot.plugin.GodotPlugin;
3230
import org.godotengine.godot.plugin.SignalInfo;
3331

34-
import java.nio.charset.StandardCharsets;
3532
import java.util.Arrays;
3633
import java.util.HashMap;
3734
import java.util.List;
@@ -75,7 +72,7 @@ public BluetoothManager(Godot godot) {
7572
this.context = getActivity().getApplicationContext();
7673

7774
// Get the location manager
78-
this.locationManager = (LocationManager)this.context.getSystemService(Context.LOCATION_SERVICE);
75+
this.locationManager = (LocationManager) this.context.getSystemService(Context.LOCATION_SERVICE);
7976

8077
// Register the listener to the Bluetooth Status
8178
IntentFilter filter = new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED);
@@ -132,7 +129,7 @@ public void sendNewDevice(ScanResult newDevice) {
132129
@NonNull
133130
@Override
134131
public Set<SignalInfo> getPluginSignals() {
135-
Set<SignalInfo> signals = new ArraySet<>();
132+
Set<SignalInfo> signals = new ArraySet<>();
136133

137134
signals.add(new SignalInfo("_on_debug_message", String.class));
138135
signals.add(new SignalInfo("_on_device_found", org.godotengine.godot.Dictionary.class));
@@ -146,7 +143,6 @@ public Set<SignalInfo> getPluginSignals() {
146143
return signals;
147144
}
148145

149-
@SuppressLint("MissingPermission")
150146
public void scan() {
151147

152148
if (hasLocationPermissions()) {
@@ -160,6 +156,11 @@ public void run() {
160156
scanning = false;
161157
sendDebugSignal("Stopping scan");
162158

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+
}
163164
bluetoothLeScanner.stopScan(leScanCallback);
164165
}
165166
}, SCAN_PERIOD);
@@ -174,11 +175,15 @@ public void run() {
174175
}
175176
}
176177

177-
@SuppressLint("MissingPermission")
178178
public void stopScan() {
179179

180180
if (scanning) {
181181
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+
}
182187
bluetoothLeScanner.stopScan(leScanCallback);
183188
}
184189
}
@@ -228,11 +233,16 @@ public boolean locationStatus() {
228233
return true;
229234
}
230235

231-
@SuppressLint("MissingPermission")
232236
public void listServicesAndCharacteristics() {
233237

234238
if (connected) {
235239
// 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+
236246
bluetoothGatt.discoverServices();
237247
}
238248
}
@@ -275,9 +285,14 @@ public void onReceive(Context context, Intent intent) {
275285
boolean isGpsEnabled = locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER);
276286
boolean isNetworkEnabled = locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER);
277287

278-
if (isGpsEnabled || isNetworkEnabled) {
288+
if (isGpsEnabled) {
289+
290+
emitSignal("_on_location_status_change", "on");
291+
} else if (isNetworkEnabled) {
292+
279293
emitSignal("_on_location_status_change", "on");
280294
} else {
295+
281296
emitSignal("_on_location_status_change", "off");
282297
}
283298
}
@@ -287,7 +302,6 @@ public void onReceive(Context context, Intent intent) {
287302
// Device connect call back
288303
private final BluetoothGattCallback btleGattCallback = new BluetoothGattCallback() {
289304

290-
@SuppressLint("MissingPermission")
291305
@Override
292306
// Called when a devices connects or disconnects
293307
public void onConnectionStateChange(final BluetoothGatt gatt, final int status, final int newState) {
@@ -343,8 +357,8 @@ public void onCharacteristicRead(BluetoothGatt gatt,
343357
@Override
344358
// Result of a characteristic read operation
345359
public void onCharacteristicWrite(BluetoothGatt gatt,
346-
BluetoothGattCharacteristic characteristic,
347-
int status) {
360+
BluetoothGattCharacteristic characteristic,
361+
int status) {
348362
sendDebugSignal("onCharacteristicWrite");
349363

350364
if (status == BluetoothGatt.GATT_SUCCESS) {
@@ -375,21 +389,29 @@ public void onCharacteristicChanged(BluetoothGatt gatt, BluetoothGattCharacteris
375389
}
376390
};
377391

378-
@SuppressLint("MissingPermission")
379392
public void connect(String address) {
380393

381394
if (!connected) {
382395
sendDebugSignal("Connecting to device with address " + address);
383396
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+
}
384402
bluetoothGatt = devices.get(address).getDevice().connectGatt(context, false, btleGattCallback);
385403
}
386404
}
387405

388-
@SuppressLint("MissingPermission")
389406
public void disconnect() {
390407

391408
if (connected) {
392409
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+
}
393415
bluetoothGatt.disconnect();
394416
}
395417
}
@@ -446,7 +468,6 @@ private void sendServicesAndCharacteristics(List<BluetoothGattService> gattServi
446468
}
447469

448470
// Read from characteristic
449-
@SuppressLint("MissingPermission")
450471
private void readFromCharacteristic(String serviceUUID, String characteristicUUID) {
451472

452473
if (connected) {
@@ -456,12 +477,17 @@ private void readFromCharacteristic(String serviceUUID, String characteristicUUI
456477

457478
BluetoothGattCharacteristic c = bluetoothGatt.getService(service).getCharacteristic(characteristic);
458479

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+
459486
bluetoothGatt.readCharacteristic(c);
460487
}
461488
}
462489

463490
// Write bytes to characteristic, automatically detects the write type
464-
@SuppressLint("MissingPermission")
465491
private void writeBytesToCharacteristic(String serviceUUID, String characteristicUUID, byte[] data) {
466492

467493
if (connected) {
@@ -476,17 +502,21 @@ private void writeBytesToCharacteristic(String serviceUUID, String characteristi
476502

477503
c.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
478504

479-
} else if (c.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE ) {
505+
} else if (c.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE) {
480506

481507
c.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
482508
}
483509

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+
}
484515
bluetoothGatt.writeCharacteristic(c);
485516
}
486517
}
487518

488519
// Write bytes to characteristic, automatically detects the write type
489-
@SuppressLint("MissingPermission")
490520
private void writeStringToCharacteristic(String serviceUUID, String characteristicUUID, String data) {
491521

492522
if (connected) {
@@ -501,17 +531,21 @@ private void writeStringToCharacteristic(String serviceUUID, String characterist
501531

502532
c.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
503533

504-
} else if (c.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE ) {
534+
} else if (c.getWriteType() == BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE) {
505535

506536
c.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_NO_RESPONSE);
507537
}
508538

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+
}
509544
bluetoothGatt.writeCharacteristic(c);
510545
}
511546
}
512547

513548
// Subscribe to characteristic
514-
@SuppressLint("MissingPermission")
515549
private void subscribeToCharacteristic(String serviceUUID, String characteristicUUID) {
516550

517551
if (connected) {
@@ -520,8 +554,12 @@ private void subscribeToCharacteristic(String serviceUUID, String characteristic
520554
UUID characteristic = UUID.fromString(characteristicUUID);
521555

522556
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");
524559

560+
return;
561+
}
562+
bluetoothGatt.setCharacteristicNotification(c, true);
525563

526564
// Set the Client Characteristic Config Descriptor to allow server initiated updates
527565
UUID CONFIG_DESCRIPTOR = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
@@ -532,16 +570,27 @@ private void subscribeToCharacteristic(String serviceUUID, String characteristic
532570
}
533571
}
534572

535-
@SuppressLint("MissingPermission")
536573
private void unsubscribeToCharacteristic(String serviceUUID, String characteristicUUID) {
537574

538575
if (connected) {
539576

540577
UUID service = UUID.fromString(serviceUUID);
541578
UUID characteristic = UUID.fromString(characteristicUUID);
542579

580+
// Set the Client Characteristic Config Descriptor to disable server initiated updates
581+
UUID CONFIG_DESCRIPTOR = UUID.fromString("00002902-0000-1000-8000-00805f9b34fb");
543582
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);
545594
}
546595
}
547596
}

app/build.gradle

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,7 @@ dependencies {
3333
implementation 'com.google.android.material:material:1.5.0'
3434
testImplementation 'junit:junit:4.13.2'
3535
androidTestImplementation 'androidx.test.ext:junit:1.1.3'
36-
androidTestImplementation 'androidx.test.espresso:espresso-core:3.4.0'
36+
implementation "androidx.activity:activity:1.4.0"
37+
implementation "androidx.fragment:fragment:1.4.1"
38+
3739
}

0 commit comments

Comments
 (0)