【问题标题】:Failing to monitor beacon regions with "altbeacon" libary android无法使用“altbeacon”库 android 监控信标区域
【发布时间】:2019-08-14 13:47:39
【问题描述】:

我正在使用altbeacon libary 为我所在地区的状态变化运行回调。 Sample code from here ("Starting an App in the Background" section)。但是在尝试了我能想到的一切之后,仍然没有运气。我错过了什么?

我进入我的应用程序的所有内容是:

  • RegionApp: App started up
  • RegionApp: Got a didDetermineStateForRegion call, isInRegion: false

详情:

  • 尝试使用最新版本的库 org.altbeacon:android-beacon-library:2+ 和旧版本 'org.altbeacon:android-beacon-library:2.12.1'
  • 3 Kontakt.io 信标与 IBeacon 协议。 (固件 v4)
  • 尝试了不同的beaconLayout,我很确定这个是对的。
  • 通过进入应用设置授予权限。 Like this
  • compileSdkVersion: 28

编辑:使用测距示例代码我也看不到信标。但是使用 Kontakt 应用程序我可以。根据BeaconScope的信标之一:

f7826da6-4fa2-4e98-8024-bc5b71e0893e
id2: 29737
id3: 24354
power: -77 dBm
distance: 0.9 meters
rssi -65dBm
average rssi: -76.5 dBm
packets: 78
packes/sec: 1.4
detection rate: 100%
stabilized: true (sometimes false)
sample period: 53.9 secs

已尝试

  • Nokia 6.1 / Android 9
  • Samsung Note 9 / Android 9
  • Huawei GRA-L09 / Android 6

代码:

App.java

public class App extends Application implements BootstrapNotifier {
    private static final String TAG = "RegionApp";
    private RegionBootstrap regionBootstrap;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "App started up");
        BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);

        beaconManager.getBeaconParsers().add(new BeaconParser().

        setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
        // Also tried this: m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25;

        Region region = new Region("com.example.regionmonitor.boostrapRegion", null, null, null);
        // Also tried creating region with defining UUID, and changing uniqueId

        regionBootstrap = new RegionBootstrap(this, region);
    }

    @Override
    public void didDetermineStateForRegion(int arg0, Region arg1) {
        Boolean isInRegion = arg0 == 1;
        Log.d(TAG, "Got a didDetermineStateForRegion call, isInRegion: " + isInRegion.toString());
    }

    @Override
    public void didEnterRegion(Region arg0) {
        Log.d(TAG, "Got a didEnterRegion call");
    }

    @Override
    public void didExitRegion(Region arg0) {
        Log.d(TAG, "Got a didExitRegion call");
    }
}

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest
    xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.regionmonitor"
    >

    <uses-permission android:name="android.permission.BLUETOOTH" />
    <uses-permission android:name="android.permission.BLUETOOTH_ADMIN"/>
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />

    <application
        android:name="com.example.regionmonitor.App"
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:roundIcon="@mipmap/ic_launcher_round"
        android:label="@string/app_name"
        android:theme="@style/AppTheme"
        >

        <!-- Note:  the singleInstance below is important to keep two copies of your activity from getting launched on automatic startup -->
        <activity
            android:launchMode="singleInstance"
            android:name="com.example.regionmonitor.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

    </application>

</manifest>

MainActivity.java 应该是无关紧要的,但为了共享所有代码:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
}

【问题讨论】:

  • 您可以使用像BeaconScope 这样的现成信标定位工具查看您的信标吗?它说它看到了什么?
  • 是的,我可以通过我的设备使用 Kontakt.io 应用程序查看信标。如果我尝试使用 altbeacon 示例代码进行测距,我看不到它们。
  • Beaconscope:f7826da6-4fa2-4e98-8024-bc5b71e0893e id2:29737 id3:24354 功率:-77 dBm 接收统计距离:0.9 米 rssi -65dBm 平均 rssi:-76.5 dBm 数据包:78 包/秒:1.4 检测率:100% 稳定:真(有时是假)采样周期:53.9 秒

标签: android altbeacon ibeacon-android kontakt.io


【解决方案1】:

在 Android 6+ 上,将位置权限放在清单中已不再足够。您还必须动态地向用户请求许可。在用户授予权限之前,会阻止信标检测(以及所有蓝牙 LE 扫描)。

请参阅此处了解说明:https://altbeacon.github.io/android-beacon-library/requesting_permission.html

【讨论】:

  • 我试过了,但为了简单起见,我在这个 repo 中排除了它。相反,我通过转到应用程序设置来设置权限。这还不够吗?我没有浏览我的日志,也没有找到任何与权限相关的东西——我还假设如果这是问题所在,那里会有某种警告。请参阅屏幕截图:imgur.com/GnQdG8z(我还将在问题中包含此信息,因为它应该首先存在)
  • 试一试,不设置权限确实会记录异常。然而“didDetermineStateForRegion”仍然被调用,arg1 为 0。
【解决方案2】:

发布的代码适用于 Kontakt 信标(和 IBeacon),但您需要耐心等待。如果您等待足够长的时间,回调将按预期调用,but it might take a while

如果您正在诺基亚(像我一样)上进行测试,您可能想阅读this

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多