@capgo/capacitor-bluetooth-low-energy 8.0.6 → 8.1.1

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/README.md CHANGED
@@ -782,9 +782,10 @@ Remove all listeners for this plugin.
782
782
 
783
783
  Initialization options for the plugin.
784
784
 
785
- | Prop | Type | Description | Default | Since |
786
- | ---------- | -------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | ----- |
787
- | **`mode`** | <code>'central' \| 'peripheral'</code> | The mode to initialize the plugin in. - 'central': Act as a BLE central (client) - 'peripheral': Act as a BLE peripheral (server) | <code>'central'</code> | 1.0.0 |
785
+ | Prop | Type | Description | Default | Since |
786
+ | -------------------- | -------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------- | ---------------------- | ----- |
787
+ | **`mode`** | <code>'central' \| 'peripheral'</code> | The mode to initialize the plugin in. - 'central': Act as a BLE central (client) - 'peripheral': Act as a BLE peripheral (server) | <code>'central'</code> | 1.0.0 |
788
+ | **`showPowerAlert`** | <code>boolean</code> | Whether iOS should show the system Bluetooth power alert when Bluetooth is off (iOS only). | <code>true</code> | 8.0.7 |
788
789
 
789
790
 
790
791
  #### IsAvailableResult
@@ -1116,12 +1117,12 @@ Options for requesting connection priority.
1116
1117
 
1117
1118
  Options for starting advertising.
1118
1119
 
1119
- | Prop | Type | Description | Default | Since |
1120
- | ------------------------- | --------------------- | -------------------------------------------------------- | ------------------ | ----- |
1121
- | **`name`** | <code>string</code> | The device name to advertise. | | 1.0.0 |
1122
- | **`services`** | <code>string[]</code> | Service UUIDs to advertise. | | 1.0.0 |
1123
- | **`includeName`** | <code>boolean</code> | Whether to include the device name in the advertisement. | <code>true</code> | 1.0.0 |
1124
- | **`includeTxPowerLevel`** | <code>boolean</code> | Whether to include TX power level in the advertisement. | <code>false</code> | 1.0.0 |
1120
+ | Prop | Type | Description | Default | Since |
1121
+ | ------------------------- | --------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ | ------------------ | ----- |
1122
+ | **`name`** | <code>string</code> | The device name to advertise. On Android, advertising the local name is implemented by temporarily setting the Bluetooth adapter name while advertising, then restoring the previous adapter name when advertising is stopped. | | 1.0.0 |
1123
+ | **`services`** | <code>string[]</code> | Service UUIDs to advertise. | | 1.0.0 |
1124
+ | **`includeName`** | <code>boolean</code> | Whether to include the device name in the advertisement. | <code>true</code> | 1.0.0 |
1125
+ | **`includeTxPowerLevel`** | <code>boolean</code> | Whether to include TX power level in the advertisement. | <code>false</code> | 1.0.0 |
1125
1126
 
1126
1127
 
1127
1128
  #### StartForegroundServiceOptions
@@ -75,7 +75,7 @@ import java.util.UUID;
75
75
  )
