无法同时更新多个kBeacons的距离

时间:2016-06-22 04:51:19

标签: android ibeacon mac-address altbeacon android-ibeacon

我现在正在尝试不断更新检测到的kBeacons的距离,并使用3 kBeacons进行测试。我使用Android Beacon Library来检测信标,但它在每个public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region)回拨中为整个信标集合提供了相同的信标。

06-22 12:39:26.740 20106-20164/hk.bds.ibeaconlocator D/MyBeacon: BC:6A:29:25:14:01
06-22 12:39:26.740 20106-20164/hk.bds.ibeaconlocator D/MyBeacon: BC:6A:29:25:14:01
06-22 12:39:26.740 20106-20164/hk.bds.ibeaconlocator D/MyBeacon: BC:6A:29:25:14:01
06-22 12:39:27.845 20106-20175/hk.bds.ibeaconlocator D/MyBeacon: BC:6A:29:27:A4:2D
06-22 12:39:27.845 20106-20175/hk.bds.ibeaconlocator D/MyBeacon: BC:6A:29:27:A4:2D
06-22 12:39:27.845 20106-20175/hk.bds.ibeaconlocator D/MyBeacon: BC:6A:29:27:A4:2D
06-22 12:39:28.955 20106-20193/hk.bds.ibeaconlocator D/MyBeacon: BC:6A:29:24:E1:DC
06-22 12:39:28.955 20106-20193/hk.bds.ibeaconlocator D/MyBeacon: BC:6A:29:24:E1:DC
06-22 12:39:28.955 20106-20193/hk.bds.ibeaconlocator D/MyBeacon: BC:6A:29:24:E1:DC
06-22 12:39:30.065 20106-20210/hk.bds.ibeaconlocator D/MyBeacon: BC:6A:29:24:E4:78
06-22 12:39:30.065 20106-20210/hk.bds.ibeaconlocator D/MyBeacon: BC:6A:29:24:E4:78
06-22 12:39:30.065 20106-20210/hk.bds.ibeaconlocator D/MyBeacon: BC:6A:29:24:E4:78

我希望它应该在回调时给我3个不同的mac地址,但它在下一个回调中给我一个不同的mac地址。怎么解决?

以下显示了我的程序。 b1b2b3MyBeacon的对象。

@Override
public void onBeaconServiceConnect() {
    //This method will be called when the Beacon Manager is binded.
    beaconManager.setRangeNotifier( new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            // This method will be executed many times according to the size of the refresh interval.
            // Try to update the distance of the devices
            b1.updateDistance(beacons);
            b2.updateDistance(beacons);
            b3.updateDistance(beacons);
            theNear = getMinOne(getMinOne(b1, b2), b3);

            // Update Displays
            updateDisplay();
        }
    });

    try {
        // Tells the BeaconService to start looking for beacons that match the passed Region object,
        // and providing updates on the estimated mDistance every seconds while beacons in the Region are visible.
        beaconManager.startRangingBeaconsInRegion(new Region("myRangingUniqueId", null, null, null));
    } catch (RemoteException e) {  /* Error is detected. */  }

}

这是MyBeacon课程。

class MyBeacon {
    private static final String TAG = "MyBeacon";
    public String name;
    public String macAddress;
    public double distance = 0d; // initially the distance is 0.
    //public double predistance = 0d;

    // Display reference pointers.
    public TextView displayName;
    public TextView displayDistance;


    public MyBeacon(String _name, String _macAddress, TextView _displayName, TextView _displayDistance) {
            name = _name;
            macAddress = _macAddress;
            displayName = _displayName;
            displayDistance = _displayDistance;
            displayName.setText(name + " :  ");
        }

    public boolean updateDistance(Beacon _beacon) {
        if (_beacon.getBluetoothAddress().equals(this.macAddress)) {
            distance = _beacon.getDistance(); // Calculate the distance based on RSSI.
            return true;
        } else
            return false;
    }

    public void updateDistance(Collection<Beacon> beacons) {
        for (Beacon theBeacon : beacons) {
            Log.d(TAG, theBeacon.getBluetoothAddress());
            if (updateDistance(theBeacon))
                return;
        }
        distance = 0d;

    }

    public void updateDisplayDistance() {
        String str;
        if (distance == 0d)
            str = "UNDETECTED";
        else
            str = String.format("%.4f", distance);
        displayDistance.setText(str);
    }

    public String toString() {
        String str = "";
        str += "\n===================================";
        str += "\nBeacon Name: " + this.name;
        str += "\nMac Address: " + this.macAddress;
        str += "\nDistance   : " + this.distance;
        str += "\n===================================";
        return str;
    }
}

1 个答案:

答案 0 :(得分:1)

默认情况下,Android Beacon Library会将多个硬件信标的测距结果与不同的Mac地址结合起来,如果他们有 Same Identifier 。我怀疑这就是这里发生的事情 - 如果所有信标都有相同的标识符,你会看到这样的行为。

要解决此问题,您可以轻松配置库,以便为每个单独的信标Mac地址返回不同的测距结果,即使信标标识符相同也是如此。为此,只需添加以下代码行:

Beacon.setHardwareEqualityEnforced(true);