-
Notifications
You must be signed in to change notification settings - Fork 61
/
index.js
390 lines (350 loc) · 10.4 KB
/
index.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
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
const ReactNative = require("react-native");
const React = require("react");
const { Buffer } = require("buffer");
const { NativeModules, DeviceEventEmitter } = ReactNative;
const { BluetoothSerial } = NativeModules;
/**
* High order component that will
* attach native event emitter and
* send it as a props named subscription.
*
* It will create an emitter when component did mount
* and remove all listeners when component will unmount.
*
* @param {Object} [options]
* @param {String} [options.subscriptionName=subscription]
* @param {Boolean} [options.destroyOnWilUnmount=true]
* @return {React.Component}
*/
export const withSubscription = (
options = {
subscriptionName: "subscription",
destroyOnWilUnmount: true
}
) => WrappedComponent => {
const subscriptionName =
typeof options.subscriptionName === "string" &&
options.subscriptionName !== ""
? options.subscriptionName
: "subscription";
const destroyOnWilUnmount =
typeof options.destroyOnWilUnmount === "boolean"
? options.destroyOnWilUnmount
: true;
const subscription = DeviceEventEmitter;
subscription.on = DeviceEventEmitter.addListener;
subscription.off = DeviceEventEmitter.removeListener;
subscription.remove = DeviceEventEmitter.removeAllListeners;
return class RTCBluetoothSerialComponent extends React.Component {
componentWillUnmount() {
if (destroyOnWilUnmount && subscription) {
if (typeof subscription.remove === "function") {
subscription.remove();
}
if (typeof subscription.removeAllListeners === "function") {
subscription.removeAllListeners();
console.log(subscription);
}
}
}
render() {
return (
<WrappedComponent
{...this.props}
{...{ [subscriptionName]: subscription }}
>
{this.props.children}
</WrappedComponent>
);
}
};
};
/**
* Select a specific bluetooth device and
* give you the ability to read / write from
* that device.
*
* @param {String} [id]
* @return {Object}
*/
BluetoothSerial.device = (id = null) => ({
/**
* Connect to certain bluetooth device / peripheral.
*
* @return {Promise<Object>}
*
* @throws this will throws an error if android bluetooth adapter
* is missing.
*/
connect: () => BluetoothSerial.connect(id),
/**
* Disconnect from the selected bluetooth device / peripheral.
*
* @return {Promise<Boolean>}
*
* @throws this will throws an error if android bluetooth adapter
* is missing.
*/
disconnect: () => BluetoothSerial.disconnect(id),
/**
* Indicates if you are connected to the selected bluetooth device / peripheral or not.
*
* @return {Promise<Boolean>}
*/
isConnected: () => BluetoothSerial.isConnected(id),
/**
* Clear all buffer data of the selected bluetooth device / peripheral.
*
* @return {Promise<Boolean>}
*/
clear: () => BluetoothSerial.clear(id),
/**
* Get length of buffer data from the selected bluetooth device / peripheral.
*
* @return {Promise<Number>}
*/
available: () => BluetoothSerial.available(id),
/**
* Set delimiter split the buffer data
* when you are reading from the selected device.
*
* @param delimiter
* @return {Promise<String>}
*/
withDelimiter: delimiter => BluetoothSerial.withDelimiter(delimiter, id),
/**
* Listen and read data from the selected device.
*
* @param {Function} [callback=() => {}]
* @param {String} [delimiter=""]
*/
read: (callback = () => {}, delimiter = "") => {
if (typeof callback !== "function") {
return;
}
BluetoothSerial.withDelimiter(delimiter, id).then(deviceId => {
const subscription = BluetoothSerial.addListener("read", result => {
const { id: readDeviceId, data } = result;
if (readDeviceId === deviceId) {
callback(data, subscription);
}
});
});
},
/**
* Read data from the selected device once.
*
* @param {String} [delimiter=""]
* @return {Promise<String>}
*/
readOnce: (delimiter = "") =>
typeof delimiter === "string"
? BluetoothSerial.readUntilDelimiter(delimiter, id)
: BluetoothSerial.readFromDevice(id),
/**
* Read data from the selected device every n ms.
*
* @param {Function} [callback=() => {}]
* @param {Number} [ms=1000]
* @param {String} [delimiter=""]
*/
readEvery: (callback = () => {}, ms = 1000, delimiter = "") => {
if (typeof callback !== "function") {
return;
}
const intervalId = setInterval(async () => {
const data =
typeof delimiter === "string"
? await BluetoothSerial.readUntilDelimiter(delimiter, id)
: await BluetoothSerial.readFromDevice(id);
callback(data, intervalId);
}, ms);
},
/**
* Read all buffer data up to particular delimiter
* from the selected device.
*
* @param delimiter
* @return {Promise<String>}
*/
readUntilDelimiter: delimiter =>
BluetoothSerial.readUntilDelimiter(delimiter, id),
/**
* Read all buffer data from connected device.
*
* @return {Promise<String>}
*/
readFromDevice: () => BluetoothSerial.readFromDevice(id),
/**
* Write data to the selected device, you can pass string or buffer,
* We must convert to base64 in RN there is no way to pass buffer directly.
*
* @param {Buffer|String} data
* @return {Promise<Boolean>}
*/
write: data => {
if (typeof data === "string") {
data = new Buffer(data);
}
return BluetoothSerial.writeToDevice(data.toString("base64"), id);
},
/**
* Write string to the selected device.
*
* @param {String} data
* @return {Promise<Boolean>}
*/
writeToDevice: data => BluetoothSerial.writeToDevice(data, id)
});
/**
* Similar to addListener, except that the listener is removed after it is
* invoked once.
*
* @param eventName - Name of the event to listen to
* @param listener - Function to invoke only once when the
* specified event is emitted
* @param context - Optional context object to use when invoking the
* listener
*/
BluetoothSerial.once = (eventName, handler, context) =>
DeviceEventEmitter.once(eventName, handler, context);
/**
* Attach listener to a certain event name.
*
* @param eventName - Name of the event to listen to
* @param listener - Function to invoke only once when the
* specified event is emitted
* @param context - Optional context object to use when invoking the
* listener
*/
BluetoothSerial.addListener = (eventName, handler, context) =>
DeviceEventEmitter.addListener(eventName, handler, context);
/**
* Attach listener to a certain event name.
*
* @param eventName - Name of the event to listen to
* @param listener - Function to invoke only once when the
* specified event is emitted
* @param context - Optional context object to use when invoking the
* listener
*/
BluetoothSerial.on = (eventName, handler, context) =>
DeviceEventEmitter.addListener(eventName, handler, context);
/**
* Removes the given listener for event of specific type.
*
* @param eventName - Name of the event to emit
* @param listener - Function to invoke when the specified event is
* emitted
*
* @example
* emitter.removeListener('someEvent', function(message) {
* console.log(message);
* }); // removes the listener if already registered
*
*/
BluetoothSerial.removeListener = (eventName, handler) =>
DeviceEventEmitter.removeListener(eventName, handler);
/**
* Removes the given listener for event of specific type.
*
* @param eventName - Name of the event to emit
* @param listener - Function to invoke when the specified event is
* emitted
*
* @example
* emitter.removeListener('someEvent', function(message) {
* console.log(message);
* }); // removes the listener if already registered
*
*/
BluetoothSerial.off = (eventName, handler) =>
DeviceEventEmitter.removeListener(eventName, handler);
/**
* Removes all of the registered listeners, including those registered as
* listener maps.
*
* @param eventName - Optional name of the event whose registered
* listeners to remove
*/
BluetoothSerial.removeAllListeners = eventName =>
DeviceEventEmitter.removeAllListeners(eventName);
/**
* Removes a specific subscription. Called by the `remove()` method of the
* subscription itself to ensure any necessary cleanup is performed.
*/
BluetoothSerial.removeSubscription = subscription =>
DeviceEventEmitter.removeSubscription(subscription);
/**
* Listen and read data from device.
*
* @param {Function} callback
* @param {String} [delimiter=""]
* @param {String} [id]
*/
BluetoothSerial.read = (callback, delimiter = "", id = null) => {
if (typeof callback !== "function") {
return;
}
BluetoothSerial.withDelimiter(delimiter, id).then(deviceId => {
const subscription = BluetoothSerial.addListener("read", result => {
const { id: readDeviceId, data } = result;
if (readDeviceId === deviceId) {
callback(data, subscription);
}
});
});
};
/**
* Read data from device once.
*
* @param {String} [delimiter=""]
* @param {String} [id]
* @return {Promise<String>}
*/
BluetoothSerial.readOnce = (delimiter = "", id = null) =>
typeof delimiter === "string"
? BluetoothSerial.readUntilDelimiter(delimiter, id)
: BluetoothSerial.readFromDevice(id);
/**
* Read data from device every n ms.
*
* @param {Function} callback
* @param {Number} [ms=1000]
* @param {String} [delimiter=""]
* @param {String} [id]
*/
BluetoothSerial.readEvery = (
callback = () => {},
ms = 1000,
delimiter = "",
id = null
) => {
if (typeof callback !== "function") {
return;
}
const intervalId = setInterval(async () => {
const data =
typeof delimiter === "string"
? await BluetoothSerial.readUntilDelimiter(delimiter, id)
: await BluetoothSerial.readFromDevice(id);
callback(data, intervalId);
}, ms);
};
/**
* Write data to device, you can pass string or buffer,
* We must convert to base64 in RN there is no way to pass buffer directly.
*
* @param {Buffer|String} data
* @param {String} [id]
* @return {Promise<Boolean>}
*/
BluetoothSerial.write = (data, id = null) => {
if (typeof data === "string") {
data = new Buffer(data);
}
return BluetoothSerial.writeToDevice(data.toString("base64"), id);
};
BluetoothSerial.discoverUnpairedDevices = BluetoothSerial.listUnpaired;
BluetoothSerial.stopScanning = BluetoothSerial.cancelDiscovery;
export default BluetoothSerial;