76
76
  public class BluetoothLowEnergyPlugin extends Plugin {
77
77
 
78
- private final String pluginVersion = "8.0.6";
78
+ private final String pluginVersion = "8.1.1";
79
79
  private BluetoothManager bluetoothManager;
80
80
  private BluetoothAdapter bluetoothAdapter;
81
81
  private BluetoothLeScanner bluetoothLeScanner;
@@ -91,6 +91,9 @@ public class BluetoothLowEnergyPlugin extends Plugin {
91
91
  private boolean isScanning = false;
92
92
  private String mode = "central";
93
93
 
94
+ private String originalBluetoothAdapterName;
95
+ private String advertisingBluetoothAdapterName;
96
+
94
97
  private PluginCall pendingConnectCall;
95
98
  private PluginCall pendingDiscoverCall;
96
99
  private PluginCall pendingReadCall;
@@ -309,10 +312,18 @@ public class BluetoothLowEnergyPlugin extends Plugin {
309
312
 
310
313
  JSObject deviceObj = new JSObject();
311
314
  deviceObj.put("deviceId", deviceId);
312
- try {
313
- deviceObj.put("name", device.getName());
314
- } catch (SecurityException e) {
315
- deviceObj.put("name", null);
315
+ String name = null;
316
+ if (result.getScanRecord() != null) {
317
+ name = result.getScanRecord().getDeviceName();
318
+ }
319
+ if (name != null) {
320
+ deviceObj.put("name", name);
321
+ } else {
322
+ try {
323
+ deviceObj.put("name", device.getName());
324
+ } catch (SecurityException e) {
325
+ deviceObj.put("name", null);
326
+ }
316
327
  }
317
328
  deviceObj.put("rssi", result.getRssi());
318
329
 
@@ -891,6 +902,11 @@ public class BluetoothLowEnergyPlugin extends Plugin {
891
902
  }
892
903
 
893
904
  String name = call.getString("name");
905
+ if (name != null && name.trim().isEmpty()) {
906
+ name = null;
907
+ }
908
+ boolean includeName = call.getBoolean("includeName", true);
909
+ boolean includeTxPowerLevel = call.getBoolean("includeTxPowerLevel", false);
894
910
  JSArray servicesArray = call.getArray("services");
895
911
 
896
912
  AdvertiseSettings settings = new AdvertiseSettings.Builder()
@@ -900,8 +916,15 @@ public class BluetoothLowEnergyPlugin extends Plugin {
900
916
  .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM)
901
917
  .build();
902
918
 
919
+ if (includeName && name != null) {
920
+ if (!setBluetoothAdapterNameForAdvertising(name, call)) {
921
+ return;
922
+ }
923
+ }
924
+
903
925
  AdvertiseData.Builder dataBuilder = new AdvertiseData.Builder()
904
- .setIncludeDeviceName(name != null);
926
+ .setIncludeDeviceName(includeName)
927
+ .setIncludeTxPowerLevel(includeTxPowerLevel);
905
928
 
906
929
  if (servicesArray != null) {
907
930
  try {
@@ -923,6 +946,7 @@ public class BluetoothLowEnergyPlugin extends Plugin {
923
946
  @Override
924
947
  public void onStartFailure(int errorCode) {
925
948
  // Advertising failed
949
+ restoreBluetoothAdapterNameIfNeeded();
926
950
  }
927
951
  };
928
952
 
@@ -930,6 +954,7 @@ public class BluetoothLowEnergyPlugin extends Plugin {
930
954
  bluetoothLeAdvertiser.startAdvertising(settings, dataBuilder.build(), advertiseCallback);
931
955
  call.resolve();
932
956
  } catch (SecurityException e) {
957
+ restoreBluetoothAdapterNameIfNeeded();
933
958
  call.reject("Permission denied: " + e.getMessage());
934
959
  }
935
960
  }
@@ -943,6 +968,7 @@ public class BluetoothLowEnergyPlugin extends Plugin {
943
968
  // Ignore
944
969
  }
945
970
  }
971
+ restoreBluetoothAdapterNameIfNeeded();
946
972
  call.resolve();
947
973
  }
948
974
 
@@ -1116,6 +1142,53 @@ public class BluetoothLowEnergyPlugin extends Plugin {
1116
1142
  }
1117
1143
  }
1118
1144
 
1145
+ private boolean setBluetoothAdapterNameForAdvertising(String name, PluginCall call) {
1146
+ if (bluetoothAdapter == null) {
1147
+ call.reject("Bluetooth adapter not available");
1148
+ return false;
1149
+ }
1150
+
1151
+ try {
1152
+ if (originalBluetoothAdapterName == null) {
1153
+ originalBluetoothAdapterName = bluetoothAdapter.getName();
1154
+ }
1155
+ advertisingBluetoothAdapterName = name;
1156
+
1157
+ boolean updated = bluetoothAdapter.setName(name);
1158
+ if (!updated) {
1159
+ restoreBluetoothAdapterNameIfNeeded();
1160
+ call.reject("Failed to set Bluetooth adapter name for advertising");
1161
+ return false;
1162
+ }
1163
+
1164
+ return true;
1165
+ } catch (SecurityException e) {
1166
+ restoreBluetoothAdapterNameIfNeeded();
1167
+ call.reject("Permission denied: " + e.getMessage());
1168
+ return false;
1169
+ }
1170
+ }
1171
+
1172
+ private void restoreBluetoothAdapterNameIfNeeded() {
1173
+ if (bluetoothAdapter == null || originalBluetoothAdapterName == null) {
1174
+ advertisingBluetoothAdapterName = null;
1175
+ originalBluetoothAdapterName = null;
1176
+ return;
1177
+ }
1178
+
1179
+ try {
1180
+ String currentName = bluetoothAdapter.getName();
1181
+ if (advertisingBluetoothAdapterName != null && advertisingBluetoothAdapterName.equals(currentName)) {
1182
+ bluetoothAdapter.setName(originalBluetoothAdapterName);
1183
+ }
1184
+ } catch (SecurityException e) {
1185
+ // Ignore
1186
+ } finally {
1187
+ advertisingBluetoothAdapterName = null;
1188
+ originalBluetoothAdapterName = null;
1189
+ }
1190
+ }
1191
+
1119
1192
  private BluetoothGattCharacteristic findCharacteristic(BluetoothGatt gatt, String serviceUuid, String characteristicUuid) {
1120
1193
  BluetoothGattService service = gatt.getService(UUID.fromString(normalizeUuid(serviceUuid)));
1121
1194
  if (service == null) {
package/dist/docs.json CHANGED
@@ -1231,6 +1231,22 @@
1231
1231
  "docs": "The mode to initialize the plugin in.\n- 'central': Act as a BLE central (client)\n- 'peripheral': Act as a BLE peripheral (server)",
1232
1232
  "complexTypes": [],
1233
1233
  "type": "'central' | 'peripheral' | undefined"
1234
+ },
1235
+ {
1236
+ "name": "showPowerAlert",
1237
+ "tags": [
1238
+ {
1239
+ "text": "true",
1240
+ "name": "default"
1241
+ },
1242
+ {
1243
+ "text": "8.0.7",
1244
+ "name": "since"
1245
+ }
1246
+ ],
1247
+ "docs": "Whether iOS should show the system Bluetooth power alert when Bluetooth is off (iOS only).",
1248
+ "complexTypes": [],
1249
+ "type": "boolean | undefined"
1234
1250
  }
1235
1251
  ]
1236
1252
  },
@@ -2560,7 +2576,7 @@
2560
2576
  "name": "since"
2561
2577
  }
