Altbeacon 库:每秒刷新 RSSI 不止一次

Altbeacon library: refresh RSSI more than once a second

免责声明:android 编程新手。

大家好, 我正在尝试设计一个应用程序来使用 altbeacon 库跟踪 smartphone 的位置。我使用的信标设置为每 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) {    }
}

我的 phone 是小米红米 Note 2 运行 Android 5.0.2.

谢谢。

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

您可以通过减少扫描周期的长度来增加回调的频率。以下代码会将扫描周期更改为 100 毫秒,因此您将在那个间隔。

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