Altbeacon库:每秒刷新一次RSSI

时间:2017-01-20 17:06:21

标签: android bluetooth-lowenergy ibeacon altbeacon

免责声明: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的小米Redmi Note 2.

感谢。

1 个答案:

答案 0 :(得分:0)

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

您可以通过缩短扫描周期来增加回调频率。以下代码会将扫描周期更改为100ms,因此您可以在该时间间隔内获得测距更新。

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