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

Cannot Update Distances of Multiple kBeacons at the Same Time

我现在正在尝试不断更新检测到的 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. */  }

}

这里是MyBeaconclass。

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

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

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

Beacon.setHardwareEqualityEnforced(true);