Ibeacon地区关闭了吗?

时间:2014-10-20 08:56:44

标签: ibeacon altbeacon

我们正在尝试使用Altbeacon库来满足下一个研究案例: 我们希望将几个IBeacons放在一个房间或走廊中,每个房间或走廊之间的距离不超过3米,我们希望根据扫描信标的用户电话获得当前最接近的Ibeacon。 我们首先尝试构建每个只有一个信标的区域,想知道一个区域是一个封闭的集合,这意味着当你进入一个区域时,你不能同时在其他区域,当你离开一个区域时,你输入下一个最近的一个。但这不是图书馆实施的方法。 我们想知道Altbeacon库中是否有任何方法可以应用我们的方法,或者是否必须使用某种补丁来满足我提交给您的研究案例。

1 个答案:

答案 0 :(得分:0)

实现这一目标的最简单方法是使用单个区域对所有信标进行测距,然后开始测距:

@Override
public void onBeaconServiceConnect() {
    try {
         // Set up a region that matches all of your beacons.  You may want to replace the first
         // null with a UUID that all your beacons share.

        Region allBeaconsRegion = new Region("all beacons", null, null, null);
        beaconManager.startRangingBeaconsInRegion(mAllBeaconsRegion);
        beaconManager.setRangeNotifier(this);    

    } catch (RemoteException e) {
        Log.e(TAG, "Cannot connect to beacon service");
    }
}

注意,如果您使用的是带有RegionBootstrap的自定义Application类,则可以将上面的代码放在didEnterRegion方法中,而不是onBeaconServiceConnect方法中。

一旦开始测距,您将获得每秒一次的回调,其中包含所有可见信标的列表。您可以添加代码以确定哪一个最接近:

@Override
public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region arg1) {
    Beacon closestBeacon = null;

    for (Beacon beacon : beacons) {
            if (closestBeacon == null) {
                closestBeacon = beacon;
            }
            else {
                if (closestBeacon.getDistance() > beacon.getDistance()) {
                    closestBeacon = beacon;
                }
            }
    }

    // Do Something with closestBeacon here        
}

请记住,最近的信标可能会因无线电噪声而来回变化,因此您可能需要添加额外的逻辑来防止最近的信标频繁来回翻转。

相关问题