【问题标题】:Altbeacon library: refresh RSSI more than once a secondAltbeacon 库:每秒多次刷新 RSSI
【发布时间】:2017-01-20 17:06:21
【问题描述】:

免责声明:android 编程新手。

大家好, 我正在尝试设计一个应用程序来使用 altbeacon 库跟踪智能手机的位置。我正在使用的信标设置为每 100 毫秒做一次广告(不可连接的广告),因此由于默认情况下库扫描 1.1 秒,因此预计会收到多个广告数据包。相反,我在日志中每次扫描只接收一个数据包,因此 RSSI 的刷新非常慢(每秒一个)。使用默认的 android 库(设置 SCAN_MODE_LOW_LATENCY),我可以连续访问 ble 回调,在一秒钟内多次更新 RSSI。

是否可以对 Altbeacon 库做同样的事情?我的示例应用程序如下所示:

public class MainActivity extends AppCompatActivity implements BeaconConsumer{


protected static final String TAG = "info";
private BeaconManager beaconManager;
private HashSet<Beacon> beaconsSeen = new HashSet<Beacon>();

Region region1 = new Region("region1", "xx:xx:xx:xx:xx:xx");
Region region2 = new Region("region2", "xx:xx:xx:xx:xx:xx");
Region region3 = new Region("region3", "xx:xx:xx:xx:xx:xx");
Region region4 = new Region("region4", "xx:xx:xx:xx:xx:xx");
Region region5 = new Region("region5", "xx:xx:xx:xx:xx:xx");
Region region6 = new Region("region6", "xx:xx:xx:xx:xx:xx");

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    RangedBeacon.setSampleExpirationMilliseconds(5000); //collects signal measurements over 5 seconds, throws out the top 10% and bottom 10% of
    // measurements, and averages the rest
    beaconManager = BeaconManager.getInstanceForApplication(this);
    // To detect proprietary beacons, you must add a line like below corresponding to your beacon
    // type.  Do a web search for "setBeaconLayout" to get the proper expression.
     beaconManager.getBeaconParsers().add(new BeaconParser().
            setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24"));
    beaconManager.bind(this);
}
@Override
protected void onDestroy() {
    super.onDestroy();
    beaconManager.unbind(this);
}
@Override
public void onBeaconServiceConnect() {
    beaconManager.addRangeNotifier(new RangeNotifier() {
        @Override
        public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
            for (Beacon beacon : beacons) {
                Log.i(TAG, "I see a beacon "+beacon.getBluetoothAddress()+" with rssi: "+beacon.getRssi()+" dBm about "+beacons.iterator().next().getDistance()+" meters away.");
            }
        }
    });

    try {
        beaconManager.startRangingBeaconsInRegion(region1);
        beaconManager.startRangingBeaconsInRegion(region2);
        beaconManager.startRangingBeaconsInRegion(region3);
        beaconManager.startRangingBeaconsInRegion(region4);
        beaconManager.startRangingBeaconsInRegion(region5);
        beaconManager.startRangingBeaconsInRegion(region6);
    } catch (RemoteException e) {    }
}

我的手机是运行 Android 5.0.2 的小米红米 Note 2。

谢谢。

【问题讨论】:

    标签: android bluetooth-lowenergy ibeacon altbeacon


    【解决方案1】:

    该库设计为每个测距周期仅向didRangeBeaconsInRegion 发送一个回调(默认情况下基于 1.1 秒扫描)。无论收到多少数据包都是如此,并且在内部跟踪各个 RSSI 读数并

    您可以通过减少扫描周期的长度来增加回调的频率。以下代码会将扫描周期更改为 100 毫秒,因此您将在该间隔内获得测距更新。 p>

    beaconManager.setForegroundScanPeriod(100l);
    beaconManager.updateScanPeriods();
    

    【讨论】:

    • 嗨,我已经尝试过使用该代码,但我忘记在问题中写了。不幸的是,当我将扫描周期设置为低于 200(例如 110 毫秒)时,应用程序会丢失很多数据包,有时甚至智能手机的蓝牙也会崩溃(我需要重新启动手机才能使其再次工作)。 “内部跟踪 RSSI 读数”是什么意思?
    • 另外我还有一个问题:我看了你的一些回答,你说不要为了接收更多的统计样本而减少信标的广告间隔;但是如果每 1.1 秒调用一次回调,每次回调我总是会收到一个新的 RSSI,而浪费了其他的。我对么?那么我不能降低广告间隔以节省信标的电池吗?谢谢。
    • 听起来您设备上的蓝牙堆栈无法经常处理重新启动扫描,这很不幸。这是安卓4.x吗?如果 Android 5.x 扫描 API 可用,则库不需要在每个周期停止和重新启动扫描。在内部,该库确实跟踪运行平均 RSSI 以进行距离估计,并且该运行平均值考虑了来自所有检测到的数据包的 所有 读数,包括单个测距周期中的多个数据包。这存储在名为mRunningAverageRssiBeacon 的私​​有成员变量中。
    • 好的,谢谢您的解释!我的手机是运行 Android 5.0.2 的小米红米 Note 2。
    猜你喜欢
    • 2022-07-06
    • 2013-04-21
    • 2016-03-21
    • 2012-06-13
    • 2014-06-01
    • 2016-01-23
    • 2014-10-16
    • 2023-04-11
    • 1970-01-01
    相关资源
    最近更新 更多