|
| 1 | + |
| 2 | +import Beacons from 'react-native-beacons-manager'; |
| 3 | +import moment from 'moment'; |
| 4 | + |
| 5 | +const TIME_FORMAT = 'MM/DD/YYYY hh:mm:ss'; |
| 6 | + |
| 7 | +class beaconMonitoringOnly extends Component { |
| 8 | + constructor(props) { |
| 9 | + super(props); |
| 10 | + |
| 11 | + this.state = { |
| 12 | + // region information |
| 13 | + uuid: '7b44b47b-52a1-5381-90c2-f09b6838c5d4', |
| 14 | + identifier: 'some id', |
| 15 | + |
| 16 | + regionEnterDatasource: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows([]), |
| 17 | + regionExitDatasource: new ListView.DataSource({rowHasChanged: (r1, r2) => r1 !== r2}).cloneWithRows([]) |
| 18 | + }; |
| 19 | + } |
| 20 | + |
| 21 | + componentWillMount(){ |
| 22 | + const { identifier, uuid } = this.state; |
| 23 | + // |
| 24 | + // ONLY non component state aware here in componentWillMount |
| 25 | + // |
| 26 | + |
| 27 | + // OPTIONAL: listen to authorization change |
| 28 | + DeviceEventEmitter.addListener( |
| 29 | + 'authorizationStatusDidChange', |
| 30 | + (info) => console.log('authorizationStatusDidChange: ', info) |
| 31 | + ); |
| 32 | + |
| 33 | + // MANDATORY: you have to request ALWAYS Authorization (not only when in use) when monitoring |
| 34 | + // you also have to add "Privacy - Location Always Usage Description" in your "Info.plist" file |
| 35 | + // otherwise monitoring won't work |
| 36 | + Beacons.requestAlwaysAuthorization(); |
| 37 | + |
| 38 | + // Define a region which can be identifier + uuid, |
| 39 | + // identifier + uuid + major or identifier + uuid + major + minor |
| 40 | + // (minor and major properties are numbers) |
| 41 | + const region = { identifier, uuid }; |
| 42 | + // Range for beacons inside the region |
| 43 | + Beacons.startMonitoringForRegion(region); |
| 44 | + // update location to ba able to monitor: |
| 45 | + Beacons.startUpdatingLocation(); |
| 46 | + } |
| 47 | + |
| 48 | + componentDidMount() { |
| 49 | + // |
| 50 | + // component state aware here - attach events |
| 51 | + // |
| 52 | + |
| 53 | + // monitoring: |
| 54 | + DeviceEventEmitter.addListener( |
| 55 | + 'regionDidEnter', |
| 56 | + (data) => { |
| 57 | + console.log('monitoring - regionDidEnter data: ', data); |
| 58 | + const time = moment().format(TIME_FORMAT); |
| 59 | + this.setState({ regionEnterDatasource: this.state.rangingDataSource.cloneWithRows([{ identifier:data.identifier, uuid:data.uuid, minor:data.minor, major:data.major, time }]) }); |
| 60 | + } |
| 61 | + ); |
| 62 | + |
| 63 | + DeviceEventEmitter.addListener( |
| 64 | + 'regionDidExit', |
| 65 | + ({ identifier, uuid, minor, major }) => { |
| 66 | + console.log('monitoring - regionDidExit data: ', { identifier, uuid, minor, major }); |
| 67 | + const time = moment().format(TIME_FORMAT); |
| 68 | + this.setState({ regionExitDatasource: this.state.rangingDataSource.cloneWithRows([{ identifier, uuid, minor, major, time }]) }); |
| 69 | + } |
| 70 | + ); |
| 71 | + } |
| 72 | + |
| 73 | + componentWillUnMount() { |
| 74 | + // stop monitoring beacons: |
| 75 | + Beacons.stopMonitoringForRegion(); |
| 76 | + // stop updating locationManager: |
| 77 | + Beacons.stopUpdatingLocation(); |
| 78 | + // remove all listeners in a row |
| 79 | + DeviceEventEmitter.remove(); |
| 80 | + } |
| 81 | + |
| 82 | + render() { |
| 83 | + const { bluetoothState, regionEnterDatasource, regionExitDatasource } = this.state; |
| 84 | + |
| 85 | + return ( |
| 86 | + <View style={styles.container}> |
| 87 | + <Text style={styles.headline}> |
| 88 | + monitoring enter information: |
| 89 | + </Text> |
| 90 | + <ListView |
| 91 | + dataSource={ regionEnterDatasource } |
| 92 | + enableEmptySections={ true } |
| 93 | + renderRow={this.renderMonitoringEnterRow} |
| 94 | + /> |
| 95 | + |
| 96 | + <Text style={styles.headline}> |
| 97 | + monitoring exit information: |
| 98 | + </Text> |
| 99 | + <ListView |
| 100 | + dataSource={ regionExitDatasource } |
| 101 | + enableEmptySections={ true } |
| 102 | + renderRow={this.renderMonitoringLeaveRow} |
| 103 | + /> |
| 104 | + </View> |
| 105 | + ); |
| 106 | + } |
| 107 | + |
| 108 | + renderMonitoringEnterRow = ({ identifier, uuid, minor, major, time }) => { |
| 109 | + return ( |
| 110 | + <View style={styles.row}> |
| 111 | + <Text style={styles.smallText}> |
| 112 | + Identifier: {identifier ? identifier : 'NA'} |
| 113 | + </Text> |
| 114 | + <Text style={styles.smallText}> |
| 115 | + UUID: {uuid ? uuid : 'NA'} |
| 116 | + </Text> |
| 117 | + <Text style={styles.smallText}> |
| 118 | + Major: {major ? major : ''} |
| 119 | + </Text> |
| 120 | + <Text style={styles.smallText}> |
| 121 | + Minor: { minor ? minor : ''} |
| 122 | + </Text> |
| 123 | + <Text style={styles.smallText}> |
| 124 | + time: { time ? time : 'NA'} |
| 125 | + </Text> |
| 126 | + </View> |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + renderMonitoringLeaveRow = ({ identifier, uuid, minor, major, time }) => { |
| 131 | + return ( |
| 132 | + <View style={styles.row}> |
| 133 | + <Text style={styles.smallText}> |
| 134 | + Identifier: {identifier ? identifier : 'NA'} |
| 135 | + </Text> |
| 136 | + <Text style={styles.smallText}> |
| 137 | + UUID: {uuid ? uuid : 'NA'} |
| 138 | + </Text> |
| 139 | + <Text style={styles.smallText}> |
| 140 | + Major: {major ? major : ''} |
| 141 | + </Text> |
| 142 | + <Text style={styles.smallText}> |
| 143 | + Minor: { minor ? minor : ''} |
| 144 | + </Text> |
| 145 | + <Text style={styles.smallText}> |
| 146 | + time: { time ? time : 'NA'} |
| 147 | + </Text> |
| 148 | + </View> |
| 149 | + ); |
| 150 | + } |
| 151 | +} |
0 commit comments