forked from MacKentoch/react-native-beacons-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.android.js
433 lines (395 loc) · 12.7 KB
/
index.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
/**
* Sample React Native App
* https://github.com/facebook/react-native
* @flow
*/
// #region imports
import React, {
Component
} from 'react';
import {
AppRegistry,
StyleSheet,
Text,
SectionList,
View,
TouchableHighlight,
ToastAndroid,
Image
} from 'react-native';
import Beacons from 'react-native-beacons-manager';
import {
Avatar
} from 'react-native-elements';
import moment from 'moment';
import beaconIMAGE from './images/beacons/ibeacon.png';
import altBeaconIMAGE from './images/beacons/altbeacon.png';
import eddystoneURLIMAGE from './images/beacons/eddystoneURL.png';
import eddystoneTLMIMAGE from './images/beacons/eddystone_TLM.png';
import eddystoneUIDIMAGE from './images/beacons/eddystone_UID.png';
// #endregion
// #region flow types
type DetectedBeacon = {
identifier: string,
uuid?: string,
major?: number,
minor?: number,
proximity?: string,
rssi?: string,
distance?: number
};
type Section = {
key: number,
data: Array<DetectedBeacon>,
title: string,
sectionId: string
};
type Props = any;
type State = {
// region information
uuid?: string,
identifier: string,
// all detected beacons:
beacons: Array<Section>
};
// #endregion
// #region constants
// const UUID = '7b44b47b-52a1-5381-90c2-f09b6838c5d4';
const IDENTIFIER = '123456';
const TIME_FORMAT = 'MM/DD/YYYY HH:mm:ss';
const RANGING_TITLE = 'ranging beacons in the area:';
const RANGING_SECTION_ID = 1;
const MONITORING_ENTER_TITLE = 'monitoring enter information:';
const MONITORING_ENTER_SECTION_ID = 2;
const MONITORING_LEAVE_TITLE = 'monitoring exit information:';
const MONITORING_LEAVE_SECTION_ID = 3;
// #endregion
class BeaconsDemo extends Component<Props, State> {
// will be set as a reference to "beaconsDidRange" event:
beaconsDidRangeEvent = null;
// will be set as a reference to "regionDidEnter" event:
beaconsDidEnterEvent = null;
// will be set as a reference to "regionDidExit" event:
beaconsDidLeaveEvent = null;
// will be set as a reference to service did connect event:
beaconsServiceDidConnect: any = null;
state = {
// region information
uuid: null,// UUID,
identifier: IDENTIFIER,
// all detected beacons:
beacons: [
{key: 1, data: [], title: RANGING_TITLE, sectionId: RANGING_SECTION_ID},
{key: 2, data: [], title: MONITORING_ENTER_TITLE, sectionId: MONITORING_ENTER_SECTION_ID},
{key: 3, data: [], title: MONITORING_LEAVE_TITLE, sectionId: MONITORING_LEAVE_SECTION_ID}
]
};
componentWillMount() {
//
// ONLY non component state aware here in componentWillMount
//
// Beacons.addParsersListToDetection([
// Beacons.PARSER_IBEACON,
// Beacons.PARSER_ALTBEACON,
// Beacons.PARSER_EDDYSTONE_TLM,
// Beacons.PARSER_EDDYSTONE_UID,
// Beacons.PARSER_EDDYSTONE_URL
// ])
// start iBeacon detection
Beacons.addIBeaconsDetection()
.then(() => Beacons.addEddystoneUIDDetection())
.then(() => Beacons.addEddystoneURLDetection())
.then(() => Beacons.addEddystoneTLMDetection())
.then(() => Beacons.addAltBeaconsDetection())
.then(() => Beacons.addEstimotesDetection())
.catch(error => console.log(`something went wrong during initialization: ${error}`));
}
componentDidMount() {
//
// component state aware here - attach events
//
// we need to wait for service connection to ensure synchronization:
this.beaconsServiceDidConnect = Beacons.BeaconsEventEmitter.addListener(
'beaconServiceConnected',
() => {
console.log('service connected');
this.startRangingAndMonitoring();
}
);
// Ranging: Listen for beacon changes
this.beaconsDidRangeEvent = Beacons.BeaconsEventEmitter.addListener(
'beaconsDidRange',
(response: {beacons: Array<{distance: number, proximity: string, rssi: string, uuid: string}>, uuid: string, indetifier: string}) => {
console.log('BEACONS: ', response);
response.beacons.forEach(
beacon => this.updateBeaconState(
RANGING_SECTION_ID,
{
identifier: response.identifier,
uuid: String(beacon.uuid),
major: parseInt(beacon.major, 10) >= 0 ? beacon.major : '',
minor: parseInt(beacon.minor, 10) >= 0 ? beacon.minor : '',
proximity: beacon.proximity ? beacon.proximity : '',
rssi: beacon.rssi ? beacon.rssi : '',
distance: beacon.distance ? beacon.distance : ''
}
)
);
}
);
// monitoring:
this.beaconsDidEnterEvent = Beacons.BeaconsEventEmitter.addListener(
'regionDidEnter',
({ identifier, uuid, minor, major }) => {
console.log('regionDidEnter: ', { identifier, uuid, minor, major });
this.updateBeaconState(MONITORING_ENTER_SECTION_ID, { identifier, uuid, minor, major });
}
);
this.beaconsDidLeaveEvent = Beacons.BeaconsEventEmitter.addListener(
'regionDidExit',
({ identifier, uuid, minor, major }) => {
console.log('regionDidExit: ', { identifier, uuid, minor, major });
this.updateBeaconState(MONITORING_LEAVE_SECTION_ID, { identifier, uuid, minor, major });
}
);
}
componentWillUnMount() {
this.stopRangingAndMonitoring();
// remove monitiring events we registered at componentDidMount::
this.beaconsDidEnterEvent.remove();
this.beaconsDidLeaveEvent.remove();
// remove ranging event we registered at componentDidMount:
this.beaconsDidRangeEvent.remove();
}
render() {
const { beacons } = this.state;
console.log('beacons: ', beacons);
return (
<Image
style={styles.backgroundImage}
resizeMode="center"
source={require('./bluetooth-300-300-opacity-45.png')}
>
<View
style={styles.container}
>
<View style={styles.actionsContainer}>
<TouchableHighlight
style={styles.actionButton}
onPress={this.handlesOnRemoveIbeacon}
>
<Text style={styles.actionText}>
remove IBeacon detection
</Text>
</TouchableHighlight>
<TouchableHighlight
style={styles.actionButton}
onPress={this.handlesOnAddIbeacon}
>
<Text style={styles.actionText}>
add IBeacon detection
</Text>
</TouchableHighlight>
</View>
<SectionList
sections={beacons}
keyExtractor={this.sectionListKeyExtractor}
renderSectionHeader={this.renderHeader}
renderItem={this.renderRow}
ListEmptyComponent={this.renderEmpty}
// SectionSeparatorComponent={this.renderSeparator}
ItemSeparatorComponent={this.renderSeparator}
// shouldItemUpdate={this.shouldItemUpdate}
/>
</View>
</Image>
);
}
sectionListKeyExtractor = (
item: DetectedBeacon,
index: number
) => {
const UUID = item.uuid ? item.uuid : 'NONE';
const ID = item.identifier ? item.identifier : 'NONE';
return `${UUID}-${ID}`;
}
renderSeparator= () => (<View style={{ height: 1, backgroundColor: '#E1E1E1', marginLeft: 80 }} />);
updateBeaconState = (
forSectionId: number = 0, // section identifier
{identifier, uuid, minor, major, ...rest} // beacon
) => {
const { beacons } = this.state;
const time = moment().format(TIME_FORMAT);
const updatedBeacons = beacons.map(
beacon => {
if (beacon.sectionId === forSectionId) {
const sameBeacon = data => !((data.UUID === uuid) && (data.identifier === identifier) && (data.minor === minor) && (data.major === major));
const updatedData = [].concat(...beacon.data.filter(sameBeacon), { identifier, uuid, minor, major, time, ...rest });
return { ...beacon, data: updatedData };
}
return beacon;
}
);
this.setState({ beacons: updatedBeacons });
}
renderHeader = (
{ section }
) => (
<Text style={styles.headline}>
{section.title}
</Text>
);
renderRow = (
{ item }
) => {
console.log('rowData: ', item);
return (
<View style={styles.row}>
<View style={styles.iconContainer}>
<Avatar
medium
rounded
source={beaconIMAGE}
onPress={() => console.log('no use')}
activeOpacity={0.7}
/>
</View>
<View style={styles.infoContainer}>
<Text style={styles.smallText}>
indentifier: {item.identifier ? item.identifier : 'NA'}
</Text>
<Text style={styles.smallText}>
UUID: {item.uuid ? item.uuid : 'NA'}
</Text>
<View style={styles.majorMinorContainer}>
<Text style={styles.smallText}>
Major: {parseInt(item.major, 10) >= 0 ? item.major : 'NA'}
</Text>
<Text style={[styles.smallText, {marginLeft: 10}]}>
Minor: {parseInt(item.minor, 10) >= 0 ? item.minor : 'NA'}
</Text>
<Text style={[styles.smallText, {marginLeft: 10}]}>
RSSI: {item.rssi ? item.rssi : 'NA'}
</Text>
</View>
<Text style={styles.smallText}>
Proximity: {item.proximity ? item.proximity : 'NA'}
</Text>
<Text style={styles.smallText}>
Distance: {item.distance ? item.distance.toFixed(2) : 'NA'}
</Text>
</View>
</View>
);
}
renderEmpty = () => (
<View style={{ height: 40, alignItems: 'center', justifyContent: 'center' }}>
<Text>
no beacon detected
</Text>
</View>
);
startRangingAndMonitoring = async () => {
const { identifier, uuid} = this.state;
const region = { identifier, uuid }; // minor and major are null here
try {
await Beacons.startRangingBeaconsInRegion(region);
console.log('Beacons ranging started successfully');
await Beacons.startMonitoringForRegion(region);
console.log('Beacons monitoring started successfully');
} catch (error) {
throw error;
}
}
stopRangingAndMonitoring = async () => {
const { identifier, uuid} = this.state;
const region = { identifier, uuid }; // minor and major are null here
try {
await Beacons.stopRangingBeaconsInRegion(region);
console.log('Beacons ranging stopped successfully');
await Beacons.stopMonitoringForRegion(region);
console.log('Beacons monitoring stopped successfully');
} catch (error) {
throw error;
}
}
handlesOnAddIbeacon = async () => {
try {
await this.stopRangingAndMonitoring();
await Beacons.addIBeaconsDetection();
await this.startRangingAndMonitoring();
ToastAndroid.showWithGravity('add IBeacon detection', ToastAndroid.SHORT, ToastAndroid.CENTER);
} catch (error) {
ToastAndroid.show(`Error: add IBeacon detection failed: ${error.message}`, ToastAndroid.SHORT);
}
}
handlesOnRemoveIbeacon = async () => {
try {
await this.stopRangingAndMonitoring();
await Beacons.removeIBeaconsDetection();
await this.startRangingAndMonitoring();
ToastAndroid.showWithGravity('removed IBeacon detection', ToastAndroid.SHORT, ToastAndroid.CENTER);
} catch (error) {
ToastAndroid.show(`Error: remove IBeacon detection failed: ${error.message}`, ToastAndroid.SHORT);
}
}
}
const styles = StyleSheet.create({
backgroundImage: {
flex: 1,
width: null,
height: null,
},
container: {
flex: 1
},
btleConnectionStatus: {
// fontSize: 20,
// paddingTop: 20
},
headline: {
fontSize: 20,
marginHorizontal: 5
},
row: {
flexDirection: 'row',
padding: 8,
paddingBottom: 16
},
iconContainer: {
flexDirection: 'column',
marginRight: 10
},
infoContainer: {
flex: 1,
flexDirection: 'column',
},
majorMinorContainer: {
flexDirection: 'row'
},
smallText: {
fontSize: 11
},
actionsContainer: {
marginVertical: 10,
marginHorizontal: 5,
flexDirection: 'row',
alignItems: 'center',
justifyContent: 'space-between'
},
actionButton: {
width: 160,
backgroundColor: '#A6A6A6',
paddingHorizontal: 5,
paddingVertical: 10,
},
actionText: {
alignSelf: 'center',
fontSize: 11,
color: '#F1F1F1'
}
});
AppRegistry.registerComponent(
'BeaconsDemo',
() => BeaconsDemo
);