npm install react-native-beacons-manager
import React, {
Component} from 'react';
import {
Text,
View,
DeviceEventEmitter,
FlatList,
StyleSheet,
PermissionsAndroid,
TouchableOpacity,
} from 'react-native';
import Beacons from 'react-native-beacons-manager';
export default class BeaconsManager extends Component {
constructor(props) {
super(props);
this.state = {
// region information
uuidRef: null,
identifier: 'TEST BEACON 1',
// React Native ListView datasource initialization
dataSource: [],
};
componentWillMount() {
// 告诉该库检测beacon
Beacons.detectIBeacons();
const {
identifier, uuid} = this.state;
const FBeacons = {
identifier, uuid};
Beacons.startRangingBeaconsInRegion(FBeacons)
.then(() => console.log('Beacons ranging started succesfully')) //启用成功
.catch(error =>
console.log(`Beacons ranging not started, error: ${
error}`), //启用失败
);
}
componentDidMount() {
// component state aware here - attach events
//获取周围的beacon数据
DeviceEventEmitter.addListener('beaconsDidRange', data => {