【问题标题】:react-native-beacons-manager Not showing any beacons on Androidreact-native-beacons-manager 未在 Android 上显示任何信标
【发布时间】:2025-12-06 23:55:01
【问题描述】:

使用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 react-native ibeacon


    【解决方案1】:

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

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

    ....如果您使用 react-native-kontaktio(比 react-native-beacons-manager imo 更好),您还需要在 &lt;application&gt; 部分的清单中添加它:

    <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 { PermissionsAndroid } from 'react-native'
    【解决方案2】:

    感谢您的回答。它确实奏效了。根据您的回答,以下是我的实现。

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

    【讨论】: