forked from MacKentoch/react-native-beacons-manager
-
Notifications
You must be signed in to change notification settings - Fork 1
/
new.module.android.js
451 lines (405 loc) · 11.4 KB
/
new.module.android.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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
// @flow
// #region imports
const RN = require('react-native');
import {
type BeaconRegion,
type BeaconsManagerANDROID,
type Parser,
} from './module.types';
import {
PARSER_IBEACON,
PARSER_ESTIMOTE,
PARSER_ALTBEACON,
PARSER_EDDYSTONE_TLM,
PARSER_EDDYSTONE_UID,
PARSER_EDDYSTONE_URL,
transmissionSupport,
} from './module.types';
// #endregion
// #region instanciation and constants
const BeaconsManager: BeaconsManagerANDROID =
RN.NativeModules.BeaconsAndroidModule;
const BeaconsEventEmitter = RN.DeviceEventEmitter;
const ARMA_RSSI_FILTER = BeaconsManager && BeaconsManager.ARMA_RSSI_FILTER || undefined;
const RUNNING_AVG_RSSI_FILTER = BeaconsManager && BeaconsManager.RUNNING_AVG_RSSI_FILTER || undefined;
// #endregion
function setHardwareEqualityEnforced(flag: boolean): void {
BeaconsManager.setHardwareEqualityEnforced(flag);
}
// #region iBeacon
/**
* set beacon layout for iBeacon
*
*/
function detectIBeacons(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(PARSER_IBEACON, resolve, reject);
});
}
/**
* same as detectIBeacons (intoduced in v1.1.0)
* adds iBeacon parser to detect them
*
*/
function addIBeaconsDetection(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(PARSER_IBEACON, resolve, reject);
});
}
/**
* removes iBeacon parser to stop detecting them
*
*/
function removeIBeaconsDetection(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.removeParser(PARSER_IBEACON, resolve, reject);
});
}
// #enregion
// #region altBeacon
/**
* set beacon layout for alBeacon
*
*/
function detectAltBeacons(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(PARSER_ALTBEACON, resolve, reject);
});
}
function addAltBeaconsDetection(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(PARSER_ALTBEACON, resolve, reject);
});
}
function removeAltBeaconsDetection(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.removeParser(PARSER_ALTBEACON, resolve, reject);
});
}
// #endregion
// #region estimote
/**
* set beacon layout for estimote
*
*/
function detectEstimotes(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(PARSER_ESTIMOTE, resolve, reject);
});
}
function addEstimotesDetection(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(PARSER_ESTIMOTE, resolve, reject);
});
}
function removeEstimotesDetection(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.removeParser(PARSER_ESTIMOTE, resolve, reject);
});
}
// #endregion
// #region eddystone UID
/**
* set beacon layout for eddystone UID
*
*/
function detectEddystoneUID(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(PARSER_EDDYSTONE_UID, resolve, reject);
});
}
/**
* same as detectEddystoneUID (intoduced in v1.1.0)
* adds EddystoneUID parser to detect them
*
*/
function addEddystoneUIDDetection(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(PARSER_EDDYSTONE_UID, resolve, reject);
});
}
/**
* removes EddystoneUID parser to stop detecting them
*
*/
function removeEddystoneUIDDetection(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.removeParser(PARSER_EDDYSTONE_UID, resolve, reject);
});
}
// #endregion
// #region eddystone URL
/**
* set beacon layout for eddystone URL
*
*/
function detectEddystoneURL(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(PARSER_EDDYSTONE_URL, resolve, reject);
});
}
function addEddystoneURLDetection(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(PARSER_EDDYSTONE_URL, resolve, reject);
});
}
function removeEddystoneURLDetection(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.removeParser(PARSER_EDDYSTONE_URL, resolve, reject);
});
}
// #endregion
// #region eddystone TLM
/**
* set beacon layout for eddystone TLM
*
*/
function detectEddystoneTLM(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(PARSER_EDDYSTONE_TLM, resolve, reject);
});
}
function addEddystoneTLMDetection(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(PARSER_EDDYSTONE_TLM, resolve, reject);
});
}
function removeEddystoneTLMDetection(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.removeParser(PARSER_EDDYSTONE_TLM, resolve, reject);
});
}
// #endregion
// #region custom beacon (set your parser)
/**
* set beacon for custom layout
*
*/
function detectCustomBeaconLayout(parser: number): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(parser, resolve, reject);
});
}
function addCustomBeaconLayoutDetection(parser: number): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.addParser(parser, resolve, reject);
});
}
function removeCustomBeaconLayoutDetection(parser: number): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.removeParser(parser, resolve, reject);
});
}
// #endregion
// #region add remove multiple parsers in a row
function addParsersListToDetection(parsers: Array<Parser>) {
return new Promise((resolve, reject) => {
BeaconsManager.addParsersListToDetection(parsers, resolve, reject);
});
}
function removeParsersListToDetection(parsers: Array<Parser>) {
return new Promise((resolve, reject) => {
BeaconsManager.removeParsersListToDetection(parsers, resolve, reject);
});
}
// #endregion
function setBackgroundScanPeriod(period: number): void {
BeaconsManager.setBackgroundScanPeriod(period);
}
function setBackgroundBetweenScanPeriod(period: number): void {
BeaconsManager.setBackgroundBetweenScanPeriod(period);
}
function setForegroundScanPeriod(period: number): void {
BeaconsManager.setForegroundScanPeriod(period);
}
function setRssiFilter(filterType: number, avgModifier: number): void {
BeaconsManager.setRssiFilter(filterType, avgModifier);
}
function getRangedRegions(): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.getRangedRegions(resolve);
});
}
/**
* get monitored regions
*
* @returns {Promise<Array<BeaconRegion>>} promise resolve to an array of monitored regions
*/
function getMonitoredRegions(): Promise<Array<BeaconRegion>> {
return new Promise((resolve, reject) => {
BeaconsManager.getMonitoredRegions(resolve);
});
}
/**
* check if beacon support transmission
*
* @returns {Promise<number>} promise resolve to an integer
*/
function checkTransmissionSupported(): Promise<number> {
return new Promise((resolve, reject) => {
BeaconsManager.checkTransmissionSupported(status =>
resolve(transmissionSupport[status]),
);
});
}
/**
* start monitoring for a region
*
* @param {Object: BeaconRegion} region region to monitor (identifier + uuid -> major and minor are optional)
* @returns {Promise<any>} promise resolves to void or error
*/
function startMonitoringForRegion(region: BeaconRegion): Promise<any> {
return new Promise((resolve, reject) => {
// NOTE: major and minor are optional values: if user don't assign them we have to send a null value (not undefined):
BeaconsManager.startMonitoring(
region.identifier,
region.uuid,
region.minor ? region.minor : -1,
region.major ? region.major : -1,
resolve,
reject,
);
});
}
/**
* stops monittorings for a region
*
* @param {BeaconRegion} region region (see BeaconRegion type)
* @returns {Promise<any>} promise resolves to void or error
*/
function stopMonitoringForRegion(region: BeaconRegion): Promise<any> {
return new Promise((resolve, reject) => {
BeaconsManager.stopMonitoring(
region.identifier,
region.uuid,
region.minor ? region.minor : -1,
region.major ? region.major : -1,
resolve,
reject,
);
});
}
/**
* start ranging a region (with optional UUID)
*
* @param {String | BeaconRegion} regionId if string or region: BeaconRegion object
* @param {String} [beaconsUUID] optional UUID
* @returns {Promise<any>} promise resolves to void or error
*/
function startRangingBeaconsInRegion(
region: BeaconRegion | string,
beaconsUUID?: string,
): Promise<any> {
if (typeof region === 'object') {
return new Promise((resolve, reject) => {
BeaconsManager.startRanging(
// $FlowIgnore
region.identifier,
// $FlowIgnore
region.uuid,
resolve,
reject,
);
});
}
return new Promise((resolve, reject) => {
BeaconsManager.startRanging(region, beaconsUUID, resolve, reject);
});
}
/**
* Stops the range scan for beacons
*
* @param {String | BeaconRegion} regionId if string or region: BeaconRegion object
* @param {string} beaconsUUID optional UUID within the specified region
* @returns {Promise<any>} promise: resolves to void when successful
*/
function stopRangingBeaconsInRegion(
region: BeaconRegion | string,
beaconsUUID?: string,
): Promise<any> {
if (typeof region === 'object') {
return new Promise((resolve, reject) => {
BeaconsManager.stopRanging(
// $FlowIgnore
region.identifier,
// $FlowIgnore
region.uuid,
resolve,
reject,
);
});
}
return new Promise((resolve, reject) => {
// $FlowIgnore
BeaconsManager.stopRanging(region, beaconsUUID, resolve, reject);
});
}
/**
* Retrieves the state of a region asynchronously.
*
* @param {BeaconRegion} region region (identifier + uuid -> major and minor are optional)
*/
function requestStateForRegion(region: BeaconRegion): void {
BeaconsManager.requestStateForRegion(
region.identifier,
region.uuid,
region.minor ? region.minor : -1,
region.major ? region.major : -1,
);
}
module.exports = {
// parsers constants
PARSER_IBEACON,
PARSER_ESTIMOTE,
PARSER_ALTBEACON,
PARSER_EDDYSTONE_TLM,
PARSER_EDDYSTONE_UID,
PARSER_EDDYSTONE_URL,
BeaconsEventEmitter,
setHardwareEqualityEnforced,
// iBeacons:
detectIBeacons,
addIBeaconsDetection,
removeIBeaconsDetection,
// alt beacons:
detectAltBeacons,
addAltBeaconsDetection,
removeAltBeaconsDetection,
// Estimotes beacon:
detectEstimotes,
addEstimotesDetection,
removeEstimotesDetection,
// Eddystone UID beacons:
detectEddystoneUID,
addEddystoneUIDDetection,
removeEddystoneUIDDetection,
// Eddystone TLM beacons:
detectEddystoneTLM,
addEddystoneTLMDetection,
removeEddystoneTLMDetection,
// Eddystone URL beacons:
detectEddystoneURL,
addEddystoneURLDetection,
removeEddystoneURLDetection,
// custom layout beacons (NOTE: create 'valid UUID' with websites like, for instance, this one: https://openuuid.net):
detectCustomBeaconLayout,
addCustomBeaconLayoutDetection,
removeCustomBeaconLayoutDetection,
addParsersListToDetection,
removeParsersListToDetection,
setBackgroundScanPeriod,
setBackgroundBetweenScanPeriod,
setForegroundScanPeriod,
setRssiFilter,
checkTransmissionSupported,
getRangedRegions,
ARMA_RSSI_FILTER,
RUNNING_AVG_RSSI_FILTER,
getMonitoredRegions,
// common with iOS:
startMonitoringForRegion,
startRangingBeaconsInRegion,
stopMonitoringForRegion,
stopRangingBeaconsInRegion,
requestStateForRegion,
};