react-native-ble-manager 12.4.5 → 12.5.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.
@@ -9,7 +9,8 @@ buildscript {
9
9
  }
10
10
 
11
11
  dependencies {
12
- classpath "com.android.tools.build:gradle:7.2.1"
12
+ classpath "com.android.tools.build:gradle:8.6.1"
13
+ classpath "com.facebook.react:react-native-gradle-plugin"
13
14
  // noinspection DifferentKotlinGradleVersion
14
15
  classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
15
16
  }
@@ -28,10 +29,7 @@ def isNewArchitectureEnabled() {
28
29
 
29
30
  apply plugin: 'com.android.library'
30
31
  apply plugin: "kotlin-android"
31
-
32
- if (isNewArchitectureEnabled()) {
33
- apply plugin: 'com.facebook.react'
34
- }
32
+ apply plugin: 'com.facebook.react'
35
33
 
36
34
  def safeExtGet(prop, fallback) {
37
35
  rootProject.ext.has(prop) ? rootProject.ext.get(prop) : fallback
@@ -116,17 +114,12 @@ repositories {
116
114
  def kotlin_version = getExtOrDefault("kotlinVersion")
117
115
 
118
116
  dependencies {
119
- // For < 0.71, this will be from the local maven repo
120
- // For > 0.71, this will be replaced by `com.facebook.react:react-android:$version` by react gradle plugin
121
- //noinspection GradleDynamicVersion
122
- implementation "com.facebook.react:react-native:+"
117
+ implementation "com.facebook.react:react-android:0.81.5"
123
118
  implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
124
119
  }
125
120
 
126
- if (isNewArchitectureEnabled()) {
127
- react {
128
- jsRootDir = file("../src/")
129
- libraryName = "BleManager"
130
- codegenJavaPackageName = "it.innove"
131
- }
121
+ react {
122
+ jsRootDir = file("../src/")
123
+ libraryName = "BleManager"
124
+ codegenJavaPackageName = "it.innove"
132
125
  }
@@ -1,5 +1,5 @@
1
- kotlinVersion=1.7.0
1
+ kotlinVersion=1.9.24
2
2
  android.useAndroidX=true
3
3
  android.enableJetifier=true
4
4
  # https://github.com/facebook/react-native/issues/35168
5
- org.gradle.jvmargs=-Xmx4096M
5
+ org.gradle.jvmargs=-Xmx4096M
@@ -1,2 +1,3 @@
1
1
  rootProject.name = 'react-native-ble-manager'
2
2
 
3
+ includeBuild('../node_modules/@react-native/gradle-plugin')
@@ -119,10 +119,25 @@ public class Peripheral extends BluetoothGattCallback {
119
119
 
120
120
  public void connect(final Callback callback, Activity activity, ReadableMap options) {
121
121
  mainHandler.post(() -> {
122
- if (!connected) {
123
- BluetoothDevice device = getDevice();
124
- this.connectCallbacks.addLast(callback);
125
- this.connecting = true;
122
+ if (connected) {
123
+ if (gatt != null) {
124
+ callback.invoke();
125
+ } else {
126
+ callback.invoke("BluetoothGatt is null");
127
+ }
128
+ return;
129
+ }
130
+
131
+ this.connectCallbacks.addLast(callback);
132
+
133
+ if (connecting) {
134
+ return;
135
+ }
136
+
137
+ BluetoothDevice device = getDevice();
138
+ this.connecting = true;
139
+
140
+ try {
126
141
  if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
127
142
  Log.d(BleManager.LOG_TAG, " Is Or Greater than M $mBluetoothDevice");
128
143
  boolean autoconnect = false;
@@ -150,12 +165,19 @@ public class Peripheral extends BluetoothGattCallback {
150
165
  gatt = device.connectGatt(activity, false, this);
151
166
  }
152
167
  }
153
- } else {
154
- if (gatt != null) {
155
- callback.invoke();
156
- } else {
157
- callback.invoke("BluetoothGatt is null");
168
+
169
+ // connectGatt() may return null if the adapter is not ready
170
+ if (gatt == null) {
171
+ throw new IllegalStateException("connectGatt returned null");
172
+ }
173
+ } catch (Exception e) {
174
+ Log.e(BleManager.LOG_TAG, "[ON CONNECT][NATIVE] connectGatt failed for " + device.getAddress(), e);
175
+ this.connecting = false;
176
+ this.gatt = null;
177
+ for (Callback connectCallback : connectCallbacks) {
178
+ connectCallback.invoke("Connection failed to start: " + e.getMessage());
158
179
  }
180
+ connectCallbacks.clear();
159
181
  }
160
182
  });
161
183
  }
@@ -163,6 +185,7 @@ public class Peripheral extends BluetoothGattCallback {
163
185
 
164
186
  public void disconnect(final Callback callback, final boolean force) {
165
187
  connected = false;
188
+ connecting = false;
166
189
  mainHandler.post(() -> {
167
190
  errorAndClearAllCallbacks("Disconnect called before the command completed");
168
191
  resetQueuesAndBuffers();
@@ -384,6 +407,7 @@ public class Peripheral extends BluetoothGattCallback {
384
407
  commandQueue.clear();
385
408
  commandQueueBusy = false;
386
409
  connected = false;
410
+ connecting = false;
387
411
  clearBuffers();
388
412
  }
389
413
 
@@ -1100,33 +1124,33 @@ public class Peripheral extends BluetoothGattCallback {
1100
1124
  }
1101
1125
  return enqueue(() -> {
1102
1126
  try {
1103
- if (!isConnected() || gatt == null) {
1104
- if (withResponse && callback != null) {
1105
- if (writeCallbacks.removeLastOccurrence(callback)) {
1106
- try {
1107
- callback.invoke("Device is not connected", null);
1108
- } catch (Exception callbackException) {
1109
- Log.e(BleManager.LOG_TAG, "Error invoking write callback for disconnected device", callbackException);
1110
- }
1111
- }
1112
- }
1113
- completedCommand();
1114
- return;
1115
- }
1116
- doWrite(characteristic, copyOfData);
1117
- } catch (Exception e) {
1118
- Log.e(BleManager.LOG_TAG, "Error in enqueueWrite lambda", e);
1119
- if (withResponse && callback != null) {
1120
- if (writeCallbacks.removeLastOccurrence(callback)) {
1121
- try {
1122
- callback.invoke("Write failed: " + e.getMessage(), null);
1123
- } catch (Exception callbackException) {
1124
- Log.e(BleManager.LOG_TAG, "Error invoking write callback", callbackException);
1125
- }
1126
- }
1127
- }
1128
- completedCommand();
1129
- }
1127
+ if (!isConnected() || gatt == null) {
1128
+ if (withResponse && callback != null) {
1129
+ if (writeCallbacks.removeLastOccurrence(callback)) {
1130
+ try {
1131
+ callback.invoke("Device is not connected", null);
1132
+ } catch (Exception callbackException) {
1133
+ Log.e(BleManager.LOG_TAG, "Error invoking write callback for disconnected device", callbackException);
1134
+ }
1135
+ }
1136
+ }
1137
+ completedCommand();
1138
+ return;
1139
+ }
1140
+ doWrite(characteristic, copyOfData);
1141
+ } catch (Exception e) {
1142
+ Log.e(BleManager.LOG_TAG, "Error in enqueueWrite lambda", e);
1143
+ if (withResponse && callback != null) {
1144
+ if (writeCallbacks.removeLastOccurrence(callback)) {
1145
+ try {
1146
+ callback.invoke("Write failed: " + e.getMessage(), null);
1147
+ } catch (Exception callbackException) {
1148
+ Log.e(BleManager.LOG_TAG, "Error invoking write callback", callbackException);
1149
+ }
1150
+ }
1151
+ }
1152
+ completedCommand();
1153
+ }
1130
1154
  });
1131
1155
  }
1132
1156
 
@@ -32,6 +32,15 @@ public class SwiftBleManager: NSObject, CBCentralManagerDelegate,
32
32
 
33
33
  private let serialQueue = DispatchQueue(label: "BleManager.serialQueue")
34
34
 
35
+ // Structured error payload so JS can detect specific failures by (domain, code).
36
+ private static func errorPayload(_ error: Error) -> [String: Any] {
37
+ return [
38
+ "code": error._code,
39
+ "domain": error._domain,
40
+ "message": error.localizedDescription,
41
+ ]
42
+ }
43
+
35
44
  private var exactAdvertisingName: [String]
36
45
 
37
46
  static var verboseLogging = false
@@ -1241,14 +1250,17 @@ public class SwiftBleManager: NSObject, CBCentralManagerDelegate,
1241
1250
  didFailToConnect peripheral: CBPeripheral,
1242
1251
  error: Error?
1243
1252
  ) {
1244
- let errorStr =
1253
+ NSLog(
1245
1254
  "Peripheral connection failure: \(peripheral.uuidAsString() ) (\(error?.localizedDescription ?? "")"
1246
- NSLog(errorStr)
1255
+ )
1247
1256
 
1257
+ let errorParam: Any =
1258
+ error.map(SwiftBleManager.errorPayload)
1259
+ ?? "Peripheral connection failure: \(peripheral.uuidAsString())"
1248
1260
  invokeAndClearDictionary(
1249
1261
  &connectCallbacks,
1250
1262
  withKey: peripheral.uuidAsString(),
1251
- usingParameters: [errorStr]
1263
+ usingParameters: [errorParam]
1252
1264
  )
1253
1265
  }
1254
1266
 
@@ -1460,7 +1472,7 @@ public class SwiftBleManager: NSObject, CBCentralManagerDelegate,
1460
1472
  invokeAndClearDictionary(
1461
1473
  &retrieveServicesCallbacks,
1462
1474
  withKey: peripheral.uuidAsString(),
1463
- usingParameters: [error.localizedDescription]
1475
+ usingParameters: [SwiftBleManager.errorPayload(error)]
1464
1476
  )
1465
1477
  return
1466
1478
  }
@@ -1612,7 +1624,7 @@ public class SwiftBleManager: NSObject, CBCentralManagerDelegate,
1612
1624
  invokeAndClearDictionary(
1613
1625
  &readRSSICallbacks,
1614
1626
  withKey: peripheral.uuidAsString(),
1615
- usingParameters: [error.localizedDescription, RSSI]
1627
+ usingParameters: [SwiftBleManager.errorPayload(error), RSSI]
1616
1628
  )
1617
1629
  } else {
1618
1630
  invokeAndClearDictionary(
@@ -1641,7 +1653,7 @@ public class SwiftBleManager: NSObject, CBCentralManagerDelegate,
1641
1653
  invokeAndClearDictionary(
1642
1654
  &readDescriptorCallbacks,
1643
1655
  withKey: key,
1644
- usingParameters: [error.localizedDescription, NSNull()]
1656
+ usingParameters: [SwiftBleManager.errorPayload(error), NSNull()]
1645
1657
  )
1646
1658
  return
1647
1659
  }
@@ -1723,7 +1735,7 @@ public class SwiftBleManager: NSObject, CBCentralManagerDelegate,
1723
1735
  invokeAndClearDictionary(
1724
1736
  &readCallbacks,
1725
1737
  withKey: key,
1726
- usingParameters: [error.localizedDescription, NSNull()]
1738
+ usingParameters: [SwiftBleManager.errorPayload(error), NSNull()]
1727
1739
  )
1728
1740
  return
1729
1741
  }
@@ -1880,7 +1892,7 @@ public class SwiftBleManager: NSObject, CBCentralManagerDelegate,
1880
1892
  invokeAndClearDictionary(
1881
1893
  &writeDescriptorCallbacks,
1882
1894
  withKey: key,
1883
- usingParameters: [error.localizedDescription]
1895
+ usingParameters: [SwiftBleManager.errorPayload(error)]
1884
1896
  )
1885
1897
  } else {
1886
1898
  invokeAndClearDictionary(
@@ -1911,7 +1923,7 @@ public class SwiftBleManager: NSObject, CBCentralManagerDelegate,
1911
1923
  invokeAndClearDictionary(
1912
1924
  &writeCallbacks,
1913
1925
  withKey: key,
1914
- usingParameters: [error.localizedDescription]
1926
+ usingParameters: [SwiftBleManager.errorPayload(error)]
1915
1927
  )
1916
1928
  serialQueue.sync {
1917
1929
  writeQueues.removeValue(forKey: key)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "react-native-ble-manager",
3
- "version": "12.4.5",
3
+ "version": "12.5.1",
4
4
  "description": "A BLE module for react native.",
5
5
  "homepage": "https://innoveit.github.io/react-native-ble-manager/",
6
6
  "repository": {