使用Altbeacon库监测iBeacons

5

我正在测试AltBeacon的Android Beacon Library。 http://altbeacon.github.io/android-beacon-library/index.html

我只监视一个“通用”区域,仅设置第一个ID(UUID),并将id2和id3留空。

Region region = new Region(uuid, Identifier.parse(uuid), null, null);

我收到了didEnterRegion事件,但是我有一个问题。在didEnterRegion事件中,我接收到了区域(Region)作为参数,但是我能否知道具体启动该事件的信标?我想知道启动此事件区域的信标的id1、id2和id3,这可能吗?
谢谢。
1个回答

9
如果您需要了解检测到的特定信标的标识符,只需使用范围API即可。您将获得一个回调,其中包含包含标识符的Beacon对象。
beaconManager.setRangeNotifier(this);
beaconManager.startRangingBeaconsInRegion(region);
...

public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        for (Beacon beacon: beacons) {
            Log.i(TAG, "Beacon detected with id1: "+beacon.getId1()+" id2:"+beacon.getId2()+" id3: "+beacon.getId3());     
        }
}

谢谢您的回复,但我不想每次监控时都进行范围设置。我认为如果这样做,会耗尽电池。 - OverMind
好消息!在进行范围测量时,您无需担心使用额外的电池。无论您是否进行范围测量,都会进行相同的无线电扫描,因此范围测量是读取信标标识符的正确方式。与监视与范围测量无关的问题出现在扫描频率上。默认情况下,当您的应用程序处于前台时,库会不断进行扫描,并且当您的应用程序处于后台时,每五分钟进行一次扫描。有关详细信息,请参见此处:http://altbeacon.github.io/android-beacon-library/battery_manager.html - davidgyoung
正如battery_manager文章所述,自Android 8及更高版本以来,这具有一些限制。 文章中还提到了另一种选择,为了更快的访问,我在此处复制它:https://altbeacon.github.io/android-beacon-library/foreground-service.html - stefan.m

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接