使用https://github.com/MacKentoch/react-native-beacons-manager
在iOS上可爱,但在Android上,在我开始测量信标之后,信标阵列中没有显示任何内容(我旁边有6个信标,它们都显示在iOS上)。
这就是我正在做的事情:
componentDidMount() {
// Start detecting all iBeacons in the nearby
Beacons.detectIBeacons();
Beacons.startRangingBeaconsInRegion('Estimotes', 'B9407F30-F5F8-466E-AFF9-25556B57FE6D').then((data)=>{
console.log(data);
}).catch((reason) => {
console.log(reason);
});
// Print a log of the detected iBeacons (1 per second)
DeviceEventEmitter.addListener('beaconsDidRange', (data) => {
console.log(data);
});
}
在我的控制台中,我明白了:
{beacons: Array(0), uuid: "b9407f30-f5f8-466e-aff9-25556b57fe6d", identifier: "Estimotes"}
我将Estimotes的UUID保留为默认值,因此这应该可行。使用三星Galaxy S8 +进行测试。我在这里做错了编码吗?我缺少Android上的其他权限吗?蓝牙和位置服务已开启。
答案 0 :(得分:4)
好吧,我明白了。较新版本的android需要额外的权限。在你的Manifest中,把这个人扔在那里:
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
....如果你正在使用react-native-kontaktio(这比react-native-beacons-manager imo更好),你还需要在<application>
部分的你的Manifest中抛出它:
<service android:name="com.kontakt.sdk.android.ble.service.ProximityService"/>
然后在你的app.js中你需要像()那样请求许可
import PermissionsAndroid
from 'react-native'
componentDidMount() {
try {
const granted = PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Location Permission',
'message': 'Activeev needs to access your location.'
}
)
console.log('here', granted);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("Location Permitted")
} else {
console.log("Location permission denied")
}
} catch (err) {
console.warn(err)
}
}
现在像魅力一样工作。希望这有助于其他人。
答案 1 :(得分:-1)
谢谢您的回答。确实有效。根据您的回答,以下是我的实现。
import React, { Component } from 'react';
import { View, DeviceEventEmitter, ListView , Text} from 'react-native';
import Beacons from 'react-native-beacons-manager';
import {PermissionsAndroid} from 'react-native'
export default class App extends Component {
async componentDidMount() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
'title': 'Location Permission',
'message': 'Activeev needs to access your location.'
}
)
console.log('here', granted);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("Location Permitted")
// Start detecting all iBeacons in the nearby
Beacons.detectIBeacons();
Beacons.startRangingBeaconsInRegion('test', '85d37dd8-a9dc-48a8-ab1c-b86fcb7a6a17').then((data)=>{
console.log(data);
})
.catch((reason) => {
console.log(reason);
});
// Print a log of the detected iBeacons (1 per second)
DeviceEventEmitter.addListener('beaconsDidRange', (data) => {
console.log(data);
});
} else {
console.log("Location permission denied")
}
}catch (err) {
console.warn(err)
}
}
render(){
return(
<View></View>
);
}
}