@iotize/device-com-ble.cordova 3.3.1 → 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.1",
5
+ "version": "3.4.0",
6
6
  "cordova": {
7
7
  "id": "@iotize/device-com-ble.cordova",
8
8
  "platforms": [
package/plugin.xml CHANGED
@@ -1,63 +1,68 @@
1
- <?xml version="1.0" encoding="utf-8"?>
2
- <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="@iotize/device-com-ble.cordova" version="2.0.0">
3
-
4
- <name>@iotize/device-com-ble.cordova</name>
5
- <description>Bluetooth Low Energy (BLE) Plugin for IoTize devices</description>
6
- <license>MIT</license>
7
- <keywords>bluetooth, BLE, bluetooth low energy, TapNLink, IoTize</keywords>
8
-
9
-
10
- <js-module src="www/plugin.js" name="iotizeBLE">
11
- <clobbers target="iotizeBLE" />
12
- </js-module>
13
-
14
- <platform name="android">
15
- <config-file target="res/xml/config.xml" parent="/*">
16
- <feature name="BLECom">
17
- <param name="android-package" value="com.iotize.plugin.cordova.ble.BLECom"/>
18
- </feature>
19
- </config-file>
20
- <framework src="src/android/build.gradle" custom="true" type="gradleReference" />
21
- <source-file src="src/android/src/ble" target-dir="java/com/iotize/plugin/cordova"/>
22
- <config-file target="AndroidManifest.xml" parent="/manifest">
23
- <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"/>
27
- </config-file>
28
- </platform>
29
-
30
- <platform name="windows">
31
- <js-module src="src/windows/iotize-ble-com.js" name="BLECom">
32
- <merges target="" />
33
- </js-module>
34
- <framework src="src/windows/ioTizeBLE.winmd" custom="true"/>
35
- <config-file target="package.appxmanifest" parent="/Package/Capabilities">
36
- <DeviceCapability Name="bluetooth"/>
37
- <DeviceCapability Name="radios"/>
38
- </config-file>
39
- </platform>
40
-
41
- <platform name="ios">
42
- <dependency id="cordova-plugin-add-swift-support"/>
43
- <config-file target="config.xml" parent="/*">
44
- <feature name="BLECom">
45
- <param name="ios-package" value="BLECom" />
46
- </feature>
47
- </config-file>
48
- <source-file src="src/ios/BLECom.swift" />
49
- <source-file src="src/ios/BLEManager.swift" />
50
- <source-file src="src/ios/BLETapPeripheral.swift" />
51
- <source-file src="src/ios/BLEGenericPeripheral.swift" />
52
- <source-file src="src/ios/CBPeripheralConverter.swift" />
53
- <source-file src="src/ios/Queue.swift" />
54
-
55
- <config-file target="*-Info.plist" parent="NSBluetoothAlwaysUsageDescription">
56
- <string>Communicate with IoTize BLE Devices</string>
57
- </config-file>
58
- <config-file target="*-Info.plist" parent="NSBluetoothPeripheralUsageDescription">
59
- <string>Communicate with IoTize BLE Devices</string>
60
- </config-file>
61
- </platform>
62
-
63
- </plugin>
1
+ <?xml version="1.0" encoding="utf-8"?>
2
+ <plugin xmlns="http://apache.org/cordova/ns/plugins/1.0" id="@iotize/device-com-ble.cordova" version="2.0.0">
3
+
4
+ <name>@iotize/device-com-ble.cordova</name>
5
+ <description>Bluetooth Low Energy (BLE) Plugin for IoTize devices</description>
6
+ <license>MIT</license>
7
+ <keywords>bluetooth, BLE, bluetooth low energy, TapNLink, IoTize</keywords>
8
+
9
+
10
+ <js-module src="www/plugin.js" name="iotizeBLE">
11
+ <clobbers target="iotizeBLE" />
12
+ </js-module>
13
+
14
+ <platform name="android">
15
+ <config-file target="res/xml/config.xml" parent="/*">
16
+ <feature name="BLECom">
17
+ <param name="android-package" value="com.iotize.plugin.cordova.ble.BLECom"/>
18
+ </feature>
19
+ </config-file>
20
+ <framework src="src/android/build.gradle" custom="true" type="gradleReference" />
21
+ <source-file src="src/android/src/ble" target-dir="java/com/iotize/plugin/cordova"/>
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" />
25
+ <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
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" />
29
+ </config-file>
30
+ </platform>
31
+
32
+ <platform name="windows">
33
+ <js-module src="src/windows/iotize-ble-com.js" name="BLECom">
34
+ <merges target="" />
35
+ </js-module>
36
+ <framework src="src/windows/ioTizeBLE.winmd" custom="true"/>
37
+ <config-file target="package.appxmanifest" parent="/Package/Capabilities">
38
+ <DeviceCapability Name="bluetooth"/>
39
+ <DeviceCapability Name="radios"/>
40
+ </config-file>
41
+ </platform>
42
+
43
+ <platform name="ios">
44
+ <dependency id="cordova-plugin-add-swift-support"/>
45
+ <config-file target="config.xml" parent="/*">
46
+ <feature name="BLECom">
47
+ <param name="ios-package" value="BLECom" />
48
+ </feature>
49
+ </config-file>
50
+ <source-file src="src/ios/BLECom.swift" />
51
+ <source-file src="src/ios/BLEManager.swift" />
52
+ <source-file src="src/ios/BLETapPeripheral.swift" />
53
+ <source-file src="src/ios/BLEGenericPeripheral.swift" />
54
+ <source-file src="src/ios/CBPeripheralConverter.swift" />
55
+ <source-file src="src/ios/Queue.swift" />
56
+
57
+ <config-file target="*-Info.plist" parent="NSBluetoothAlwaysUsageDescription">
58
+ <string>Communicate with IoTize BLE Devices</string>
59
+ </config-file>
60
+ <config-file target="*-Info.plist" parent="NSBluetoothPeripheralUsageDescription">
61
+ <string>Communicate with IoTize BLE Devices</string>
62
+ </config-file>
63
+ <config-file target="*-Info.plist" parent="NSLocationWhenInUseUsageDescription">
64
+ <string>Communicate with IoTize BLE Devices</string>
65
+ </config-file>
66
+ </platform>
67
+
68
+ </plugin>
@@ -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();
@@ -494,6 +524,7 @@ public class BLECom extends CordovaPlugin {
494
524
  }
495
525
  BluetoothDevice device = bluetoothAdapter.getRemoteDevice(macAddress);
496
526
  peripheral = new BLEProtocol(cordova.getActivity(), device);
527
+ peripheral.setConnectionPriority(BluetoothGatt.CONNECTION_PRIORITY_BALANCED);
497
528
  peripherals.put(macAddress, peripheral);
498
529
  if (initialMTU != null) {
499
530
  peripheral.setMTU(initialMTU);
@@ -506,19 +537,28 @@ public class BLECom extends CordovaPlugin {
506
537
  public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) {
507
538
  for (int result : grantResults) {
508
539
  if (result == PackageManager.PERMISSION_DENIED) {
509
- LOG.d(TAG, "User *rejected* Coarse Location Access");
510
- if (permissionCallback != null) {
511
- 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;
512
548
  }
513
549
  return;
514
550
  }
515
551
  }
516
552
 
517
553
  switch (requestCode) {
518
- case REQUEST_SCAN_PERMISSIONS:
554
+ case REQUEST_BLE_PERMISSIONS:
519
555
  LOG.d(TAG, "User granted scan permissions");
520
- this.startScan(this.requestDeviceOptions, pluginResponseDiscoverDevice);
521
- this.permissionCallback = null;
556
+ if (pluginResponseDiscoverDevice != null) {
557
+ this.startScan(this.requestDeviceOptions, pluginResponseDiscoverDevice);
558
+ }
559
+ if (enableBluetoothCallback != null) {
560
+ this.enableBLE(enableBluetoothCallback);
561
+ }
522
562
  break;
523
563
  }
524
564
  }
@@ -536,7 +576,7 @@ public class BLECom extends CordovaPlugin {
536
576
  } else {
537
577
  LOG.d(TAG, "User did *NOT* enable Bluetooth");
538
578
  if (enableBluetoothCallback != null) {
539
- enableBluetoothCallback.error("User did not enable Bluetooth");
579
+ enableBluetoothCallback.error(BLEComError.bleNotEnabled());
540
580
  }
541
581
  }
542
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
  }