Android Altbeacon库:每秒刷新RSSI一次以上

Android Altbeacon库:每秒刷新RSSI一次以上,android,bluetooth-lowenergy,ibeacon,altbeacon,Android,Bluetooth Lowenergy,Ibeacon,Altbeacon,免责声明:android编程新手 大家好,, 我正在尝试设计一个应用程序,使用altbeacon库跟踪智能手机的位置。我使用的信标设置为每100ms播发一次(不可连接的播发),因此,由于默认情况下库扫描1.1s,因此可能会收到多个播发数据包。相反,我在日志中每次扫描只接收一个数据包,因此RSSI的刷新速度相当慢(每秒一个)。使用默认的android库(设置扫描模式低延迟),我可以连续访问ble回调,在一秒钟内多次更新RSSI 是否可以对Altbeacon库执行相同的操作?我的示例应用程序如下所示

免责声明:android编程新手

大家好,, 我正在尝试设计一个应用程序,使用altbeacon库跟踪智能手机的位置。我使用的信标设置为每100ms播发一次(不可连接的播发),因此,由于默认情况下库扫描1.1s,因此可能会收到多个播发数据包。相反,我在日志中每次扫描只接收一个数据包,因此RSSI的刷新速度相当慢(每秒一个)。使用默认的android库(设置扫描模式低延迟),我可以连续访问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) {    }
}
public类MainActivity扩展AppCompatActivity实现BeaconConsumer{
受保护的静态最终字符串TAG=“info”;
私人BeaconManager BeaconManager;
私有HashSet beaconseen=新HashSet();
地区地区1=新地区(“地区1”,“xx:xx:xx:xx:xx:xx”);
地区地区2=新地区(“地区2”,“xx:xx:xx:xx:xx:xx”);
地区地区3=新地区(“地区3”,“xx:xx:xx:xx:xx:xx”);
region4=新区域(“region4”,“xx:xx:xx:xx:xx:xx”);
地区地区5=新地区(“地区5”,“xx:xx:xx:xx:xx:xx”);
region6=新区域(“region6”,“xx:xx:xx:xx:xx:xx”);
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RangedBeacon.setSampleExpirationMilliconds(5000);//在5秒内收集信号测量值,抛出前10%和后10%的
//测量,并对其余的进行平均
beaconManager=beaconManager.getInstanceForApplication(此应用程序);
//要检测专有信标,您必须添加一条与您的信标对应的线,如下所示
//键入。在web上搜索“setBeaconLayout”以获得正确的表达式。
beaconManager.GetBeaconParser().add(新建BeaconParser()。
立根布置(“m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24”);
beaconManager.bind(这个);
}
@凌驾
受保护的空onDestroy(){
super.ondestory();
beaconManager.unbind(此);
}
@凌驾
beacerviceconnect()上的公共无效{
beaconManager.addRangeNotifier(新的RangeNotifier(){
@凌驾
公共无效范围信标区域(收集信标,区域){
用于(信标:信标){
Log.i(标记“我看到一个信标”+beacon.getBluetoothAddress()+”和rssi:“+beacon.getRssi()+”dBm about”+beacons.iterator().next().getDistance()+“米远”);
}
}
});
试一试{
StarTrangBeaconRegion(区域1)的Beacon经理;
StarTrangBeaconRegion(区域2)的Beacon经理;
StarTrangBeaconRegion(区域3)的Beacon经理;
StarTrangBeaconRegion(区域4)的Beacon经理;
StarTrangBeaconRegion(区域5)的Beacon经理;
StarTrangBeaconRegion(区域6)的Beacon经理;
}捕获(远程异常e){}
}
我的手机是运行安卓5.0.2的小米Redmi Note 2


谢谢。

该库设计为每个测距周期只向
DidRangeBeanConsincin区域发送一次回调(默认情况下基于1.1秒的扫描)。无论收到多少数据包,这都是正确的,并且各个RSSI读数都会在内部和外部进行跟踪

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

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

您好,我已经尝试过使用该代码,但我忘了在问题中编写该代码。不幸的是,当我将扫描周期设置为200(例如110毫秒)以下时,应用程序会丢失大量数据包,有时甚至智能手机的蓝牙也会崩溃(我需要重新启动手机以使其再次工作)。您认为“RSSI读数在内部跟踪”是什么意思另外,我还有一个问题:我读了你的一些回答,你说不要减少信标的广告间隔,以便接收更多的统计样本;但是如果每1.1秒调用一次回调,我每次回调都会收到一个新的RSSI,浪费其他的。我说得对吗?那么我不能降低广告间隔吗要节省信标的电池?谢谢。听起来你设备上的蓝牙堆栈无法处理如此频繁的重新启动扫描,这很不幸。这是Android 4.x吗?如果Android 5.x扫描API可用,那么库将不需要在每个周期停止并重新启动扫描。库内部跟踪运行平均值RSSI用于距离估计,此运行平均值考虑了单个测距周期内检测到的所有数据包(包括多个数据包)的所有读数。它存储在名为
mRunningAverageRssi
Beacon的私有成员变量中。好的,谢谢您的解释!我的手机是运行ndroid 5.0.2。