按信标长度对数组进行排序
Sorting array by beacon streight
我有几个好玩的信标。我想按强度 rssi 列出它们。所以基本上他们会改变名单上的位置,这取决于在一段时间内(例如每 10 秒)与信标的距离接收器。
有什么更好的方法,使用 sqllite 并保存地址信标和直接 rssi,然后对它们进行排序,或者在某个数组中执行此操作?
有什么方法可以对数组中的项目进行排序,在我的示例中,信标地址取决于 rssi?
使用数组,因为数据的生命周期很短,因此数据库开销不值得。然后使用 Collections.sort(List, Comparator)
其中 Comparator
比较单个强度。要每 10 秒 运行 使用 Handler
的 postDelayed
.
使用 Android Beacon Library,您可以使用如下代码按 RSSI 对远程信标进行排序:
public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {
ArrayList<Beacon> sortedBeacons = new ArrayList<Beacon>(beacons);
Collections.sort(sortedBeacons, new Comparator<Beacon>() {
@Override
public int compare(Beacon beacon0, Beacon beacon1) {
return new Integer(beacon0.getRssi()).compareTo(new Integer(beacon1.getRssi()));
}
});
// Add code here to update display with sortedBeacons
}
我有几个好玩的信标。我想按强度 rssi 列出它们。所以基本上他们会改变名单上的位置,这取决于在一段时间内(例如每 10 秒)与信标的距离接收器。 有什么更好的方法,使用 sqllite 并保存地址信标和直接 rssi,然后对它们进行排序,或者在某个数组中执行此操作? 有什么方法可以对数组中的项目进行排序,在我的示例中,信标地址取决于 rssi?
使用数组,因为数据的生命周期很短,因此数据库开销不值得。然后使用 Collections.sort(List, Comparator)
其中 Comparator
比较单个强度。要每 10 秒 运行 使用 Handler
的 postDelayed
.
使用 Android Beacon Library,您可以使用如下代码按 RSSI 对远程信标进行排序:
public void didRangeBeaconsInRegion(final Collection<Beacon> beacons, Region region) {
ArrayList<Beacon> sortedBeacons = new ArrayList<Beacon>(beacons);
Collections.sort(sortedBeacons, new Comparator<Beacon>() {
@Override
public int compare(Beacon beacon0, Beacon beacon1) {
return new Integer(beacon0.getRssi()).compareTo(new Integer(beacon1.getRssi()));
}
});
// Add code here to update display with sortedBeacons
}