@capgo/capacitor-bluetooth-low-energy 8.1.0 → 8.1.2

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
@@ -1117,12 +1117,12 @@ Options for requesting connection priority.
1117
1117
 
1118
1118
  Options for starting advertising.
1119
1119
 
1120
- | Prop | Type | Description | Default | Since |
1121
- | ------------------------- | --------------------- | -------------------------------------------------------- | ------------------ | ----- |
1122
- | **`name`** | <code>string</code> | The device name to advertise. | | 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 |
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 |
1126
1126
 
1127
1127
 
1128
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.1.0";
78
+ private final String pluginVersion = "8.1.2";
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
@@ -2576,7 +2576,7 @@
2576
2576
  "name": "since"
2577
2577
  }
2578
2578
  ],
2579
- "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.",
2580
2580
  "complexTypes": [],
2581
2581
  "type": "string | undefined"
2582
2582
  },
@@ -1158,6 +1158,9 @@ export interface StartAdvertisingOptions {
1158
1158
  /**
1159
1159
  * The device name to advertise.
1160
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
+ *
1161
1164
  * @since 1.0.0
1162
1165
  */
1163
1166
  name?: string;
@@ -1 +1 @@
1
- {"version":3,"file":"definitions.js","sourceRoot":"","sources":["../../src/definitions.ts"],"names":[],"mappings":"AAs5CA;;;;GAIG;AACH,MAAM,OAAO,uBAAuB;IAClC;;;;;;;;;;;OAWG;IACH,MAAM,CAAC,iBAAiB,CAAC,KAAe;QACtC,OAAO,KA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AI,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"}
@@ -420,9 +420,10 @@ extension BluetoothLowEnergy: CBCentralManagerDelegate {
420
420
  let deviceId = peripheral.identifier.uuidString
421
421
  discoveredPeripherals[deviceId] = peripheral
422
422
 
423
+ let name = (advertisementData[CBAdvertisementDataLocalNameKey] as? String) ?? peripheral.name
423
424
  var device: [String: Any] = [
424
425
  "deviceId": deviceId,
425
- "name": peripheral.name as Any,
426
+ "name": name as Any,
426
427
  "rssi": RSSI.intValue
427
428
  ]
428
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.1.0"
8
+ private let pluginVersion: String = "8.1.2"
9
9
  public let identifier = "BluetoothLowEnergyPlugin"
10
10
  public let jsName = "BluetoothLowEnergy"
11
11
  public let pluginMethods: [CAPPluginMethod] = [
@@ -390,7 +390,8 @@ public class BluetoothLowEnergyPlugin: CAPPlugin, CAPBridgedPlugin {
390
390
  }
391
391
 
392
392
  @objc func startAdvertising(_ call: CAPPluginCall) {
393
- let name = call.getString("name")
393
+ let includeName = call.getBool("includeName") ?? true
394
+ let name = includeName ? call.getString("name") : nil
394
395
  var serviceUUIDs: [CBUUID]?
395
396
  if let services = call.getArray("services", String.self) {
396
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.1.0",
3
+ "version": "8.1.2",
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",