gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 01:15:08 26 4
gpt4 key购买 nike

我现在正尝试不断更新检测到的 kBeacon 的距离,我使用 3 kBeacon 进行测试。我用了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 地址。如何解决?

下面向您展示了有关我的程序的一些信息。 b1 , b2 , b3MyBeacon 的对象.

@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)。

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 Identifier,将合并来自具有不同 Mac 地址的多个硬件信标的测距结果。我怀疑这就是这里发生的事情——如果所有信标都具有相同的标识符,您会看到这样的行为。

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

Beacon.setHardwareEqualityEnforced(true);

关于android - 无法同时更新多个 kBeacon 的距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37958801/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com