@rodrigo7/react-native-beacons-manager 1.0.0
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.
- package/.flowconfig +21 -0
- package/.nvmrc +1 -0
- package/.prettierignore +4 -0
- package/.prettierrc +12 -0
- package/.vscode/settings.json +24 -0
- package/LICENSE +21 -0
- package/README.md +246 -0
- package/ReactNativeBeaconsManager.podspec +13 -0
- package/android/.project +17 -0
- package/android/.settings/org.eclipse.buildship.core.prefs +2 -0
- package/android/build.gradle +108 -0
- package/android/gradle/wrapper/gradle-wrapper.jar +0 -0
- package/android/gradle/wrapper/gradle-wrapper.properties +6 -0
- package/android/gradle/wrapper/gradle.properties +2 -0
- package/android/gradlew +160 -0
- package/android/gradlew.bat +90 -0
- package/android/src/main/AndroidManifest.xml +8 -0
- package/android/src/main/java/com/mackentoch/beaconsandroid/BeaconsAndroidModule.java +457 -0
- package/android/src/main/java/com/mackentoch/beaconsandroid/BeaconsAndroidPackage.java +29 -0
- package/index.js +10 -0
- package/ios/RNiBeacon/RNiBeacon/ESSBeaconScanner.h +41 -0
- package/ios/RNiBeacon/RNiBeacon/ESSBeaconScanner.m +204 -0
- package/ios/RNiBeacon/RNiBeacon/ESSEddystone.h +117 -0
- package/ios/RNiBeacon/RNiBeacon/ESSEddystone.m +352 -0
- package/ios/RNiBeacon/RNiBeacon/ESSTimer.h +72 -0
- package/ios/RNiBeacon/RNiBeacon/ESSTimer.m +107 -0
- package/ios/RNiBeacon/RNiBeacon/RNiBeacon.h +16 -0
- package/ios/RNiBeacon/RNiBeacon/RNiBeacon.m +467 -0
- package/ios/RNiBeacon/RNiBeacon.xcodeproj/project.pbxproj +290 -0
- package/jsconfig.json +9 -0
- package/lib/next/module.types.js +169 -0
- package/lib/next/new.module.android.js +451 -0
- package/lib/next/new.module.ios.js +176 -0
- package/package.json +65 -0
- package/typings/index.d.ts +174 -0
- package/typings.json +7 -0
- package/yarn-error.log +4182 -0
|
@@ -0,0 +1,457 @@
|
|
|
1
|
+
package com.mackentoch.beaconsandroid;
|
|
2
|
+
|
|
3
|
+
import android.content.Context;
|
|
4
|
+
import android.content.Intent;
|
|
5
|
+
import android.content.ServiceConnection;
|
|
6
|
+
import android.os.RemoteException;
|
|
7
|
+
import org.jetbrains.annotations.Nullable;
|
|
8
|
+
import android.util.Log;
|
|
9
|
+
|
|
10
|
+
import com.facebook.react.bridge.Callback;
|
|
11
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
12
|
+
import com.facebook.react.bridge.ReactContext;
|
|
13
|
+
import com.facebook.react.bridge.ReactContextBaseJavaModule;
|
|
14
|
+
import com.facebook.react.bridge.ReactMethod;
|
|
15
|
+
import com.facebook.react.bridge.ReadableArray;
|
|
16
|
+
import com.facebook.react.bridge.WritableArray;
|
|
17
|
+
import com.facebook.react.bridge.WritableMap;
|
|
18
|
+
import com.facebook.react.bridge.WritableNativeArray;
|
|
19
|
+
import com.facebook.react.bridge.WritableNativeMap;
|
|
20
|
+
import com.facebook.react.modules.core.DeviceEventManagerModule;
|
|
21
|
+
|
|
22
|
+
import org.altbeacon.beacon.Beacon;
|
|
23
|
+
import org.altbeacon.beacon.BeaconManager;
|
|
24
|
+
import org.altbeacon.beacon.BeaconParser;
|
|
25
|
+
import org.altbeacon.beacon.BeaconTransmitter;
|
|
26
|
+
import org.altbeacon.beacon.Identifier;
|
|
27
|
+
import org.altbeacon.beacon.MonitorNotifier;
|
|
28
|
+
import org.altbeacon.beacon.RangeNotifier;
|
|
29
|
+
import org.altbeacon.beacon.Region;
|
|
30
|
+
import org.altbeacon.beacon.service.ArmaRssiFilter;
|
|
31
|
+
import org.altbeacon.beacon.service.RunningAverageRssiFilter;
|
|
32
|
+
|
|
33
|
+
import java.util.Collection;
|
|
34
|
+
import java.util.HashMap;
|
|
35
|
+
import java.util.Map;
|
|
36
|
+
|
|
37
|
+
public class BeaconsAndroidModule extends ReactContextBaseJavaModule {
|
|
38
|
+
private static final String LOG_TAG = "BeaconsAndroidModule";
|
|
39
|
+
private static final int RUNNING_AVG_RSSI_FILTER = 0;
|
|
40
|
+
private static final int ARMA_RSSI_FILTER = 1;
|
|
41
|
+
private BeaconManager mBeaconManager;
|
|
42
|
+
private Context mApplicationContext;
|
|
43
|
+
private ReactApplicationContext mReactContext;
|
|
44
|
+
|
|
45
|
+
public BeaconsAndroidModule(ReactApplicationContext reactContext) {
|
|
46
|
+
super(reactContext);
|
|
47
|
+
Log.d(LOG_TAG, "BeaconsAndroidModule - started");
|
|
48
|
+
this.mReactContext = reactContext;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
@Override
|
|
52
|
+
public void initialize() {
|
|
53
|
+
this.mApplicationContext = this.mReactContext.getApplicationContext();
|
|
54
|
+
this.mBeaconManager = BeaconManager.getInstanceForApplication(mApplicationContext);
|
|
55
|
+
this.mBeaconManager.addRangeNotifier(mRangeNotifier);
|
|
56
|
+
this.mBeaconManager.addMonitorNotifier(mMonitorNotifier);
|
|
57
|
+
// need to bind at instantiation so that service loads (to test more)
|
|
58
|
+
//mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
|
|
59
|
+
|
|
60
|
+
//mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
@Override
|
|
64
|
+
public String getName() {
|
|
65
|
+
return LOG_TAG;
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
@Override
|
|
69
|
+
public Map<String, Object> getConstants() {
|
|
70
|
+
final Map<String, Object> constants = new HashMap<>();
|
|
71
|
+
constants.put("SUPPORTED", BeaconTransmitter.SUPPORTED);
|
|
72
|
+
constants.put("NOT_SUPPORTED_MIN_SDK", BeaconTransmitter.NOT_SUPPORTED_MIN_SDK);
|
|
73
|
+
constants.put("NOT_SUPPORTED_BLE", BeaconTransmitter.NOT_SUPPORTED_BLE);
|
|
74
|
+
constants.put("NOT_SUPPORTED_CANNOT_GET_ADVERTISER_MULTIPLE_ADVERTISEMENTS", BeaconTransmitter.NOT_SUPPORTED_CANNOT_GET_ADVERTISER_MULTIPLE_ADVERTISEMENTS);
|
|
75
|
+
constants.put("NOT_SUPPORTED_CANNOT_GET_ADVERTISER", BeaconTransmitter.NOT_SUPPORTED_CANNOT_GET_ADVERTISER);
|
|
76
|
+
constants.put("RUNNING_AVG_RSSI_FILTER",RUNNING_AVG_RSSI_FILTER);
|
|
77
|
+
constants.put("ARMA_RSSI_FILTER",ARMA_RSSI_FILTER);
|
|
78
|
+
return constants;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
@ReactMethod
|
|
82
|
+
public void setHardwareEqualityEnforced(Boolean e) {
|
|
83
|
+
Beacon.setHardwareEqualityEnforced(e.booleanValue());
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/*public void bindManager() {
|
|
87
|
+
if (!mBeaconManager.isBound(this)) {
|
|
88
|
+
Log.d(LOG_TAG, "BeaconsAndroidModule - bindManager: ");
|
|
89
|
+
mBeaconManager.bind(this);
|
|
90
|
+
}
|
|
91
|
+
}*/
|
|
92
|
+
|
|
93
|
+
/*public void unbindManager() {
|
|
94
|
+
if (mBeaconManager.isBound(this)) {
|
|
95
|
+
Log.d(LOG_TAG, "BeaconsAndroidModule - unbindManager: ");
|
|
96
|
+
mBeaconManager.unbind(this);
|
|
97
|
+
}
|
|
98
|
+
}*/
|
|
99
|
+
|
|
100
|
+
@ReactMethod
|
|
101
|
+
public void addParser(String parser, Callback resolve, Callback reject) {
|
|
102
|
+
try {
|
|
103
|
+
Log.d(LOG_TAG, "BeaconsAndroidModule - addParser: " + parser);
|
|
104
|
+
//unbindManager();
|
|
105
|
+
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(parser));
|
|
106
|
+
//bindManager();
|
|
107
|
+
resolve.invoke();
|
|
108
|
+
} catch (Exception e) {
|
|
109
|
+
reject.invoke(e.getMessage());
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
@ReactMethod
|
|
114
|
+
public void removeParser(String parser, Callback resolve, Callback reject) {
|
|
115
|
+
try {
|
|
116
|
+
Log.d(LOG_TAG, "BeaconsAndroidModule - removeParser: " + parser);
|
|
117
|
+
//unbindManager();
|
|
118
|
+
mBeaconManager.getBeaconParsers().remove(new BeaconParser().setBeaconLayout(parser));
|
|
119
|
+
//bindManager();
|
|
120
|
+
resolve.invoke();
|
|
121
|
+
} catch (Exception e) {
|
|
122
|
+
reject.invoke(e.getMessage());
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
@ReactMethod
|
|
127
|
+
public void addParsersListToDetection(ReadableArray parsers, Callback resolve, Callback reject) {
|
|
128
|
+
try {
|
|
129
|
+
//unbindManager();
|
|
130
|
+
for (int i = 0; i < parsers.size(); i++) {
|
|
131
|
+
String parser = parsers.getString(i);
|
|
132
|
+
Log.d(LOG_TAG, "addParsersListToDetection - add parser: " + parser);
|
|
133
|
+
mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(parser));
|
|
134
|
+
}
|
|
135
|
+
//bindManager();
|
|
136
|
+
resolve.invoke(parsers);
|
|
137
|
+
} catch (Exception e) {
|
|
138
|
+
reject.invoke(e.getMessage());
|
|
139
|
+
}
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
@ReactMethod
|
|
143
|
+
public void removeParsersListToDetection(ReadableArray parsers, Callback resolve, Callback reject) {
|
|
144
|
+
try {
|
|
145
|
+
//unbindManager();
|
|
146
|
+
for (int i = 0; i < parsers.size(); i++) {
|
|
147
|
+
String parser = parsers.getString(i);
|
|
148
|
+
Log.d(LOG_TAG, "removeParsersListToDetection - remove parser: " + parser);
|
|
149
|
+
mBeaconManager.getBeaconParsers().remove(new BeaconParser().setBeaconLayout(parser));
|
|
150
|
+
}
|
|
151
|
+
//bindManager();
|
|
152
|
+
resolve.invoke(parsers);
|
|
153
|
+
} catch (Exception e) {
|
|
154
|
+
reject.invoke(e.getMessage());
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
|
|
158
|
+
@ReactMethod
|
|
159
|
+
public void setBackgroundScanPeriod(int period) {
|
|
160
|
+
mBeaconManager.setBackgroundScanPeriod((long) period);
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
@ReactMethod
|
|
164
|
+
public void setBackgroundBetweenScanPeriod(int period) {
|
|
165
|
+
mBeaconManager.setBackgroundBetweenScanPeriod((long) period);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
@ReactMethod
|
|
169
|
+
public void setForegroundScanPeriod(int period) {
|
|
170
|
+
mBeaconManager.setForegroundScanPeriod((long) period);
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
@ReactMethod
|
|
174
|
+
public void setForegroundBetweenScanPeriod(int period) {
|
|
175
|
+
mBeaconManager.setForegroundBetweenScanPeriod((long) period);
|
|
176
|
+
}
|
|
177
|
+
|
|
178
|
+
@ReactMethod
|
|
179
|
+
public void setRssiFilter(int filterType, double avgModifier) {
|
|
180
|
+
String logMsg = "Could not set the rssi filter.";
|
|
181
|
+
if (filterType==RUNNING_AVG_RSSI_FILTER){
|
|
182
|
+
logMsg="Setting filter RUNNING_AVG";
|
|
183
|
+
BeaconManager.setRssiFilterImplClass(RunningAverageRssiFilter.class);
|
|
184
|
+
if (avgModifier>0){
|
|
185
|
+
RunningAverageRssiFilter.setSampleExpirationMilliseconds((long) avgModifier);
|
|
186
|
+
logMsg+=" with custom avg modifier";
|
|
187
|
+
}
|
|
188
|
+
} else if (filterType==ARMA_RSSI_FILTER){
|
|
189
|
+
logMsg="Setting filter ARMA";
|
|
190
|
+
BeaconManager.setRssiFilterImplClass(ArmaRssiFilter.class);
|
|
191
|
+
if (avgModifier>0){
|
|
192
|
+
ArmaRssiFilter.setDEFAULT_ARMA_SPEED(avgModifier);
|
|
193
|
+
logMsg+=" with custom avg modifier";
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
Log.d(LOG_TAG, logMsg);
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
@ReactMethod
|
|
200
|
+
public void checkTransmissionSupported(Callback callback) {
|
|
201
|
+
int result = BeaconTransmitter.checkTransmissionSupported(mReactContext);
|
|
202
|
+
callback.invoke(result);
|
|
203
|
+
}
|
|
204
|
+
|
|
205
|
+
@ReactMethod
|
|
206
|
+
public void getMonitoredRegions(Callback callback) {
|
|
207
|
+
WritableArray array = new WritableNativeArray();
|
|
208
|
+
for (Region region: mBeaconManager.getMonitoredRegions()) {
|
|
209
|
+
WritableMap map = new WritableNativeMap();
|
|
210
|
+
map.putString("identifier", region.getUniqueId());
|
|
211
|
+
map.putString("uuid", region.getId1().toString());
|
|
212
|
+
map.putInt("major", region.getId2() != null ? region.getId2().toInt() : 0);
|
|
213
|
+
map.putInt("minor", region.getId3() != null ? region.getId3().toInt() : 0);
|
|
214
|
+
array.pushMap(map);
|
|
215
|
+
}
|
|
216
|
+
callback.invoke(array);
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
@ReactMethod
|
|
220
|
+
public void getRangedRegions(Callback callback) {
|
|
221
|
+
WritableArray array = new WritableNativeArray();
|
|
222
|
+
for (Region region: mBeaconManager.getRangedRegions()) {
|
|
223
|
+
WritableMap map = new WritableNativeMap();
|
|
224
|
+
map.putString("region", region.getUniqueId());
|
|
225
|
+
map.putString("uuid", region.getId1().toString());
|
|
226
|
+
array.pushMap(map);
|
|
227
|
+
}
|
|
228
|
+
callback.invoke(array);
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
/***********************************************************************************************
|
|
232
|
+
* BeaconConsumer
|
|
233
|
+
**********************************************************************************************/
|
|
234
|
+
/*@Override
|
|
235
|
+
public void onBeaconServiceConnect() {
|
|
236
|
+
Log.v(LOG_TAG, "onBeaconServiceConnect");
|
|
237
|
+
|
|
238
|
+
// deprecated since v2.9 (see github: https://github.com/AltBeacon/android-beacon-library/releases/tag/2.9)
|
|
239
|
+
// mBeaconManager.setMonitorNotifier(mMonitorNotifier);
|
|
240
|
+
// mBeaconManager.setRangeNotifier(mRangeNotifier);
|
|
241
|
+
mBeaconManager.addMonitorNotifier(mMonitorNotifier);
|
|
242
|
+
mBeaconManager.addRangeNotifier(mRangeNotifier);
|
|
243
|
+
sendEvent(mReactContext, "beaconServiceConnected", null);
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
@Override
|
|
247
|
+
public Context getApplicationContext() {
|
|
248
|
+
return mApplicationContext;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
@Override
|
|
252
|
+
public void unbindService(ServiceConnection serviceConnection) {
|
|
253
|
+
mApplicationContext.unbindService(serviceConnection);
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
@Override
|
|
257
|
+
public boolean bindService(Intent intent, ServiceConnection serviceConnection, int i) {
|
|
258
|
+
return mApplicationContext.bindService(intent, serviceConnection, i);
|
|
259
|
+
}*/
|
|
260
|
+
|
|
261
|
+
/***********************************************************************************************
|
|
262
|
+
* Monitoring
|
|
263
|
+
**********************************************************************************************/
|
|
264
|
+
@ReactMethod
|
|
265
|
+
public void startMonitoring(String regionId, String beaconUuid, int minor, int major, Callback resolve, Callback reject) {
|
|
266
|
+
Log.d(LOG_TAG, "startMonitoring, monitoringRegionId: " + regionId + ", monitoringBeaconUuid: " + beaconUuid + ", minor: " + minor + ", major: " + major);
|
|
267
|
+
try {
|
|
268
|
+
Region region = createRegion(
|
|
269
|
+
regionId,
|
|
270
|
+
beaconUuid,
|
|
271
|
+
String.valueOf(minor).equals("-1") ? "" : String.valueOf(minor),
|
|
272
|
+
String.valueOf(major).equals("-1") ? "" : String.valueOf(major)
|
|
273
|
+
);
|
|
274
|
+
//mBeaconManager.startMonitoringBeaconsInRegion(region);
|
|
275
|
+
mBeaconManager.startMonitoring(region);
|
|
276
|
+
resolve.invoke();
|
|
277
|
+
} catch (Exception e) {
|
|
278
|
+
Log.e(LOG_TAG, "startMonitoring, error: ", e);
|
|
279
|
+
reject.invoke(e.getMessage());
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
|
|
283
|
+
private MonitorNotifier mMonitorNotifier = new MonitorNotifier() {
|
|
284
|
+
@Override
|
|
285
|
+
public void didEnterRegion(Region region) {
|
|
286
|
+
sendEvent(mReactContext, "regionDidEnter", createMonitoringResponse(region));
|
|
287
|
+
}
|
|
288
|
+
|
|
289
|
+
@Override
|
|
290
|
+
public void didExitRegion(Region region) {
|
|
291
|
+
sendEvent(mReactContext, "regionDidExit", createMonitoringResponse(region));
|
|
292
|
+
}
|
|
293
|
+
|
|
294
|
+
@Override
|
|
295
|
+
public void didDetermineStateForRegion(int i, Region region) {
|
|
296
|
+
String state = "unknown";
|
|
297
|
+
switch (i) {
|
|
298
|
+
case MonitorNotifier.INSIDE:
|
|
299
|
+
state = "inside";
|
|
300
|
+
break;
|
|
301
|
+
case MonitorNotifier.OUTSIDE:
|
|
302
|
+
state = "outside";
|
|
303
|
+
break;
|
|
304
|
+
default:
|
|
305
|
+
break;
|
|
306
|
+
}
|
|
307
|
+
WritableMap map = createMonitoringResponse(region);
|
|
308
|
+
map.putString("state", state);
|
|
309
|
+
sendEvent(mReactContext, "didDetermineState", map);
|
|
310
|
+
}
|
|
311
|
+
};
|
|
312
|
+
|
|
313
|
+
private WritableMap createMonitoringResponse(Region region) {
|
|
314
|
+
WritableMap map = new WritableNativeMap();
|
|
315
|
+
map.putString("identifier", region.getUniqueId());
|
|
316
|
+
map.putString("uuid", region.getId1() != null ? region.getId1().toString() : "");
|
|
317
|
+
map.putInt("major", region.getId2() != null ? region.getId2().toInt() : 0);
|
|
318
|
+
map.putInt("minor", region.getId3() != null ? region.getId3().toInt() : 0);
|
|
319
|
+
return map;
|
|
320
|
+
}
|
|
321
|
+
|
|
322
|
+
@ReactMethod
|
|
323
|
+
public void stopMonitoring(String regionId, String beaconUuid, int minor, int major, Callback resolve, Callback reject) {
|
|
324
|
+
Region region = createRegion(
|
|
325
|
+
regionId,
|
|
326
|
+
beaconUuid,
|
|
327
|
+
String.valueOf(minor).equals("-1") ? "" : String.valueOf(minor),
|
|
328
|
+
String.valueOf(major).equals("-1") ? "" : String.valueOf(major)
|
|
329
|
+
// minor,
|
|
330
|
+
// major
|
|
331
|
+
);
|
|
332
|
+
|
|
333
|
+
try {
|
|
334
|
+
mBeaconManager.stopMonitoring(region);
|
|
335
|
+
resolve.invoke();
|
|
336
|
+
} catch (Exception e) {
|
|
337
|
+
Log.e(LOG_TAG, "stopMonitoring, error: ", e);
|
|
338
|
+
reject.invoke(e.getMessage());
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/***********************************************************************************************
|
|
343
|
+
* Ranging
|
|
344
|
+
**********************************************************************************************/
|
|
345
|
+
@ReactMethod
|
|
346
|
+
public void startRanging(String regionId, String beaconUuid, Callback resolve, Callback reject) {
|
|
347
|
+
Log.d(LOG_TAG, "startRanging, rangingRegionId: " + regionId + ", rangingBeaconUuid: " + beaconUuid);
|
|
348
|
+
try {
|
|
349
|
+
Region region = createRegion(regionId, beaconUuid);
|
|
350
|
+
mBeaconManager.startRangingBeacons(region);
|
|
351
|
+
resolve.invoke();
|
|
352
|
+
} catch (Exception e) {
|
|
353
|
+
Log.e(LOG_TAG, "startRanging, error: ", e);
|
|
354
|
+
reject.invoke(e.getMessage());
|
|
355
|
+
}
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
private RangeNotifier mRangeNotifier = new RangeNotifier() {
|
|
359
|
+
@Override
|
|
360
|
+
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
|
|
361
|
+
Log.d(LOG_TAG, "rangingConsumer didRangeBeaconsInRegion, beacons: " + beacons.toString());
|
|
362
|
+
Log.d(LOG_TAG, "rangingConsumer didRangeBeaconsInRegion, region: " + region.toString());
|
|
363
|
+
sendEvent(mReactContext, "beaconsDidRange", createRangingResponse(beacons, region));
|
|
364
|
+
}
|
|
365
|
+
};
|
|
366
|
+
|
|
367
|
+
private WritableMap createRangingResponse(Collection<Beacon> beacons, Region region) {
|
|
368
|
+
WritableMap map = new WritableNativeMap();
|
|
369
|
+
map.putString("identifier", region.getUniqueId());
|
|
370
|
+
map.putString("uuid", region.getId1() != null ? region.getId1().toString() : "");
|
|
371
|
+
WritableArray a = new WritableNativeArray();
|
|
372
|
+
for (Beacon beacon : beacons) {
|
|
373
|
+
WritableMap b = new WritableNativeMap();
|
|
374
|
+
b.putString("uuid", beacon.getId1().toString());
|
|
375
|
+
if (beacon.getIdentifiers().size() > 2) {
|
|
376
|
+
b.putInt("major", beacon.getId2().toInt());
|
|
377
|
+
b.putInt("minor", beacon.getId3().toInt());
|
|
378
|
+
}
|
|
379
|
+
b.putInt("rssi", beacon.getRssi());
|
|
380
|
+
if(beacon.getDistance() == Double.POSITIVE_INFINITY
|
|
381
|
+
|| Double.isNaN(beacon.getDistance())
|
|
382
|
+
|| beacon.getDistance() == Double.NaN
|
|
383
|
+
|| beacon.getDistance() == Double.NEGATIVE_INFINITY){
|
|
384
|
+
b.putDouble("distance", 999.0);
|
|
385
|
+
b.putString("proximity", "far");
|
|
386
|
+
}else {
|
|
387
|
+
b.putDouble("distance", beacon.getDistance());
|
|
388
|
+
b.putString("proximity", getProximity(beacon.getDistance()));
|
|
389
|
+
}
|
|
390
|
+
a.pushMap(b);
|
|
391
|
+
}
|
|
392
|
+
map.putArray("beacons", a);
|
|
393
|
+
return map;
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
private String getProximity(double distance) {
|
|
397
|
+
if (distance == -1.0) {
|
|
398
|
+
return "unknown";
|
|
399
|
+
} else if (distance < 1) {
|
|
400
|
+
return "immediate";
|
|
401
|
+
} else if (distance < 3) {
|
|
402
|
+
return "near";
|
|
403
|
+
} else {
|
|
404
|
+
return "far";
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
|
|
408
|
+
@ReactMethod
|
|
409
|
+
public void stopRanging(String regionId, String beaconUuid, Callback resolve, Callback reject) {
|
|
410
|
+
Region region = createRegion(regionId, beaconUuid);
|
|
411
|
+
try {
|
|
412
|
+
mBeaconManager.stopRangingBeacons(region);
|
|
413
|
+
resolve.invoke();
|
|
414
|
+
} catch (Exception e) {
|
|
415
|
+
Log.e(LOG_TAG, "stopRanging, error: ", e);
|
|
416
|
+
reject.invoke(e.getMessage());
|
|
417
|
+
}
|
|
418
|
+
}
|
|
419
|
+
|
|
420
|
+
@ReactMethod
|
|
421
|
+
public void requestStateForRegion(String regionId, String beaconUuid, int minor, int major) {
|
|
422
|
+
Region region = createRegion(
|
|
423
|
+
regionId,
|
|
424
|
+
beaconUuid,
|
|
425
|
+
String.valueOf(minor).equals("-1") ? "" : String.valueOf(minor),
|
|
426
|
+
String.valueOf(major).equals("-1") ? "" : String.valueOf(major)
|
|
427
|
+
);
|
|
428
|
+
mBeaconManager.requestStateForRegion(region);
|
|
429
|
+
}
|
|
430
|
+
|
|
431
|
+
|
|
432
|
+
/***********************************************************************************************
|
|
433
|
+
* Utils
|
|
434
|
+
**********************************************************************************************/
|
|
435
|
+
private void sendEvent(ReactContext reactContext, String eventName, @Nullable WritableMap params) {
|
|
436
|
+
if (reactContext.hasActiveReactInstance()) {
|
|
437
|
+
reactContext
|
|
438
|
+
.getJSModule(DeviceEventManagerModule.RCTDeviceEventEmitter.class)
|
|
439
|
+
.emit(eventName, params);
|
|
440
|
+
}
|
|
441
|
+
}
|
|
442
|
+
|
|
443
|
+
private Region createRegion(String regionId, String beaconUuid) {
|
|
444
|
+
Identifier id1 = (beaconUuid == null) ? null : Identifier.parse(beaconUuid);
|
|
445
|
+
return new Region(regionId, id1, null, null);
|
|
446
|
+
}
|
|
447
|
+
|
|
448
|
+
private Region createRegion(String regionId, String beaconUuid, String minor, String major) {
|
|
449
|
+
Identifier id1 = (beaconUuid == null) ? null : Identifier.parse(beaconUuid);
|
|
450
|
+
return new Region(
|
|
451
|
+
regionId,
|
|
452
|
+
id1,
|
|
453
|
+
major.length() > 0 ? Identifier.parse(major) : null,
|
|
454
|
+
minor.length() > 0 ? Identifier.parse(minor) : null
|
|
455
|
+
);
|
|
456
|
+
}
|
|
457
|
+
}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
package com.mackentoch.beaconsandroid;
|
|
2
|
+
|
|
3
|
+
import com.facebook.react.ReactPackage;
|
|
4
|
+
import com.facebook.react.bridge.JavaScriptModule;
|
|
5
|
+
import com.facebook.react.bridge.NativeModule;
|
|
6
|
+
import com.facebook.react.bridge.ReactApplicationContext;
|
|
7
|
+
import com.facebook.react.uimanager.ViewManager;
|
|
8
|
+
|
|
9
|
+
import java.util.ArrayList;
|
|
10
|
+
import java.util.List;
|
|
11
|
+
|
|
12
|
+
public class BeaconsAndroidPackage implements ReactPackage {
|
|
13
|
+
|
|
14
|
+
@Override
|
|
15
|
+
public List<NativeModule> createNativeModules(ReactApplicationContext reactContext) {
|
|
16
|
+
List<NativeModule> modules = new ArrayList<>();
|
|
17
|
+
modules.add(new BeaconsAndroidModule(reactContext));
|
|
18
|
+
return modules;
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
@Override
|
|
22
|
+
public List<ViewManager> createViewManagers(ReactApplicationContext reactContext) {
|
|
23
|
+
return new ArrayList<>();
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
public List<Class<? extends JavaScriptModule>> createJSModules() {
|
|
27
|
+
return new ArrayList<>();
|
|
28
|
+
}
|
|
29
|
+
}
|
package/index.js
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
// flow
|
|
2
|
+
|
|
3
|
+
import { Platform } from 'react-native';
|
|
4
|
+
|
|
5
|
+
const RNiBeaconsModule = Platform.select({
|
|
6
|
+
ios: () => require('./lib/next/new.module.ios.js'),
|
|
7
|
+
android: () => require('./lib/next/new.module.android.js'),
|
|
8
|
+
})();
|
|
9
|
+
|
|
10
|
+
export default RNiBeaconsModule;
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
// Copyright 2015 Google Inc. All rights reserved.
|
|
2
|
+
//
|
|
3
|
+
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
4
|
+
// you may not use this file except in compliance with the License.
|
|
5
|
+
// You may obtain a copy of the License at
|
|
6
|
+
//
|
|
7
|
+
// http://www.apache.org/licenses/LICENSE-2.0
|
|
8
|
+
//
|
|
9
|
+
// Unless required by applicable law or agreed to in writing, software
|
|
10
|
+
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
11
|
+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
12
|
+
// See the License for the specific language governing permissions and
|
|
13
|
+
// limitations under the License.
|
|
14
|
+
|
|
15
|
+
#import <Foundation/Foundation.h>
|
|
16
|
+
|
|
17
|
+
@class ESSBeaconScanner;
|
|
18
|
+
|
|
19
|
+
// Delegates to the ESSBeaconScanner should implement this protocol.
|
|
20
|
+
@protocol ESSBeaconScannerDelegate <NSObject>
|
|
21
|
+
|
|
22
|
+
@optional
|
|
23
|
+
|
|
24
|
+
- (void)beaconScanner:(ESSBeaconScanner *)scanner
|
|
25
|
+
didRangeBeacon:(NSArray *)beacons;
|
|
26
|
+
|
|
27
|
+
- (void)beaconScanner:(ESSBeaconScanner *)scanner
|
|
28
|
+
didFindURL:(NSURL *)url;
|
|
29
|
+
|
|
30
|
+
@end
|
|
31
|
+
|
|
32
|
+
@interface ESSBeaconScanner : NSObject
|
|
33
|
+
|
|
34
|
+
@property(nonatomic, weak) id<ESSBeaconScannerDelegate> delegate;
|
|
35
|
+
|
|
36
|
+
@property(nonatomic, assign) NSTimeInterval onLostTimeout;
|
|
37
|
+
|
|
38
|
+
- (void)startScanning;
|
|
39
|
+
- (void)stopScanning;
|
|
40
|
+
|
|
41
|
+
@end
|