-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ios.js
156 lines (137 loc) · 4.72 KB
/
index.ios.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
import React from 'react'
import { View, Text, AppRegistry, ListView, DeviceEventEmitter } from 'react-native'
import Beacons from 'react-native-beacons-manager'
const TIME_FORMAT = 'MM/DD/YYYY HH:mm:ss';
class HelloBeacons extends React.Component {
constructor(props) {
super(props);
this.state = {
// region information
uuid: '45db6ada-890d-486d-abee-266396d8aaa6',
identifier: 'test',
regionEnterDatasource: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows([]),
regionExitDatasource: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows([])
};
}
componentWillMount(){
const { identifier, uuid } = this.state;
//
// ONLY non component state aware here in componentWillMount
//
// OPTIONAL: listen to authorization change
DeviceEventEmitter.addListener(
'authorizationStatusDidChange',
(info) => console.log('authorizationStatusDidChange: ', info)
);
// MANDATORY: you have to request ALWAYS Authorization (not only when in use) when monitoring
// you also have to add "Privacy - Location Always Usage Description" in your "Info.plist" file
// otherwise monitoring won't work
Beacons.requestAlwaysAuthorization();
// Define a region which can be identifier + uuid,
// identifier + uuid + major or identifier + uuid + major + minor
// (minor and major properties are numbers)
const region = { identifier, uuid };
// Monitor beacons inside the region
Beacons.startMonitoringForRegion(region);
// update location to ba able to monitor:
Beacons.startUpdatingLocation();
}
componentDidMount() {
//
// component state aware here - attach events
//
// monitoring:
DeviceEventEmitter.addListener(
'regionDidEnter',
(data) => {
console.log('monitoring - regionDidEnter data: ', data);
const time = TIME_FORMAT
this.setState({ regionEnterDatasource: this.state.rangingDataSource.cloneWithRows([{ identifier:data.identifier, uuid:data.uuid, minor:data.minor, major:data.major, time }]) });
}
);
DeviceEventEmitter.addListener(
'regionDidExit',
({ identifier, uuid, minor, major }) => {
console.log('monitoring - regionDidExit data: ', { identifier, uuid, minor, major });
const time = TIME_FORMAT
this.setState({ regionExitDatasource: this.state.rangingDataSource.cloneWithRows([{ identifier, uuid, minor, major, time }]) });
}
);
}
componentWillUnMount() {
// stop monitoring beacons:
Beacons.stopMonitoringForRegion();
// stop updating locationManager:
Beacons.stopUpdatingLocation();
// remove beacons events we registered at componentDidMount
DeviceEventEmitter.removeListener('regionDidEnter');
DeviceEventEmitter.removeListener('regionDidExit');
}
render() {
const { bluetoothState, regionEnterDatasource, regionExitDatasource } = this.state;
return (
<View style={styles.container}>
<Text style={styles.headline}>
monitoring enter information:
</Text>
<ListView
dataSource={ regionEnterDatasource }
enableEmptySections={ true }
renderRow={this.renderMonitoringEnterRow}
/>
<Text style={styles.headline}>
monitoring exit information:
</Text>
<ListView
dataSource={ regionExitDatasource }
enableEmptySections={ true }
renderRow={this.renderMonitoringLeaveRow}
/>
</View>
);
}
renderMonitoringEnterRow = ({ identifier, uuid, minor, major, time }) => {
return (
<View style={styles.row}>
<Text style={styles.smallText}>
Identifier: {identifier ? identifier : 'NA'}
</Text>
<Text style={styles.smallText}>
UUID: {uuid ? uuid : 'NA'}
</Text>
<Text style={styles.smallText}>
Major: {major ? major : ''}
</Text>
<Text style={styles.smallText}>
Minor: { minor ? minor : ''}
</Text>
<Text style={styles.smallText}>
time: { time ? time : 'NA'}
</Text>
</View>
);
}
renderMonitoringLeaveRow = ({ identifier, uuid, minor, major, time }) => {
return (
<View style={styles.row}>
<Text style={styles.smallText}>
Identifier: {identifier ? identifier : 'NA'}
</Text>
<Text style={styles.smallText}>
UUID: {uuid ? uuid : 'NA'}
</Text>
<Text style={styles.smallText}>
Major: {major ? major : ''}
</Text>
<Text style={styles.smallText}>
Minor: { minor ? minor : ''}
</Text>
<Text style={styles.smallText}>
time: { time ? time : 'NA'}
</Text>
</View>
);
}
}
const styles = {}
AppRegistry.registerComponent('HelloBeacons', () => HelloBeacons)