forked from dotintent/react-native-ble-plx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Device.js
364 lines (331 loc) · 13.4 KB
/
Device.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
// @flow
'use strict'
import type { BleManager } from './BleManager'
import type { BleError } from './BleError'
import type { Characteristic } from './Characteristic'
import type { Service } from './Service'
import type { Descriptor } from './Descriptor'
import { ConnectionPriority } from './TypeDefinition'
import type { NativeDevice } from './BleModule'
import type { DeviceId, Base64, UUID, Subscription, TransactionId, ConnectionOptions } from './TypeDefinition'
/**
* Device instance which can be retrieved only by calling
* {@link #blemanagerstartdevicescan|bleManager.startDeviceScan()}.
*/
export class Device implements NativeDevice {
/**
* Internal BLE Manager handle
* @private
*/
_manager: BleManager
/**
* Device identifier: MAC address on Android and UUID on iOS.
*/
id: DeviceId
/**
* Device name if present
*/
name: ?string
/**
* Current Received Signal Strength Indication of device
*/
rssi: ?number
/**
* Current Maximum Transmission Unit for this device. When device is not connected
* default value of 23 is used.
*/
mtu: number
// Advertisement
/**
* Device's custom manufacturer data. Its format is defined by manufacturer.
*/
manufacturerData: ?Base64
/**
* Map of service UUIDs (as keys) with associated data (as values).
*/
serviceData: ?{ [uuid: UUID]: Base64 }
/**
* List of available services visible during scanning.
*/
serviceUUIDs: ?Array<UUID>
/**
* User friendly name of device.
*/
localName: ?string
/**
* Transmission power level of device.
*/
txPowerLevel: ?number
/**
* List of solicited service UUIDs.
*/
solicitedServiceUUIDs: ?Array<UUID>
/**
* Is device connectable. [iOS only]
*/
isConnectable: ?boolean
/**
* List of overflow service UUIDs. [iOS only]
*/
overflowServiceUUIDs: ?Array<UUID>
/**
* Private constructor used to create {@link Device} object.
*
* @param {NativeDevice} nativeDevice Native device properties
* @param {BleManager} manager {@link BleManager} handle
* @private
*/
constructor(nativeDevice: NativeDevice, manager: BleManager) {
Object.assign(this, nativeDevice, { _manager: manager })
}
/**
* {@link #blemanagerrequestconnectionpriorityfordevice|bleManager.requestConnectionPriorityForDevice()} with partially filled arguments.
*
* @param {ConnectionPriority} connectionPriority: Connection priority.
* @param {?TransactionId} transactionId Transaction handle used to cancel operation.
* @returns {Promise<Device>} Connected device.
*/
requestConnectionPriority(
connectionPriority: $Values<typeof ConnectionPriority>,
transactionId: ?TransactionId
): Promise<Device> {
return this._manager.requestConnectionPriorityForDevice(this.id, connectionPriority, transactionId)
}
/**
* {@link #blemanagerreadrssifordevice|bleManager.readRSSIForDevice()} with partially filled arguments.
*
* @param {?TransactionId} transactionId Transaction handle used to cancel operation.
* @returns {Promise<Device>} This device with updated RSSI value.
*/
readRSSI(transactionId: ?TransactionId): Promise<Device> {
return this._manager.readRSSIForDevice(this.id, transactionId)
}
/**
* {@link #blemanagerrequestmtufordevice|bleManager.requestMTUForDevice()} with partially filled arguments.
*
* @param {?TransactionId} transactionId Transaction handle used to cancel operation.
* @returns {Promise<Device>} Device with updated MTU size. Default value is 23.
*/
requestMTU(mtu: number, transactionId: ?TransactionId): Promise<Device> {
return this._manager.requestMTUForDevice(this.id, mtu, transactionId)
}
/**
* {@link #blemanagerconnecttodevice|bleManager.connectToDevice()} with partially filled arguments.
*
* @param {?ConnectionOptions} options Platform specific options for connection establishment. Not used currently.
* @returns {Promise<Device>} Connected {@link Device} object if successful.
*/
connect(options: ?ConnectionOptions): Promise<Device> {
return this._manager.connectToDevice(this.id, options)
}
/**
* {@link #blemanagercanceldeviceconnection|bleManager.cancelDeviceConnection()} with partially filled arguments.
*
* @returns {Promise<Device>} Returns closed {@link Device} when operation is successful.
*/
cancelConnection(): Promise<Device> {
return this._manager.cancelDeviceConnection(this.id)
}
/**
* {@link #blemanagerisdeviceconnected|bleManager.isDeviceConnected()} with partially filled arguments.
*
* @returns {Promise<boolean>} Promise which emits `true` if device is connected, and `false` otherwise.
*/
isConnected(): Promise<boolean> {
return this._manager.isDeviceConnected(this.id)
}
/**
* {@link #blemanagerondevicedisconnected|bleManager.onDeviceDisconnected()} with partially filled arguments.
*
* @param {function(error: ?BleError, device: Device)} listener callback returning error as a reason of disconnection
* if available and {@link Device} object. If an error is null, that means the connection was terminated by
* {@link #blemanagercanceldeviceconnection|bleManager.cancelDeviceConnection()} call.
* @returns {Subscription} Subscription on which `remove()` function can be called to unsubscribe.
*/
onDisconnected(listener: (error: ?BleError, device: Device) => void): Subscription {
return this._manager.onDeviceDisconnected(this.id, listener)
}
/**
* {@link #blemanagerdiscoverallservicesandcharacteristicsfordevice|bleManager.discoverAllServicesAndCharacteristicsForDevice()} with partially filled arguments.
*
* @param {?TransactionId} transactionId Transaction handle used to cancel operation
* @returns {Promise<Device>} Promise which emits {@link Device} object if all available services and
* characteristics have been discovered.
*/
discoverAllServicesAndCharacteristics(transactionId: ?TransactionId): Promise<Device> {
return this._manager.discoverAllServicesAndCharacteristicsForDevice(this.id, transactionId)
}
/**
* {@link #blemanagerservicesfordevice|bleManager.servicesForDevice()} with partially filled arguments.
*
* @returns {Promise<Service[]>} Promise which emits array of {@link Service} objects which are discovered by this
* device.
*/
services(): Promise<Service[]> {
return this._manager.servicesForDevice(this.id)
}
/**
* {@link #blemanagercharacteristicsfordevice|bleManager.characteristicsForDevice()} with partially filled arguments.
*
* @param {UUID} serviceUUID {@link Service} UUID.
* @returns {Promise<Characteristic[]>} Promise which emits array of {@link Characteristic} objects which are
* discovered for a {@link Device} in specified {@link Service}.
*/
characteristicsForService(serviceUUID: string): Promise<Characteristic[]> {
return this._manager.characteristicsForDevice(this.id, serviceUUID)
}
/**
* {@link #blemanagerdescriptorsfordevice|bleManager.descriptorsForDevice()} with partially filled arguments.
*
* @param {UUID} serviceUUID {@link Service} UUID.
* @param {UUID} characteristicUUID {@link Characteristic} UUID.
* @returns {Promise<Array<Descriptor>>} Promise which emits array of {@link Descriptor} objects which are
* discovered for this {@link Characteristic}.
*/
descriptorsForService(serviceUUID: UUID, characteristicUUID: UUID): Promise<Array<Descriptor>> {
return this._manager.descriptorsForDevice(this.id, serviceUUID, characteristicUUID)
}
/**
* {@link #blemanagerreadcharacteristicfordevice|bleManager.readCharacteristicForDevice()} with partially filled arguments.
*
* @param {UUID} serviceUUID {@link Service} UUID.
* @param {UUID} characteristicUUID {@link Characteristic} UUID.
* @param {?TransactionId} transactionId optional `transactionId` which can be used in
* {@link #blemanagercanceltransaction|bleManager.cancelTransaction()} function.
* @returns {Promise<Characteristic>} Promise which emits first {@link Characteristic} object matching specified
* UUID paths. Latest value of {@link Characteristic} will be stored inside returned object.
*/
readCharacteristicForService(
serviceUUID: UUID,
characteristicUUID: UUID,
transactionId: ?TransactionId
): Promise<Characteristic> {
return this._manager.readCharacteristicForDevice(this.id, serviceUUID, characteristicUUID, transactionId)
}
/**
* {@link #blemanagerwritecharacteristicwithresponsefordevice|bleManager.writeCharacteristicWithResponseForDevice()} with partially filled arguments.
*
* @param {UUID} serviceUUID {@link Service} UUID.
* @param {UUID} characteristicUUID {@link Characteristic} UUID.
* @param {Base64} valueBase64 Value in Base64 format.
* @param {?TransactionId} transactionId optional `transactionId` which can be used in
* {@link #blemanagercanceltransaction|bleManager.cancelTransaction()} function.
* @returns {Promise<Characteristic>} Promise which emits first {@link Characteristic} object matching specified
* UUID paths. Latest value of characteristic may not be stored inside returned object.
*/
writeCharacteristicWithResponseForService(
serviceUUID: UUID,
characteristicUUID: UUID,
valueBase64: Base64,
transactionId: ?TransactionId
): Promise<Characteristic> {
return this._manager.writeCharacteristicWithResponseForDevice(
this.id,
serviceUUID,
characteristicUUID,
valueBase64,
transactionId
)
}
/**
* {@link #blemanagerwritecharacteristicwithoutresponsefordevice|bleManager.writeCharacteristicWithoutResponseForDevice()} with partially filled arguments.
*
* @param {UUID} serviceUUID {@link Service} UUID.
* @param {UUID} characteristicUUID {@link Characteristic} UUID.
* @param {Base64} valueBase64 Value in Base64 format.
* @param {?TransactionId} transactionId optional `transactionId` which can be used in
* {@link #blemanagercanceltransaction|bleManager.cancelTransaction()} function.
* @returns {Promise<Characteristic>} Promise which emits first {@link Characteristic} object matching specified
* UUID paths. Latest value of characteristic may not be stored inside returned object.
*/
writeCharacteristicWithoutResponseForService(
serviceUUID: UUID,
characteristicUUID: UUID,
valueBase64: Base64,
transactionId: ?TransactionId
): Promise<Characteristic> {
return this._manager.writeCharacteristicWithoutResponseForDevice(
this.id,
serviceUUID,
characteristicUUID,
valueBase64,
transactionId
)
}
/**
* {@link #blemanagermonitorcharacteristicfordevice|bleManager.monitorCharacteristicForDevice()} with partially filled arguments.
*
* @param {UUID} serviceUUID {@link Service} UUID.
* @param {UUID} characteristicUUID {@link Characteristic} UUID.
* @param {function(error: ?BleError, characteristic: ?Characteristic)} listener - callback which emits
* {@link Characteristic} objects with modified value for each notification.
* @param {?TransactionId} transactionId optional `transactionId` which can be used in
* {@link #blemanagercanceltransaction|bleManager.cancelTransaction()} function.
* @returns {Subscription} Subscription on which `remove()` function can be called to unsubscribe.
*/
monitorCharacteristicForService(
serviceUUID: UUID,
characteristicUUID: UUID,
listener: (error: ?BleError, characteristic: ?Characteristic) => void,
transactionId: ?TransactionId
): Subscription {
return this._manager.monitorCharacteristicForDevice(
this.id,
serviceUUID,
characteristicUUID,
listener,
transactionId
)
}
/**
* {@link #blemanagerreaddescriptorfordevice|bleManager.readDescriptorForDevice()} with partially filled arguments.
*
* @param {UUID} serviceUUID {@link Service} UUID.
* @param {UUID} characteristicUUID {@link Characteristic} UUID.
* @param {UUID} descriptorUUID {@link Descriptor} UUID.
* @param {?TransactionId} transactionId optional `transactionId` which can be used in
* {@link #blemanagercanceltransaction|cancelTransaction()} function.
* @returns {Promise<Descriptor>} Promise which emits first {@link Descriptor} object matching specified
* UUID paths. Latest value of {@link Descriptor} will be stored inside returned object.
*/
async readDescriptorForService(
serviceUUID: UUID,
characteristicUUID: UUID,
descriptorUUID: UUID,
transactionId: ?TransactionId
): Promise<Descriptor> {
return this._manager.readDescriptorForDevice(
this.id,
serviceUUID,
characteristicUUID,
descriptorUUID,
transactionId
)
}
/**
* {@link #blemanagerwritedescriptorfordevice|bleManager.writeDescriptorForDevice()} with partially filled arguments.
*
* @param {UUID} serviceUUID {@link Service} UUID.
* @param {UUID} characteristicUUID Characteristic UUID
* @param {UUID} descriptorUUID Descriptor UUID
* @param {Base64} valueBase64 Value to be set coded in Base64
* @param {?TransactionId} transactionId Transaction handle used to cancel operation
* @returns {Promise<Descriptor>} Descriptor which saved passed value.
*/
async writeDescriptorForService(
serviceUUID: UUID,
characteristicUUID: UUID,
descriptorUUID: UUID,
valueBase64: Base64,
transactionId: ?TransactionId
): Promise<Descriptor> {
return this._manager.writeDescriptorForDevice(
this.id,
serviceUUID,
characteristicUUID,
descriptorUUID,
valueBase64,
transactionId
)
}
}