@capgo/capacitor-bluetooth-low-energy 8.1.6 → 8.2.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.
@@ -7,6 +7,8 @@ import android.bluetooth.BluetoothGatt;
7
7
  import android.bluetooth.BluetoothGattCallback;
8
8
  import android.bluetooth.BluetoothGattCharacteristic;
9
9
  import android.bluetooth.BluetoothGattDescriptor;
10
+ import android.bluetooth.BluetoothGattServer;
11
+ import android.bluetooth.BluetoothGattServerCallback;
10
12
  import android.bluetooth.BluetoothGattService;
11
13
  import android.bluetooth.BluetoothManager;
12
14
  import android.bluetooth.BluetoothProfile;
@@ -75,11 +77,15 @@ import java.util.UUID;
75
77
  )
76
78
  public class BluetoothLowEnergyPlugin extends Plugin {
77
79
 
78
- private final String pluginVersion = "8.1.6";
80
+ private final String pluginVersion = "8.2.0";
79
81
  private BluetoothManager bluetoothManager;
80
82
  private BluetoothAdapter bluetoothAdapter;
81
83
  private BluetoothLeScanner bluetoothLeScanner;
82
84
  private BluetoothLeAdvertiser bluetoothLeAdvertiser;
85
+ private BluetoothGattServer gattServer;
86
+ private final Map<String, BluetoothGattCharacteristic> localGattCharacteristics = new HashMap<>();
87
+ private final Map<String, BluetoothGattService> localGattServices = new HashMap<>();
88
+ private final Map<String, BluetoothDevice> connectedCentrals = new HashMap<>();
83
89
 
84
90
  private final Map<String, BluetoothDevice> discoveredDevices = new HashMap<>();
85
91
  private final Map<String, BluetoothGatt> connectedGatts = new HashMap<>();
@@ -972,6 +978,217 @@ public class BluetoothLowEnergyPlugin extends Plugin {
972
978
  call.resolve();
973
979
  }
974
980
 
981
+
982
+ @PluginMethod
983
+ public void addGattService(PluginCall call) {
984
+ if (!"peripheral".equals(mode)) {
985
+ call.reject("GATT server is only available in peripheral mode");
986
+ return;
987
+ }
988
+
989
+ String serviceUuid = call.getString("service");
990
+ if (serviceUuid == null) {
991
+ call.reject("Service UUID is required");
992
+ return;
993
+ }
994
+
995
+ JSArray characteristicsArray = call.getArray("characteristics");
996
+ if (characteristicsArray == null) {
997
+ call.reject("Characteristics array is required");
998
+ return;
999
+ }
1000
+
1001
+ try {
1002
+ ensureGattServer();
1003
+ if (gattServer == null) {
1004
+ call.reject("Failed to open GATT server");
1005
+ return;
1006
+ }
1007
+
1008
+ String normalizedServiceUuid = normalizeUuid(serviceUuid);
1009
+ if (localGattServices.containsKey(normalizedServiceUuid)) {
1010
+ call.reject("Service already exists");
1011
+ return;
1012
+ }
1013
+
1014
+ BluetoothGattService service = new BluetoothGattService(
1015
+ UUID.fromString(normalizedServiceUuid),
1016
+ BluetoothGattService.SERVICE_TYPE_PRIMARY
1017
+ );
1018
+
1019
+ for (int i = 0; i < characteristicsArray.length(); i++) {
1020
+ JSObject characteristicObj = JSObject.fromJSONObject(characteristicsArray.getJSONObject(i));
1021
+ if (characteristicObj == null) {
1022
+ continue;
1023
+ }
1024
+
1025
+ String characteristicUuid = characteristicObj.getString("uuid");
1026
+ if (characteristicUuid == null) {
1027
+ call.reject("Characteristic UUID is required");
1028
+ return;
1029
+ }
1030
+
1031
+ JSObject propertiesObj = characteristicObj.getJSObject("properties");
1032
+ int properties = parseCharacteristicProperties(propertiesObj);
1033
+ int permissions = parseCharacteristicPermissions(propertiesObj);
1034
+
1035
+ BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(
1036
+ UUID.fromString(normalizeUuid(characteristicUuid)),
1037
+ properties,
1038
+ permissions
1039
+ );
1040
+
1041
+ JSArray valueArray = characteristicObj.has("value") ? JSArray.from(characteristicObj.getJSONArray("value")) : null;
1042
+ if (valueArray != null) {
1043
+ characteristic.setValue(jsArrayToBytes(valueArray));
1044
+ }
1045
+
1046
+ if (propertiesObj != null && (propertiesObj.getBoolean("notify", false) || propertiesObj.getBoolean("indicate", false))) {
1047
+ BluetoothGattDescriptor ccc = new BluetoothGattDescriptor(
1048
+ CLIENT_CHARACTERISTIC_CONFIG,
1049
+ BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE
1050
+ );
1051
+ characteristic.addDescriptor(ccc);
1052
+ }
1053
+
1054
+ JSArray descriptorsArray = characteristicObj.has("descriptors") ? JSArray.from(characteristicObj.getJSONArray("descriptors")) : null;
1055
+ if (descriptorsArray != null) {
1056
+ for (int j = 0; j < descriptorsArray.length(); j++) {
1057
+ JSObject descriptorObj = JSObject.fromJSONObject(descriptorsArray.getJSONObject(j));
1058
+ if (descriptorObj == null) {
1059
+ continue;
1060
+ }
1061
+ String descriptorUuid = descriptorObj.getString("uuid");
1062
+ if (descriptorUuid == null) {
1063
+ continue;
1064
+ }
1065
+ BluetoothGattDescriptor descriptor = new BluetoothGattDescriptor(
1066
+ UUID.fromString(normalizeUuid(descriptorUuid)),
1067
+ BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE
1068
+ );
1069
+ JSArray descriptorValue = descriptorObj.has("value") ? JSArray.from(descriptorObj.getJSONArray("value")) : null;
1070
+ if (descriptorValue != null) {
1071
+ descriptor.setValue(jsArrayToBytes(descriptorValue));
1072
+ }
1073
+ characteristic.addDescriptor(descriptor);
1074
+ }
1075
+ }
1076
+
1077
+ service.addCharacteristic(characteristic);
1078
+ localGattCharacteristics.put(normalizedServiceUuid + "/" + normalizeUuid(characteristicUuid), characteristic);
1079
+ }
1080
+
1081
+ boolean added = gattServer.addService(service);
1082
+ if (!added) {
1083
+ call.reject("Failed to add GATT service");
1084
+ return;
1085
+ }
1086
+
1087
+ localGattServices.put(normalizedServiceUuid, service);
1088
+ call.resolve();
1089
+ } catch (Exception e) {
1090
+ call.reject("Failed to add GATT service: " + e.getMessage());
1091
+ }
1092
+ }
1093
+
1094
+ @PluginMethod
1095
+ public void removeGattService(PluginCall call) {
1096
+ String serviceUuid = call.getString("service");
1097
+ if (serviceUuid == null) {
1098
+ call.reject("Service UUID is required");
1099
+ return;
1100
+ }
1101
+
1102
+ if (gattServer == null) {
1103
+ call.reject("GATT server is not initialized");
1104
+ return;
1105
+ }
1106
+
1107
+ String normalizedServiceUuid = normalizeUuid(serviceUuid);
1108
+ BluetoothGattService service = localGattServices.remove(normalizedServiceUuid);
1109
+ if (service == null) {
1110
+ call.reject("Service not found");
1111
+ return;
1112
+ }
1113
+
1114
+ localGattCharacteristics.entrySet().removeIf(entry -> entry.getKey().startsWith(normalizedServiceUuid + "/"));
1115
+ gattServer.removeService(service);
1116
+ call.resolve();
1117
+ }
1118
+
1119
+ @PluginMethod
1120
+ public void setGattCharacteristicValue(PluginCall call) {
1121
+ String serviceUuid = call.getString("service");
1122
+ String characteristicUuid = call.getString("characteristic");
1123
+ JSArray valueArray = call.getArray("value");
1124
+
1125
+ if (serviceUuid == null || characteristicUuid == null || valueArray == null) {
1126
+ call.reject("Service, characteristic, and value are required");
1127
+ return;
1128
+ }
1129
+
1130
+ BluetoothGattCharacteristic characteristic = localGattCharacteristics.get(
1131
+ normalizeUuid(serviceUuid) + "/" + normalizeUuid(characteristicUuid)
1132
+ );
1133
+ if (characteristic == null) {
1134
+ call.reject("Characteristic not found");
1135
+ return;
1136
+ }
1137
+
1138
+ try {
1139
+ characteristic.setValue(jsArrayToBytes(valueArray));
1140
+ call.resolve();
1141
+ } catch (Exception e) {
1142
+ call.reject("Failed to set characteristic value: " + e.getMessage());
1143
+ }
1144
+ }
1145
+
1146
+ @PluginMethod
1147
+ public void notifyGattCharacteristicChanged(PluginCall call) {
1148
+ String serviceUuid = call.getString("service");
1149
+ String characteristicUuid = call.getString("characteristic");
1150
+ String deviceId = call.getString("deviceId");
1151
+ JSArray valueArray = call.getArray("value");
1152
+
1153
+ if (serviceUuid == null || characteristicUuid == null || valueArray == null) {
1154
+ call.reject("Service, characteristic, and value are required");
1155
+ return;
1156
+ }
1157
+
1158
+ if (gattServer == null) {
1159
+ call.reject("GATT server is not initialized");
1160
+ return;
1161
+ }
1162
+
1163
+ BluetoothGattCharacteristic characteristic = localGattCharacteristics.get(
1164
+ normalizeUuid(serviceUuid) + "/" + normalizeUuid(characteristicUuid)
1165
+ );
1166
+ if (characteristic == null) {
1167
+ call.reject("Characteristic not found");
1168
+ return;
1169
+ }
1170
+
1171
+ try {
1172
+ characteristic.setValue(jsArrayToBytes(valueArray));
1173
+ boolean indicate = (characteristic.getProperties() & BluetoothGattCharacteristic.PROPERTY_INDICATE) != 0;
1174
+ if (deviceId != null) {
1175
+ BluetoothDevice device = connectedCentrals.get(deviceId);
1176
+ if (device == null) {
1177
+ call.reject("Central not connected");
1178
+ return;
1179
+ }
1180
+ gattServer.notifyCharacteristicChanged(device, characteristic, indicate);
1181
+ } else {
1182
+ for (BluetoothDevice device : connectedCentrals.values()) {
1183
+ gattServer.notifyCharacteristicChanged(device, characteristic, indicate);
1184
+ }
1185
+ }
1186
+ call.resolve();
1187
+ } catch (Exception e) {
1188
+ call.reject("Failed to notify characteristic change: " + e.getMessage());
1189
+ }
1190
+ }
1191
+
975
1192
  @PluginMethod
976
1193
  public void startForegroundService(PluginCall call) {
977
1194
  // Foreground service implementation would go here
@@ -1230,6 +1447,154 @@ public class BluetoothLowEnergyPlugin extends Plugin {
1230
1447
  return bytes;
1231
1448
  }
1232
1449
 
1450
+
1451
+ private void ensureGattServer() {
1452
+ if (gattServer == null && bluetoothManager != null) {
1453
+ try {
1454
+ gattServer = bluetoothManager.openGattServer(getContext(), gattServerCallback);
1455
+ } catch (SecurityException e) {
1456
+ // Ignore
1457
+ }
1458
+ }
1459
+ }
1460
+
1461
+ private int parseCharacteristicProperties(JSObject propertiesObj) {
1462
+ int properties = 0;
1463
+ if (propertiesObj == null) {
1464
+ return properties;
1465
+ }
1466
+ if (propertiesObj.getBoolean("broadcast", false)) {
1467
+ properties |= BluetoothGattCharacteristic.PROPERTY_BROADCAST;
1468
+ }
1469
+ if (propertiesObj.getBoolean("read", false)) {
1470
+ properties |= BluetoothGattCharacteristic.PROPERTY_READ;
1471
+ }
1472
+ if (propertiesObj.getBoolean("writeWithoutResponse", false)) {
1473
+ properties |= BluetoothGattCharacteristic.PROPERTY_WRITE_NO_RESPONSE;
1474
+ }
1475
+ if (propertiesObj.getBoolean("write", false)) {
1476
+ properties |= BluetoothGattCharacteristic.PROPERTY_WRITE;
1477
+ }
1478
+ if (propertiesObj.getBoolean("notify", false)) {
1479
+ properties |= BluetoothGattCharacteristic.PROPERTY_NOTIFY;
1480
+ }
1481
+ if (propertiesObj.getBoolean("indicate", false)) {
1482
+ properties |= BluetoothGattCharacteristic.PROPERTY_INDICATE;
1483
+ }
1484
+ if (propertiesObj.getBoolean("authenticatedSignedWrites", false)) {
1485
+ properties |= BluetoothGattCharacteristic.PROPERTY_SIGNED_WRITE;
1486
+ }
1487
+ if (propertiesObj.getBoolean("extendedProperties", false)) {
1488
+ properties |= BluetoothGattCharacteristic.PROPERTY_EXTENDED_PROPS;
1489
+ }
1490
+ return properties;
1491
+ }
1492
+
1493
+ private int parseCharacteristicPermissions(JSObject propertiesObj) {
1494
+ int permissions = 0;
1495
+ if (propertiesObj == null) {
1496
+ return permissions;
1497
+ }
1498
+ if (propertiesObj.getBoolean("read", false)) {
1499
+ permissions |= BluetoothGattCharacteristic.PERMISSION_READ;
1500
+ }
1501
+ if (propertiesObj.getBoolean("write", false) || propertiesObj.getBoolean("writeWithoutResponse", false)) {
1502
+ permissions |= BluetoothGattCharacteristic.PERMISSION_WRITE;
1503
+ }
1504
+ return permissions;
1505
+ }
1506
+
1507
+ private String characteristicKey(BluetoothGattCharacteristic characteristic) {
1508
+ for (Map.Entry<String, BluetoothGattCharacteristic> entry : localGattCharacteristics.entrySet()) {
1509
+ if (entry.getValue().equals(characteristic)) {
1510
+ return entry.getKey();
1511
+ }
1512
+ }
1513
+ return null;
1514
+ }
1515
+
1516
+ private final BluetoothGattServerCallback gattServerCallback = new BluetoothGattServerCallback() {
1517
+ @Override
1518
+ public void onConnectionStateChange(BluetoothDevice device, int status, int newState) {
1519
+ String deviceId = device.getAddress();
1520
+ if (newState == BluetoothProfile.STATE_CONNECTED) {
1521
+ connectedCentrals.put(deviceId, device);
1522
+ JSObject event = new JSObject();
1523
+ event.put("deviceId", deviceId);
1524
+ notifyListeners("centralConnected", event);
1525
+ } else if (newState == BluetoothProfile.STATE_DISCONNECTED) {
1526
+ connectedCentrals.remove(deviceId);
1527
+ JSObject event = new JSObject();
1528
+ event.put("deviceId", deviceId);
1529
+ notifyListeners("centralDisconnected", event);
1530
+ }
1531
+ }
1532
+
1533
+ @Override
1534
+ public void onCharacteristicReadRequest(BluetoothDevice device, int requestId, int offset, BluetoothGattCharacteristic characteristic) {
1535
+ String key = characteristicKey(characteristic);
1536
+ if (key != null) {
1537
+ String[] parts = key.split("/", 2);
1538
+ JSObject event = new JSObject();
1539
+ event.put("deviceId", device.getAddress());
1540
+ event.put("service", parts[0]);
1541
+ event.put("characteristic", parts[1]);
1542
+ notifyListeners("gattCharacteristicReadRequest", event);
1543
+ }
1544
+
1545
+ if (gattServer != null) {
1546
+ byte[] value = characteristic.getValue();
1547
+ if (value == null) {
1548
+ value = new byte[0];
1549
+ }
1550
+ gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
1551
+ }
1552
+ }
1553
+
1554
+ @Override
1555
+ public void onCharacteristicWriteRequest(
1556
+ BluetoothDevice device,
1557
+ int requestId,
1558
+ BluetoothGattCharacteristic characteristic,
1559
+ boolean preparedWrite,
1560
+ boolean responseNeeded,
1561
+ int offset,
1562
+ byte[] value
1563
+ ) {
1564
+ characteristic.setValue(value);
1565
+ String key = characteristicKey(characteristic);
1566
+ if (key != null) {
1567
+ String[] parts = key.split("/", 2);
1568
+ JSObject event = new JSObject();
1569
+ event.put("deviceId", device.getAddress());
1570
+ event.put("service", parts[0]);
1571
+ event.put("characteristic", parts[1]);
1572
+ event.put("value", bytesToJsArray(value));
1573
+ notifyListeners("gattCharacteristicWriteRequest", event);
1574
+ }
1575
+
1576
+ if (responseNeeded && gattServer != null) {
1577
+ gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
1578
+ }
1579
+ }
1580
+
1581
+ @Override
1582
+ public void onDescriptorWriteRequest(
1583
+ BluetoothDevice device,
1584
+ int requestId,
1585
+ BluetoothGattDescriptor descriptor,
1586
+ boolean preparedWrite,
1587
+ boolean responseNeeded,
1588
+ int offset,
1589
+ byte[] value
1590
+ ) {
1591
+ descriptor.setValue(value);
1592
+ if (responseNeeded && gattServer != null) {
1593
+ gattServer.sendResponse(device, requestId, BluetoothGatt.GATT_SUCCESS, offset, value);
1594
+ }
1595
+ }
1596
+ };
1597
+
1233
1598
  private JSArray bytesToJsArray(byte[] bytes) {
1234
1599
  JSArray array = new JSArray();
1235
1600
  if (bytes != null) {