react-native-ble-manager 12.4.3 → 12.4.4

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.
@@ -1099,15 +1099,34 @@ public class Peripheral extends BluetoothGattCallback {
1099
1099
  writeCallbacks.addLast(callback);
1100
1100
  }
1101
1101
  return enqueue(() -> {
1102
- if (!isConnected() || gatt == null) {
1103
- if (withResponse && callback != null) {
1104
- writeCallbacks.removeLastOccurrence(callback);
1105
- callback.invoke("Device is not connected", null);
1106
- }
1107
- completedCommand();
1108
- return;
1109
- }
1110
- doWrite(characteristic, copyOfData);
1102
+ 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
+ }
1111
1130
  });
1112
1131
  }
1113
1132
 
@@ -4,105 +4,159 @@ export * from './types';
4
4
  declare class BleManager {
5
5
  constructor();
6
6
  /**
7
+ * Read the current value of the specified characteristic, you need to call `retrieveServices` method before.
7
8
  *
8
- * @param peripheralId
9
- * @param serviceUUID
10
- * @param characteristicUUID
11
- * @returns data as an array of numbers (which can be converted back to a Uint8Array (ByteArray) using something like [Buffer.from()](https://github.com/feross/buffer))
9
+ * @param peripheralId The id/mac address of the peripheral.
10
+ * @param serviceUUID The UUID of the service.
11
+ * @param characteristicUUID The UUID of the characteristic.
12
+ * @returns Data as an array of numbers (which can be converted back to a Uint8Array (ByteArray) using something like [Buffer.from()](https://github.com/feross/buffer))
12
13
  */
13
14
  read(peripheralId: string, serviceUUID: string, characteristicUUID: string): Promise<number[]>;
14
15
  /**
16
+ * Read the current value of the specified descriptor, you need to call `retrieveServices` method before.
15
17
  *
16
- * @param peripheralId
17
- * @param serviceUUID
18
- * @param characteristicUUID
19
- * @param descriptorUUID
18
+ * @param peripheralId The id/mac address of the peripheral.
19
+ * @param serviceUUID The UUID of the service.
20
+ * @param characteristicUUID The UUID of the characteristic.
21
+ * @param descriptorUUID The UUID of the descriptor.
20
22
  * @returns data as an array of numbers (which can be converted back to a Uint8Array (ByteArray) using something like [Buffer.from()](https://github.com/feross/buffer))
21
23
  */
22
24
  readDescriptor(peripheralId: string, serviceUUID: string, characteristicUUID: string, descriptorUUID: string): Promise<number[]>;
23
25
  /**
26
+ * Write a value to the specified descriptor, you need to call `retrieveServices` method before.
24
27
  *
25
- * @param peripheralId
26
- * @param serviceUUID
27
- * @param characteristicUUID
28
- * @param descriptorUUID
29
- * @param data data to write as an array of numbers (which can be converted from a Uint8Array (ByteArray) using something like [Buffer.toJSON().data](https://github.com/feross/buffer))
28
+ * @param peripheralId The id/mac address of the peripheral.
29
+ * @param serviceUUID The UUID of the service.
30
+ * @param characteristicUUID The UUID of the characteristic.
31
+ * @param descriptorUUID The UUID of the descriptor.
32
+ * @param data Data to write as an array of numbers (which can be converted from a Uint8Array (ByteArray) using something like [Buffer.toJSON().data](https://github.com/feross/buffer))
30
33
  * @returns
31
34
  */
32
35
  writeDescriptor(peripheralId: string, serviceUUID: string, characteristicUUID: string, descriptorUUID: string, data: number[]): Promise<void>;
33
36
  /**
37
+ * Read the current value of the RSSI.
34
38
  *
35
- * @param peripheralId
36
- * @returns a promise resolving with the updated RSSI (`number`) if it succeeds.
39
+ * @param peripheralId The id/mac address of the peripheral.
40
+ * @returns A promise resolving with the updated RSSI (`number`) if it succeeds.
37
41
  */
38
42
  readRSSI(peripheralId: string): Promise<number>;
39
43
  /**
40
44
  * [Android only]
41
- * @param peripheralId
42
- * @returns a promise that resolves to a boolean indicating if gatt was successfully refreshed or not.
45
+ *
46
+ * Refreshes the peripheral's services and characteristics cache.
47
+ *
48
+ * @param peripheralId The id/mac address of the peripheral.
49
+ * @returns A promise that resolves to a boolean indicating if gatt was successfully refreshed or not.
43
50
  */
44
51
  refreshCache(peripheralId: string): Promise<boolean>;
45
52
  /**
53
+ * Retrieve the peripheral's services and characteristics.
46
54
  *
47
- * @param peripheralId
48
- * @param serviceUUIDs [iOS only] optional filter of services to retrieve.
55
+ * @param peripheralId The id/mac address of the peripheral.
56
+ * @param serviceUUIDs [iOS only] Optional filter of services to retrieve.
49
57
  * @returns
50
58
  */
51
59
  retrieveServices(peripheralId: string, serviceUUIDs?: string[]): Promise<PeripheralInfo>;
52
60
  /**
61
+ * Write with response to the specified characteristic, you need to call `retrieveServices` method before.
53
62
  *
54
- * @param peripheralId
55
- * @param serviceUUID
56
- * @param characteristicUUID
57
- * @param data data to write as an array of numbers (which can be converted from a Uint8Array (ByteArray) using something like [Buffer.toJSON().data](https://github.com/feross/buffer))
58
- * @param maxByteSize optional, defaults to 20
63
+ * @param peripheralId The id/mac address of the peripheral.
64
+ * @param serviceUUID The UUID of the service.
65
+ * @param characteristicUUID The UUID of the characteristic.
66
+ * @param data Data to write as an array of numbers (which can be converted from a Uint8Array (ByteArray) using something like [Buffer.toJSON().data](https://github.com/feross/buffer))
67
+ * @param maxByteSize Optional, defaults to 20
59
68
  * @returns
60
69
  */
61
70
  write(peripheralId: string, serviceUUID: string, characteristicUUID: string, data: number[], maxByteSize?: number): Promise<void>;
62
71
  /**
72
+ * Write without response to the specified characteristic, you need to call `retrieveServices` method before.
63
73
  *
64
- * @param peripheralId
65
- * @param serviceUUID
66
- * @param characteristicUUID
67
- * @param data data to write as an array of numbers (which can be converted from a Uint8Array (ByteArray) using something like [Buffer.toJSON().data](https://github.com/feross/buffer))
68
- * @param maxByteSize optional, defaults to 20
69
- * @param queueSleepTime optional, defaults to 10. Only useful if data length is greater than maxByteSize.
74
+ * @param peripheralId The id/mac address of the peripheral.
75
+ * @param serviceUUID The UUID of the service.
76
+ * @param characteristicUUID The UUID of the characteristic.
77
+ * @param data Data to write as an array of numbers (which can be converted from a Uint8Array (ByteArray) using something like [Buffer.toJSON().data](https://github.com/feross/buffer))
78
+ * @param maxByteSize Optional, defaults to 20
79
+ * @param queueSleepTime Optional, defaults to 10. Only useful if data length is greater than maxByteSize.
70
80
  * @returns
71
81
  */
72
82
  writeWithoutResponse(peripheralId: string, serviceUUID: string, characteristicUUID: string, data: number[], maxByteSize?: number, queueSleepTime?: number): Promise<void>;
83
+ /**
84
+ * Attempts to connect to a peripheral. In many case if you can't connect you have to scan for the peripheral before.
85
+ *
86
+ * > In iOS, attempts to connect to a peripheral do not time out (please see [Apple's doc](https://developer.apple.com/documentation/corebluetooth/cbcentralmanager/1518766-connect)), so you might need to set a timer explicitly if you don't want this behavior.
87
+ */
73
88
  connect(peripheralId: string, options?: ConnectOptions): Promise<void>;
74
89
  /**
75
90
  * [Android only]
76
- * @param peripheralId
77
- * @param peripheralPin optional. will be used to auto-bond if possible.
91
+ *
92
+ * Start the bonding (pairing) process with the remote device.
93
+ * If you pass peripheralPin (optional), bonding will be auto (without manually entering the pin).
94
+ * > Ensure to make one bond request at a time.
95
+ *
96
+ * @param peripheralId The id/mac address of the peripheral.
97
+ * @param peripheralPin Optional. will be used to auto-bond if possible.
78
98
  * @returns
79
99
  */
80
100
  createBond(peripheralId: string, peripheralPin?: string | null): Promise<void>;
81
101
  /**
82
102
  * [Android only]
83
- * @param peripheralId
103
+ *
104
+ * Remove a paired device.
105
+ *
106
+ * @param peripheralId The id/mac address of the peripheral
84
107
  * @returns
85
108
  */
86
109
  removeBond(peripheralId: string): Promise<void>;
87
110
  /**
111
+ * Disconnect from a peripheral.
88
112
  *
89
- * @param peripheralId
90
- * @param force [Android only] defaults to true.
113
+ * @param peripheralId The id/mac address of the peripheral to disconnect.
114
+ * @param force [Android only] Defaults to true. Don't wait for the disconnect state to close the Gatt client.
91
115
  * @returns
92
116
  */
93
117
  disconnect(peripheralId: string, force?: boolean): Promise<void>;
118
+ /**
119
+ * Start the notification on the specified characteristic, you need to call `retrieveServices` method before.
120
+ *
121
+ * Events will be send to `onDidUpdateValueForCharacteristic` when the peripheral notifies a new value for the characteristic.
122
+ *
123
+ * @param peripheralId The id/mac address of the peripheral.
124
+ * @param serviceUUID The UUID of the service.
125
+ * @param characteristicUUID The UUID of the characteristic.
126
+ * @returns
127
+ */
94
128
  startNotification(peripheralId: string, serviceUUID: string, characteristicUUID: string): Promise<void>;
95
129
  /**
96
130
  * [Android only]
97
- * @param peripheralId
98
- * @param serviceUUID
99
- * @param characteristicUUID
100
- * @param buffer
131
+ *
132
+ * Start the notification on the specified characteristic, you need to call `retrieveServices` method before.
133
+ * The buffer collect messages until the buffer of messages bytes reaches the limit defined with the `buffer` argument and then emit all the collected data.
134
+ * Useful to reduce the number of calls between the native and the react-native part in case of many messages.
135
+ *
136
+ * @param peripheralId The id/mac address of the peripheral.
137
+ * @param serviceUUID The UUID of the service.
138
+ * @param characteristicUUID The UUID of the characteristic.
139
+ * @param buffer The capacity of the buffer (bytes) stored before emitting the data for the characteristic.
101
140
  * @returns
102
141
  */
103
142
  startNotificationWithBuffer(peripheralId: string, serviceUUID: string, characteristicUUID: string, buffer: number): Promise<void>;
143
+ /**
144
+ * Stop the notification on the specified characteristic.
145
+ *
146
+ * @param peripheralId The id/mac address of the peripheral.
147
+ * @param serviceUUID The UUID of the service.
148
+ * @param characteristicUUID The UUID of the characteristic.
149
+ * @returns
150
+ */
104
151
  stopNotification(peripheralId: string, serviceUUID: string, characteristicUUID: string): Promise<void>;
152
+ /**
153
+ * Force the module to check the state of the native BLE manager and trigger an event for `onDidUpdateState`.
154
+ * @returns A promise containing the current BleState
155
+ */
105
156
  checkState(): Promise<BleState>;
157
+ /**
158
+ * Init the module. Don't call this multiple times.
159
+ */
106
160
  start(options?: StartOptions): Promise<void>;
107
161
  /**
108
162
  * Check if the BLE manager has been started.
@@ -110,11 +164,19 @@ declare class BleManager {
110
164
  */
111
165
  isStarted(): Promise<boolean>;
112
166
  /**
167
+ * Scan for available peripherals.
113
168
  *
114
- * @param scanningOptions optional map of properties to fine-tune scan behavior, see DOCS.
169
+ * See `onDiscoverPeripheral` to get live updates of devices being discovered.
170
+ *
171
+ * See `getDiscoveredPeripherals` to get a list of discovered devices after a scan is completed.
172
+ *
173
+ * @param scanningOptions Optional map of properties to fine-tune scan behavior, see DOCS.
115
174
  * @returns
116
175
  */
117
176
  scan(scanningOptions?: ScanOptions): Promise<void>;
177
+ /**
178
+ * Stop the scanning.
179
+ */
118
180
  stopScan(): Promise<void>;
119
181
  /**
120
182
  * [Android only] triggers an ENABLE_REQUEST intent to the end-user to enable bluetooth.
@@ -122,57 +184,81 @@ declare class BleManager {
122
184
  */
123
185
  enableBluetooth(): Promise<void>;
124
186
  /**
187
+ * Return the connected peripherals.
188
+ *
189
+ * > In Android, Peripherals "advertising" property can be not set!
190
+ * > Will be available if peripheral was found through scan before connect.
191
+ * > This matches to current Android Bluetooth design specification.
125
192
  *
126
- * @param serviceUUIDs [optional] not used on android, optional on ios.
193
+ * @param serviceUUIDs [iOS only] Optional, only retrieve peripherals with these services. Ignored in Android.
127
194
  * @returns
128
195
  */
129
196
  getConnectedPeripherals(serviceUUIDs?: string[]): Promise<Peripheral[]>;
130
197
  /**
131
198
  * [Android only]
199
+ *
200
+ * Return the bonded peripherals.
201
+ *
132
202
  * @returns
133
203
  */
134
204
  getBondedPeripherals(): Promise<Peripheral[]>;
205
+ /**
206
+ * Return the discovered peripherals after a scan.
207
+ */
135
208
  getDiscoveredPeripherals(): Promise<Peripheral[]>;
136
209
  /**
137
210
  * [Android only]
138
- * @param peripheralId
211
+ *
212
+ * Removes a disconnected peripheral from the cached list.
213
+ * It is useful if the device is turned off, because it will be re-discovered upon turning on again.
214
+ *
215
+ * @param peripheralId The id/mac address of the peripheral.
139
216
  * @returns
140
217
  */
141
218
  removePeripheral(peripheralId: string): Promise<void>;
142
219
  /**
143
- * @param peripheralId
144
- * @param serviceUUIDs [optional] not used on android, optional on ios.
220
+ * Check whether a specific peripheral is connected and return `true` or `false`.
221
+ *
222
+ * @param peripheralId The id/mac address of the peripheral.
223
+ * @param serviceUUIDs [iOS only] Optional, only retrieve peripherals with these services. Ignored in Android.
145
224
  * @returns
146
225
  */
147
226
  isPeripheralConnected(peripheralId: string, serviceUUIDs?: string[]): Promise<boolean>;
148
227
  /**
149
- * @param peripheralId
150
- * @param serviceUUIDs [optional] not used on android, optional on ios.
228
+ * Checks whether the scan is in progress and return `true` or `false`.
151
229
  * @returns
152
230
  */
153
231
  isScanning(): Promise<boolean>;
154
232
  /**
155
233
  * [Android only, API 21+]
156
- * @param peripheralId
157
- * @param connectionPriority
158
- * @returns a promise that resolves with a boolean indicating of the connection priority was changed successfully, or rejects with an error message.
234
+ * @param peripheralId The id/mac address of the peripheral.
235
+ * @param connectionPriority The connection priority to be requested
236
+ * @returns A promise that resolves with a boolean indicating of the connection priority was changed successfully, or rejects with an error message.
159
237
  */
160
238
  requestConnectionPriority(peripheralId: string, connectionPriority: ConnectionPriority): Promise<boolean>;
161
239
  /**
162
240
  * [Android only, API 21+]
163
- * @param peripheralId
164
- * @param mtu size to be requested, in bytes.
165
- * @returns a promise resolving with the negotiated MTU if it succeeded. Beware that it might not be the one requested due to device's BLE limitations on both side of the negotiation.
241
+ *
242
+ * Request an MTU size used for a given connection.
243
+ *
244
+ * @param peripheralId The id/mac address of the peripheral.
245
+ * @param mtu Size to be requested, in bytes.
246
+ * @returns A promise resolving with the negotiated MTU if it succeeded. Beware that it might not be the one requested due to device's BLE limitations on both side of the negotiation.
166
247
  */
167
248
  requestMTU(peripheralId: string, mtu: number): Promise<number>;
168
249
  /**
169
250
  * [Android only, API 26+]
170
251
  *
252
+ * Retrieve associated peripherals (from companion manager).
253
+ *
171
254
  * @returns
172
255
  */
173
256
  getAssociatedPeripherals(): Promise<Peripheral[]>;
174
257
  /**
175
258
  * [Android only, API 26+]
259
+ *
260
+ * Remove an associated peripheral.
261
+ *
176
262
  * @param peripheralId Peripheral to remove
177
263
  * @returns Promise that resolves once the peripheral has been removed. Rejects
178
264
  * if no association is found.
@@ -181,7 +267,7 @@ declare class BleManager {
181
267
  /**
182
268
  * [Android only]
183
269
  *
184
- * Check if current device supports companion device manager.
270
+ * Check if current device supports the companion device manager.
185
271
  *
186
272
  * @return Promise resolving to a boolean.
187
273
  */
@@ -189,35 +275,95 @@ declare class BleManager {
189
275
  /**
190
276
  * [Android only, API 26+]
191
277
  *
192
- * Start companion scan.
278
+ * Scan for companion devices.
279
+ *
280
+ * Rejects if the companion device manager is not supported on this device.
281
+ *
282
+ * The promise it will eventually resolve with either:
283
+ *
284
+ * 1. peripheral if user selects one
285
+ * 2. null if user "cancels" (i.e. doesn't select anything)
286
+ *
287
+ * See `BleManager.supportsCompanion`.
288
+ *
289
+ * See: https://developer.android.com/develop/connectivity/bluetooth/companion-device-pairing
290
+ *
291
+ * @param serviceUUIDs List of service UUIDs to use as a filter
193
292
  */
194
293
  companionScan(serviceUUIDs: string[], options?: CompanionScanOptions): Promise<Peripheral | null>;
195
294
  /**
196
295
  * [Android only]
296
+ *
297
+ * Create the request to set the name of the bluetooth adapter. (https://developer.android.com/reference/android/bluetooth/BluetoothAdapter#setName(java.lang.String))
298
+ *
197
299
  * @param name
198
300
  */
199
301
  setName(name: string): void;
200
302
  /**
201
303
  * [iOS only]
202
- * @param peripheralId
304
+ * @param peripheralId The id/mac address of the peripheral.
203
305
  * @returns
204
306
  */
205
307
  getMaximumWriteValueLengthForWithoutResponse(peripheralId: string): Promise<number>;
206
308
  /**
207
309
  * [iOS only]
208
- * @param peripheralId
310
+ * @param peripheralId The id/mac address of the peripheral.
209
311
  * @returns
210
312
  */
211
313
  getMaximumWriteValueLengthForWithResponse(peripheralId: string): Promise<number>;
314
+ /**
315
+ * The scanning found a new peripheral.
316
+ */
212
317
  onDiscoverPeripheral(callback: EventCallback<BleDiscoverPeripheralEvent>): EventSubscription;
318
+ /**
319
+ * The scanning for peripherals is ended.
320
+ */
213
321
  onStopScan(callback: EventCallback<BleStopScanEvent>): EventSubscription;
322
+ /**
323
+ * The BLE state changed.
324
+ */
214
325
  onDidUpdateState(callback: EventCallback<BleManagerDidUpdateStateEvent>): EventSubscription;
326
+ /**
327
+ * A peripheral was connected.
328
+ */
215
329
  onConnectPeripheral(callback: EventCallback<BleConnectPeripheralEvent>): EventSubscription;
330
+ /**
331
+ * A peripheral was disconnected.
332
+ */
216
333
  onDisconnectPeripheral(callback: EventCallback<BleDisconnectPeripheralEvent>): EventSubscription;
334
+ /**
335
+ * A characteristic notified a new value.
336
+ *
337
+ * > Event will only be emitted after successful `startNotification`.
338
+ */
217
339
  onDidUpdateValueForCharacteristic(callback: EventCallback<BleManagerDidUpdateValueForCharacteristicEvent>): EventSubscription;
340
+ /**
341
+ * A bond with a peripheral was established.
342
+ */
218
343
  onPeripheralDidBond(callback: EventCallback<BleBondedPeripheralEvent>): EventSubscription;
344
+ /**
345
+ * [iOS only]
346
+ *
347
+ * This is fired when [`centralManager:WillRestoreState:`](https://developer.apple.com/documentation/corebluetooth/cbcentralmanagerdelegate/1518819-centralmanager) is called (app relaunched in the background to handle a bluetooth event).
348
+ *
349
+ * _For more on performing long-term bluetooth actions in the background:_
350
+ *
351
+ * [iOS Bluetooth State Preservation and Restoration](https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html#//apple_ref/doc/uid/TP40013257-CH7-SW10)
352
+ *
353
+ * [iOS Relaunch Conditions](https://developer.apple.com/documentation/technotes/tn3115-bluetooth-state-restoration-app-relaunch-rules/)
354
+ */
219
355
  onCentralManagerWillRestoreState(callback: EventCallback<BleManagerCentralManagerWillRestoreState>): EventSubscription;
356
+ /**
357
+ * [iOS only]
358
+ *
359
+ * The peripheral received a request to start or stop providing notifications for a specified characteristic's value.
360
+ */
220
361
  onDidUpdateNotificationStateFor(callback: EventCallback<BleManagerDidUpdateNotificationStateForEvent>): EventSubscription;
362
+ /**
363
+ * User picked a device to associate with.
364
+ *
365
+ * Null if the request was cancelled by the user.
366
+ */
221
367
  onCompanionPeripheral(callback: EventCallback<BleManagerCompanionPeripheral>): EventSubscription;
222
368
  }
223
369
  declare const _default: BleManager;