@iotize/device-com-ble.cordova 3.3.2 → 3.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "author": "IoTize <contact@iotize.com>",
3
3
  "license": "MIT",
4
4
  "name": "@iotize/device-com-ble.cordova",
5
- "version": "3.3.2",
5
+ "version": "3.4.0",
6
6
  "cordova": {
7
7
  "id": "@iotize/device-com-ble.cordova",
8
8
  "platforms": [
package/plugin.xml CHANGED
@@ -20,10 +20,12 @@
20
20
  <framework src="src/android/build.gradle" custom="true" type="gradleReference" />
21
21
  <source-file src="src/android/src/ble" target-dir="java/com/iotize/plugin/cordova"/>
22
22
  <config-file target="AndroidManifest.xml" parent="/manifest">
23
+ <uses-permission android:name="android.permission.BLUETOOTH" android:maxSdkVersion="30" />
24
+ <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" android:maxSdkVersion="30" />
23
25
  <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
24
- <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
25
- <uses-permission android:name="android.permission.BLUETOOTH"/>
26
- <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
26
+ <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
27
+ <uses-permission android:name="android.permission.BLUETOOTH_SCAN" android:usesPermissionFlags="neverForLocation" />
28
+ <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
27
29
  </config-file>
28
30
  </platform>
29
31
 
@@ -7,7 +7,10 @@
7
7
 
8
8
  package com.iotize.plugin.cordova.ble;
9
9
 
10
+ import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
10
11
  import static android.Manifest.permission.ACCESS_FINE_LOCATION;
12
+ import static android.Manifest.permission.BLUETOOTH_CONNECT;
13
+ import static android.Manifest.permission.BLUETOOTH_SCAN;
11
14
 
12
15
  import android.annotation.SuppressLint;
13
16
  import android.app.Activity;
@@ -20,6 +23,7 @@ import android.bluetooth.BluetoothManager;
20
23
  import android.content.Context;
21
24
  import android.content.Intent;
22
25
  import android.content.pm.PackageManager;
26
+ import android.os.Build;
23
27
  import android.util.Log;
24
28
 
25
29
  import com.iotize.android.communication.protocol.ble.BLEProtocol;
@@ -76,14 +80,13 @@ public class BLECom extends CordovaPlugin {
76
80
 
77
81
 
78
82
  private static final int REQUEST_ENABLE_BLUETOOTH = 1;
79
- private static final int REQUEST_SCAN_PERMISSIONS = 2;
83
+ private static final int REQUEST_BLE_PERMISSIONS = 2;
80
84
 
81
85
  private static final String TAG = "BLECom";
82
86
  private DeviceManager<BLEProtocol> peripherals;
83
87
  private BluetoothAdapter bluetoothAdapter;
84
88
  private PluginResponse enableBluetoothCallback;
85
89
  private PluginResponse pluginResponseDiscoverDevice;
86
- private PluginResponse permissionCallback;
87
90
  private BLEScanner scanner;
88
91
  private IOnDeviceDiscovered<BLEScanner.BLEScanData> onDeviceDiscoveredCallback;
89
92
  @Nullable
@@ -108,7 +111,6 @@ public class BLECom extends CordovaPlugin {
108
111
 
109
112
  @SuppressLint("MissingPermission")
110
113
  public boolean execute(String action, JSONArray args, final CallbackContext callbackContext) {
111
-
112
114
  PluginResponse pluginResponse = new PluginResponse(
113
115
  action,
114
116
  args,
@@ -162,12 +164,7 @@ public class BLECom extends CordovaPlugin {
162
164
  boolean isConnected = peripherals.get(macAddress).isConnected();
163
165
  pluginResponse.success(isConnected);
164
166
  } else if (action.equals(ENABLE)) {
165
- if (enableBluetoothCallback != null) {
166
- Log.w(TAG, "There is already an enable request pending...");
167
- }
168
- enableBluetoothCallback = pluginResponse;
169
- Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
170
- cordova.startActivityForResult(this, intent, REQUEST_ENABLE_BLUETOOTH);
167
+ this.enableBLE(pluginResponse);
171
168
  } else if (action.equals(CHARACTERISTIC_READ)) {
172
169
  String macAddress = argsHelper.getString(0);
173
170
  UUID serviceUUID = argsHelper.getUUID(1);
@@ -217,8 +214,40 @@ public class BLECom extends CordovaPlugin {
217
214
 
218
215
  }
219
216
 
217
+ private void enableBLE(PluginResponse pluginResponse) {
218
+ if (enableBluetoothCallback != null) {
219
+ Log.w(TAG, "There is already an enable request pending...");
220
+ }
221
+ enableBluetoothCallback = pluginResponse;
222
+ if (!this._checkPermissions()) {
223
+ return;
224
+ }
225
+ Intent intent = new Intent(BluetoothAdapter.ACTION_REQUEST_ENABLE);
226
+ cordova.startActivityForResult(this, intent, REQUEST_ENABLE_BLUETOOTH);
227
+ }
228
+
229
+ private boolean _checkPermissions() {
230
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) {
231
+ if (!PermissionHelper.hasPermission(this, BLUETOOTH_SCAN) || !PermissionHelper.hasPermission(this, BLUETOOTH_CONNECT)) {
232
+ PermissionHelper.requestPermissions(this, REQUEST_BLE_PERMISSIONS, new String[]{
233
+ BLUETOOTH_SCAN,
234
+ BLUETOOTH_CONNECT
235
+ });
236
+ return false;
237
+ }
238
+ } else {
239
+ if (!PermissionHelper.hasPermission(this, ACCESS_FINE_LOCATION)) {
240
+ PermissionHelper.requestPermissions(this, REQUEST_BLE_PERMISSIONS, new String[]{
241
+ ACCESS_COARSE_LOCATION,
242
+ ACCESS_FINE_LOCATION
243
+ });
244
+ return false;
245
+ }
246
+ }
247
+ return true;
248
+ }
249
+
220
250
  private void discoverServices(PluginResponse pluginResponse, String macAddress) throws Exception {
221
- // TODO async ?
222
251
  BLEProtocol peripheral = peripherals.get(macAddress);
223
252
  peripheral.discoverServices();
224
253
  List<BluetoothGattService> services = peripheral.getGatt().getServices();
@@ -265,9 +294,7 @@ public class BLECom extends CordovaPlugin {
265
294
  private void startBLEScanner(@Nullable RequestDeviceOptions options, @NonNull PluginResponse pluginResponse) {
266
295
  pluginResponseDiscoverDevice = pluginResponse;
267
296
 
268
- if (!PermissionHelper.hasPermission(this, ACCESS_FINE_LOCATION)) {
269
- permissionCallback = pluginResponse;
270
- PermissionHelper.requestPermission(this, REQUEST_SCAN_PERMISSIONS, ACCESS_FINE_LOCATION);
297
+ if (!this._checkPermissions()) {
271
298
  return;
272
299
  }
273
300
 
@@ -350,12 +377,15 @@ public class BLECom extends CordovaPlugin {
350
377
  });
351
378
  }
352
379
 
380
+ @SuppressLint("MissingPermission")
353
381
  private void close(final PluginResponse pluginResponse, String macAddress) {
354
382
  executeAsync(() -> {
355
383
  try {
356
384
  BLEProtocol peripheral = peripherals.getIfExists(macAddress);
357
385
  if (peripheral != null) {
358
- peripheral.getGatt().close();
386
+ if (PermissionHelper.hasPermission(this, BLUETOOTH_CONNECT)) {
387
+ peripheral.getGatt().close();
388
+ }
359
389
  peripherals.remove(macAddress);
360
390
  }
361
391
  pluginResponse.success();
@@ -507,19 +537,28 @@ public class BLECom extends CordovaPlugin {
507
537
  public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
508
538
  for (int result : grantResults) {
509
539
  if (result == PackageManager.PERMISSION_DENIED) {
510
- LOG.d(TAG, "User *rejected* Coarse Location Access");
511
- if (permissionCallback != null) {
512
- this.permissionCallback.error("Location permission not granted.");
540
+ LOG.d(TAG, "User *rejected* permission ");
541
+ if (pluginResponseDiscoverDevice != null) {
542
+ pluginResponseDiscoverDevice.error(BLEComError.missingPermissionError());
543
+ pluginResponseDiscoverDevice = null;
544
+ }
545
+ if (enableBluetoothCallback != null) {
546
+ enableBluetoothCallback.error(BLEComError.missingPermissionError());
547
+ enableBluetoothCallback = null;
513
548
  }
514
549
  return;
515
550
  }
516
551
  }
517
552
 
518
553
  switch (requestCode) {
519
- case REQUEST_SCAN_PERMISSIONS:
554
+ case REQUEST_BLE_PERMISSIONS:
520
555
  LOG.d(TAG, "User granted scan permissions");
521
- this.startScan(this.requestDeviceOptions, pluginResponseDiscoverDevice);
522
- this.permissionCallback = null;
556
+ if (pluginResponseDiscoverDevice != null) {
557
+ this.startScan(this.requestDeviceOptions, pluginResponseDiscoverDevice);
558
+ }
559
+ if (enableBluetoothCallback != null) {
560
+ this.enableBLE(enableBluetoothCallback);
561
+ }
523
562
  break;
524
563
  }
525
564
  }
@@ -537,7 +576,7 @@ public class BLECom extends CordovaPlugin {
537
576
  } else {
538
577
  LOG.d(TAG, "User did *NOT* enable Bluetooth");
539
578
  if (enableBluetoothCallback != null) {
540
- enableBluetoothCallback.error("User did not enable Bluetooth");
579
+ enableBluetoothCallback.error(BLEComError.bleNotEnabled());
541
580
  }
542
581
  }
543
582
 
@@ -16,12 +16,12 @@ public class BLEComError extends Exception {
16
16
  }
17
17
 
18
18
  public BLEComError(String code, Throwable cause) {
19
- super(cause.getMessage() + " (code " + code + ")");
19
+ super(cause.getMessage());
20
20
  this.code = code;
21
21
  }
22
22
 
23
23
  public BLEComError(String code, String message) {
24
- super(message + " (code " + code + ")");
24
+ super(message);
25
25
  this.code = code;
26
26
  }
27
27
 
@@ -74,6 +74,20 @@ public class BLEComError extends Exception {
74
74
  );
75
75
  }
76
76
 
77
+ public static BLEComError bleNotEnabled() {
78
+ return new BLEComError(
79
+ Code.BLE_ADPATER_NOT_AVAILABLE,
80
+ "Use did not enable BLE"
81
+ );
82
+ }
83
+
84
+ public static BLEComError missingPermissionError() {
85
+ return new BLEComError(
86
+ Code.BLE_ADPATER_NOT_AVAILABLE,
87
+ "User has rejected BLE related permissions"
88
+ );
89
+ }
90
+
77
91
  public String getCode() {
78
92
  return code;
79
93
  }