2562
2578
  ],
2563
- "docs": "The device name to advertise.",
2579
+ "docs": "The device name to advertise.\n\nOn Android, advertising the local name is implemented by temporarily setting the Bluetooth adapter name\nwhile advertising, then restoring the previous adapter name when advertising is stopped.",
2564
2580
  "complexTypes": [],
2565
2581
  "type": "string | undefined"
2566
2582
  },
@@ -491,6 +491,13 @@ export interface InitializeOptions {
491
491
  * @since 1.0.0
492
492
  */
493
493
  mode?: 'central' | 'peripheral';
494
+ /**
495
+ * Whether iOS should show the system Bluetooth power alert when Bluetooth is off (iOS only).
496
+ *
497
+ * @default true
498
+ * @since 8.0.7
499
+ */
500
+ showPowerAlert?: boolean;
494
501
  }
495
502
  /**
496
503
  * Result of the isAvailable method.
@@ -1151,6 +1158,9 @@ export interface StartAdvertisingOptions {
1151
1158
  /**
1152
1159
  * The device name to advertise.
1153
1160
  *
1161
+ * On Android, advertising the local name is implemented by temporarily setting the Bluetooth adapter name
1162
+ * while advertising, then restoring the previous adapter name when advertising is stopped.
1163
+ *
1154
1164
  * @since 1.0.0
1155
1165
  */
1156
1166
  name?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"AA84CA;;;;GAIG;AACH,MAAM,OAA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AC,QAAQ,CAAC,EAAE,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE,CAAC,CAAC;IACpE,CAAC;IAED;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,iBAAiB,CAAC,GAAW;QAClC,MAAM,KAAK,GAAa,EAAE,CAAC;QAC3B,KAAK,IAAI,CAAC,GAAG,CAAC,EAAE,CAAC,GAAG,GAAG,CAAC,MAAM,EAAE,CAAC,IAAI,CAAC,EAAE,CAAC;YACvC,KAAK,CAAC,IAAI,CAAC,QAAQ,CAAC,GAAG,CAAC,MAAM,CAAC,CAAC,EAAE,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC;QAC7C,CAAC;QACD,OAAO,KAAK,CAAC;IACf,CAAC;IAED;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,oBAAoB,CAAC,GAAW;QACrC,OAAO,KAAK,CAAC,IAAI,CAAC,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC;IACnD,CAAC;IAED;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,oBAAoB,CAAC,KAAe;QACzC,OAAO,IAAI,WAAW,EAAE,CAAC,MAAM,CAAC,IAAI,UAAU,CAAC,KAAK,CAAC,CAAC,CAAC;IACzD,CAAC;CACF"}
