react-native-beacons-manager Android 上没有显示任何信标

react-native-beacons-manager Not showing any beacons on Android

使用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 的其他权限吗?蓝牙和定位服务已开启。

好的,我明白了。 android 的较新版本需要额外的权限。在你的清单中,把这个人扔进去:

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>

..... 如果你使用的是 react-native-kontaktio(在我看来比 react-native-beacons-manager 更好),你还需要将它放在 [=14= 的清单中] 部分:

<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)
    }
}

现在工作得很好。希望这可以帮助别人。

感谢您的回答。它绝对有效。根据您的回答,下面是我的实现。

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>
   );
 }

}