@@ -31,13 +31,19 @@ public class BluetoothLowEnergy: NSObject {
31
31
  super.init()
32
32
  }
33
33
 
34
- func initialize(mode: String, completion: @escaping (Error?) -> Void) {
34
+ func initialize(mode: String, showPowerAlert: Bool, completion: @escaping (Error?) -> Void) {
35
35
  self.mode = mode
36
36
 
37
37
  if mode == "peripheral" {
38
- peripheralManager = CBPeripheralManager(delegate: self, queue: nil)
38
+ let options: [String: Any] = [
39
+ CBPeripheralManagerOptionShowPowerAlertKey: showPowerAlert
40
+ ]
41
+ peripheralManager = CBPeripheralManager(delegate: self, queue: nil, options: options)
39
42
  } else {
40
- centralManager = CBCentralManager(delegate: self, queue: nil)
43
+ let options: [String: Any] = [
44
+ CBCentralManagerOptionShowPowerAlertKey: showPowerAlert
45
+ ]
46
+ centralManager = CBCentralManager(delegate: self, queue: nil, options: options)
41
47
  }
42
48
 
43
49
  // Wait for the manager to be ready
@@ -414,9 +420,10 @@ extension BluetoothLowEnergy: CBCentralManagerDelegate {
414
420
  let deviceId = peripheral.identifier.uuidString
415
421
  discoveredPeripherals[deviceId] = peripheral
416
422
 
423
+ let name = (advertisementData[CBAdvertisementDataLocalNameKey] as? String) ?? peripheral.name
417
424
  var device: [String: Any] = [
418
425
  "deviceId": deviceId,
419
- "name": peripheral.name as Any,
426
+ "name": name as Any,
420
427
  "rssi": RSSI.intValue
421
428
  ]
422
429
 
@@ -5,7 +5,7 @@ import CoreBluetooth
5
5
  // swiftlint:disable type_body_length file_length
6
6
  @objc(BluetoothLowEnergyPlugin)
7
7
  public class BluetoothLowEnergyPlugin: CAPPlugin, CAPBridgedPlugin {
8
- private let pluginVersion: String = "8.0.6"
8
+ private let pluginVersion: String = "8.1.1"
9
9
  public let identifier = "BluetoothLowEnergyPlugin"
10
10
  public let jsName = "BluetoothLowEnergy"
11
11
  public let pluginMethods: [CAPPluginMethod] = [
@@ -51,7 +51,8 @@ public class BluetoothLowEnergyPlugin: CAPPlugin, CAPBridgedPlugin {
51
51
 
52
52
  @objc func initialize(_ call: CAPPluginCall) {
53
53
  let mode = call.getString("mode") ?? "central"
54
- implementation?.initialize(mode: mode) { error in
54
+ let showPowerAlert = call.getBool("showPowerAlert") ?? true
55
+ implementation?.initialize(mode: mode, showPowerAlert: showPowerAlert) { error in
55
56
  if let error = error {
56
57
  call.reject(error.localizedDescription)
57
58
  } else {
@@ -389,7 +390,8 @@ public class BluetoothLowEnergyPlugin: CAPPlugin, CAPBridgedPlugin {
389
390
  }
390
391
 
391
392
  @objc func startAdvertising(_ call: CAPPluginCall) {
392
- let name = call.getString("name")
393
+ let includeName = call.getBool("includeName") ?? true
394
+ let name = includeName ? call.getString("name") : nil
393
395
  var serviceUUIDs: [CBUUID]?
394
396
  if let services = call.getArray("services", String.self) {
395
397
  serviceUUIDs = services.map { CBUUID(string: $0) }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@capgo/capacitor-bluetooth-low-energy",
3
- "version": "8.0.6",
3
+ "version": "8.1.1",
4
4
  "description": "Bluetooth Low Energy (BLE) plugin for Capacitor with support for scanning, connecting, reading, writing, and notifications.",
5
5
  "main": "dist/plugin.cjs.js",
6
6
  "module": "dist/esm/index.js",