AltBeacon对于OnyxBeacons不稳定,反复循环通过didEnterRegion和didExitRegion [英] AltBeacon unstable for OnyxBeacons, cycling through didEnterRegion and didExitRegion repeatedly

查看:84
本文介绍了AltBeacon对于OnyxBeacons不稳定,反复循环通过didEnterRegion和didExitRegion的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 AltBeacon 库构建应用程序以支持 玛瑙信标(信标一) < a href = https://store.gimbal.com/collections/beacons/products/s21 rel = nofollow>云台信标(系列21)



我的测试设备是带有Android 4.4.4 KitKat和Onyx Beacon的Nexus 7 2013。信标距离我的设备不超过一米,而我没有移动它。检测到信标并转到 didEnterRegion ,然后等待一分钟左右,然后转到 didExitRegion ,然后重复该循环。 / p>

我们还使用其他(Onyx)信标在其他设备(三星手机)上对此进行了测试。仍然出现问题。请注意,这仅在 玛瑙信标(信标一) 上发生。仅触发 云台信标(系列21)



我使用正确的信标解析器字符串吗?我能正确实现吗?我真的没有主意,我想同时支持两个信标。



BaseApplication

 公共类BaseApplication扩展了应用程序{

@Override
public void onCreate(){
super.onCreate();
startService(new Intent(this,BeaconMonitoringService.class));
}

}

BeaconMonitoringService

 公共类BeaconMonitoringService扩展了服务实现BeaconConsumer {

私有上下文上下文;

私人BeaconService服务;
private BeaconManager beaconManager;
private NotificationCompat.Builder notifBuilder;

@Override
public void onCreate(){
super.onCreate();
context = getBaseContext();
beaconManager = BeaconManager.getInstanceForApplication(this);
beaconManager.setBackgroundMode(true);

beaconManager.setBackgroundScanPeriod(1100l);
beaconManager.setBackgroundBetweenScanPeriod(15000l);

String [] beaconParsers = new String [] { m:2-3 = 0215,i:4-19,i:20-21,i:22-23,p:24-24 , m:2-3 = beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25, m:2-3 = 0203 ,i:14-19l,d:10-13,p:9-9};

for(String beacon:beaconParsers){
Log.i( BeaconService, layout: + beacon);
beaconManager.getBeaconParsers()。add(new BeaconParser()。setBeaconLayout(beacon));
}
beaconManager.bind(this);
}

@Override
public void onBeaconServiceConnect(){
beaconManager.setMonitorNotifier(new MonitorNotifier(){

私有字符串日志;

@Override
public void didEnterRegion(区域区域){
信标信标= service.getBeacon(region.getId1()。toString(),region.getId2()。toString (),region.getId3()。toString());
log =我第一次看到信标! + region.getId1()+ + region.getId2()+ + region.getId3();
Log.i( BeaconService,log);
sendNotification(beacon);
}

@Override
public void didExitRegion(Region region){
信标信标= service.getBeacon(region.getId1()。toString(),region.getId2()。toString(),region.getId3()。toString());
log =我不再看到信标;
Log.i( BeaconS ervice,日志);
}

@Override
public void didDetermineStateForRegion(int state,Region region){

}
});

List< BackendRegion> regionList = Util.getRegionList();
for(BackendRegion region:regionList){
ArrayList< Beacon> beaconList = region.getBeacons();
for(Beacon beacon:beaconList){
try {
字符串beaconString = region.getUuid()。toLowerCase()+ + beacon.getMajor()+ + beacon.getMinor ();
service.addBeacon(beacon);
beaconManager.startMonitoringBeaconsInRegion(新区域(beacon.getIdentifier(),Identifier.parse(region.getUuid()),Identifier.parse(beacon.getMajor()),Identifier.parse(beacon.getMinor())) );
} catch(RemoteException e){}
}
}

}


@Override
public IBinder onBind(Intent intent){
返回null;
}

@Override
public void onDestroy(){
beaconManager.unbind(this);
super.onDestroy();
}

private void sendNotification(Beacon beacon,InteractiveImage InteractiveImage){
this.notifBuilder = new NotificationCompat.Builder(this)
.setAutoCancel(true)
.setContentTitle( Sample Beacon)
.setContentText(检测到一个信标)
.setSmallIcon(R.drawable.ic_launcher);

Intent notifyIntent = new Intent(this,NotificationActivity.class);
notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

PendingIntentendingIntent = PendingIntent.getActivity(this,0,notifyIntent,0);
notifBuilder.setContentIntent(pendingIntent);

NotificationManager notificationManager =(NotificationManager)this.getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(Integer.parseInt(beacon.getId()),notifBuilder.build());

}

}

日志

  12-11 15:45:24.103 I / BeaconService:我第一次看到信标! 20cae8a0-a9cf-11e3-a5e2-0800200c9a66 7 44512 
12-11 15:45:24.784 D / BluetoothAdapter:stopLeScan()
12-11 15:45:39.698 D / BluetoothAdapter:startLeScan(): null
12-11 15:45:39.709 D / BluetoothAdapter:onClientRegistered()-status = 0 clientIf = 5
12-11 15:45:40.830 D / BluetoothAdapter:stopLeScan()
12-11 15:45:55.794 D / BluetoothAdapter:startLeScan():null
12-11 15:45:55.804 D / BluetoothAdapter:onClientRegistered()-status = 0 clientIf = 5
12-11 15:45:56.985 D / BluetoothAdapter:stopLeScan()
12-11 15:46:11.920 D / BluetoothAdapter:startLeScan():null
12-11 15:46:11.950 D / BluetoothAdapter:onClientRegistered ()-status = 0 clientIf = 5
12-11 15:46:13.091 D / BluetoothAdapter:stopLeScan()
12-11 15:46:27.996 D / BluetoothAdapter:startLeScan():null
12-11 15:46:28.016 D / BluetoothAdapter:onClientRegistered()-status = 0 clientIf = 5
12-11 15:46:29.147 D / BluetoothAdapter:stopLeScan()
12- 11 15:46:29.227 I / BeaconService:我不再看到信标
12-11 15:46:44.121 D / BluetoothAdapter:startLeScan():null
12-11 15:46:44.141 D / BluetoothAdapter:onClientRegistered()-status = 0 clientIf = 5
12-11 15:46:44.402 I / BeaconService:我只是第一次看到信标! 20cae8a0-a9cf-11e3-a5e2-0800200c9a66 7 44512
12-11 15:46:45.282 D / BluetoothAdapter:stopLeScan()
12-11 15:47:00.197 D / BluetoothAdapter:startLeScan(): null
12-11 15:47:00.197 D / BluetoothAdapter:onClientRegistered()-status = 0 clientIf = 5
12-11 15:47:01.328 D / BluetoothAdapter:stopLeScan()
12-11 15:47:16.343 D / BluetoothAdapter:startLeScan():null
12-11 15:47:16.363 D / BluetoothAdapter:onClientRegistered()-status = 0 clientIf = 5
12-11 15:47:17.514 D / BluetoothAdapter:stopLeScan()
12-11 15:47:32.418 D / BluetoothAdapter:startLeScan():null
12-11 15:47:32.428 D / BluetoothAdapter:onClientRegistered ()-status = 0 clientIf = 5
12-11 15:47:33.540 D / BluetoothAdapter:stopLeScan()
12-11 15:47:48.494 D / BluetoothAdapter:startLeScan():null
12-11 15:47:48.504 D / BluetoothAdapter:onClientRegistered()-status = 0 clientIf = 5
12-11 15:47:49.635 D / BluetoothAdapter:stopLeScan()
12- 11 15:48:04.620 D / BluetoothAdapter:startLeScan():空
12-11 15:48:04.630 D / BluetoothAdapter:onClientRegistered()-status = 0 clientIf = 5
12-11 15:48:05.771 D / BluetoothAdapter:stopLeScan()
12-11 15:48:20.706 D / BluetoothAdapter:startLeScan():null
12-11 15:48:20.706 D / BluetoothAdapter:onClientRegistered()-status = 0 clientIf = 5 $ If $ 5
12-11 15:48 :21.827 D / BluetoothAdapter:stopLeScan()
12-11 15:48:36.821 D / BluetoothAdapter:startLeScan():null
12-11 15:48:36.831 D / BluetoothAdapter:onClientRegistered()- status = 0 clientIf = 5
12-11 15:48:37.962 D / BluetoothAdapter:stopLeScan()
12-11 15:48:52.917 D / BluetoothAdapter:startLeScan():null
12-11 15:48:52.927 D / BluetoothAdapter:onClientRegistered()-status = 0 clientIf = 5
12-11 15:48:54.058 D / BluetoothAdapter:stopLeScan()
12-11 15: 49:09.003 D / BluetoothAdapter:startLeScan():null
12-11 15:49:09.003 D / BluetoothAdapter:onClientRegistered()-status = 0 clientIf = 5
12-11 15:49:10.154 D /蓝牙适配器:stopLeScan()
12-11 15:49:10.214 I / BeaconService:我不再看到信标


解决方案

一些提示:




  • 检查Onyx Beacon One的广告投放率。为获得最佳效果,信标应该投放广告以标准的10Hz一些制造商放慢了此广告投放速度,以节省电池寿命。如果您有一个以1Hz或更低频率进行广告的信标,则信标扫描程序可能会开始定期丢失它。 (由于蓝牙无线电噪声,未检测到100%的数据包。)


  • 如果发现广告速率为1 Hz或更低,则提高


  • 如果无法提高信标的广告率,则可以放慢 Android信标库 的扫描速度,以提供更多扫描速度是时候收集信标包了。默认情况下,它使用1.1秒的扫描周期。您可以使用下面的代码行将其减慢到5秒。这对于不经常发布广告的信标将更可靠地起作用,但也会使您对区域条目/出口的响应时间变慢。 beaconManager.setForegroundScanPeriod(5000l);



I am building an application using the AltBeacon library to support Onyx Beacons (Beacon One) and Gimbal Beacons (Series 21)

My test device is a Nexus 7 2013 with Android 4.4.4 KitKat and an Onyx Beacon. The beacon is just sitting less than a meter next to my device and I am not moving it. The beacon is detected and goes to didEnterRegion then after a minute or so it goes to didExitRegion then the cycle repeats.

We have also tested this on a different device (Samsung Phone) using a different (Onyx) beacon. Still the issue occurs. Note that this only happens on the Onyx Beacons (Beacon One). The Gimbal Beacons (Series 21) are only triggered once when in range.

Am I using the correct Beacon Parser strings? Am I implementing this correctly? I am really out of ideas and I wanted to support both beacons.

BaseApplication

public class BaseApplication extends Application {

    @Override
    public void onCreate() {
        super.onCreate();
        startService(new Intent(this, BeaconMonitoringService.class));
    }

}

BeaconMonitoringService

public class BeaconMonitoringService extends Service implements BeaconConsumer{

    private Context context;

    private BeaconService service;
    private BeaconManager beaconManager;
    private NotificationCompat.Builder notifBuilder;

    @Override
    public void onCreate(){
        super.onCreate();
        context = getBaseContext();
        beaconManager = BeaconManager.getInstanceForApplication(this);
        beaconManager.setBackgroundMode(true);

        beaconManager.setBackgroundScanPeriod(1100l);
        beaconManager.setBackgroundBetweenScanPeriod(15000l);

        String[] beaconParsers = new String[] { "m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24", "m:2-3=beac,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25", "m:2-3=0203,i:14-19l,d:10-13,p:9-9" };

        for(String beacon : beaconParsers){
            Log.i("BeaconService","layout: "+beacon);
            beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout(beacon));           
        }
        beaconManager.bind(this);
    }

    @Override
    public void onBeaconServiceConnect() {
        beaconManager.setMonitorNotifier(new MonitorNotifier() {

            private String log;

            @Override
            public void didEnterRegion(Region region) {
                Beacon beacon = service.getBeacon(region.getId1().toString(), region.getId2().toString(), region.getId3().toString());
                log = "I just saw a beacon for the first time! "+region.getId1()+" "+region.getId2()+" "+region.getId3();
                Log.i("BeaconService", log);   
                sendNotification(beacon);
            }

            @Override
            public void didExitRegion(Region region) {
                Beacon beacon = service.getBeacon(region.getId1().toString(), region.getId2().toString(), region.getId3().toString());
                log = "I no longer see a beacon";
                Log.i("BeaconService", log);
            }

            @Override
            public void didDetermineStateForRegion(int state, Region region) {

            }
        });

        List<BackendRegion> regionList = Util.getRegionList();  
        for(BackendRegion region : regionList){
            ArrayList<Beacon> beaconList = region.getBeacons();
            for(Beacon beacon : beaconList){
                try {
                    String beaconString = region.getUuid().toLowerCase() + " " + beacon.getMajor() + " " + beacon.getMinor();
                    service.addBeacon(beacon);
                    beaconManager.startMonitoringBeaconsInRegion(new Region(beacon.getIdentifier(), Identifier.parse(region.getUuid()),Identifier.parse(beacon.getMajor()),Identifier.parse(beacon.getMinor())));
                } catch (RemoteException e) {    }
            }
        }

    }


    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onDestroy(){
        beaconManager.unbind(this);
        super.onDestroy();
    }

    private void sendNotification(Beacon beacon, InteractiveImage interactiveImage) {
        this.notifBuilder =  new NotificationCompat.Builder(this)
                .setAutoCancel(true)
                .setContentTitle("Sample Beacon")
                .setContentText("A beacon was detected")
                .setSmallIcon(R.drawable.ic_launcher);

        Intent notifyIntent = new Intent(this, NotificationActivity.class);
        notifyIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_MULTIPLE_TASK);

        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notifyIntent, 0);
        notifBuilder.setContentIntent(pendingIntent);

        NotificationManager notificationManager = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(Integer.parseInt(beacon.getId()), notifBuilder.build());

    }

}

Log

12-11 15:45:24.103 I/BeaconService: I just saw a beacon for the first time! 20cae8a0-a9cf-11e3-a5e2-0800200c9a66 7 44512
12-11 15:45:24.784 D/BluetoothAdapter: stopLeScan()
12-11 15:45:39.698 D/BluetoothAdapter: startLeScan(): null
12-11 15:45:39.709 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:45:40.830 D/BluetoothAdapter: stopLeScan()
12-11 15:45:55.794 D/BluetoothAdapter: startLeScan(): null
12-11 15:45:55.804 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:45:56.985 D/BluetoothAdapter: stopLeScan()
12-11 15:46:11.920 D/BluetoothAdapter: startLeScan(): null
12-11 15:46:11.950 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:46:13.091 D/BluetoothAdapter: stopLeScan()
12-11 15:46:27.996 D/BluetoothAdapter: startLeScan(): null
12-11 15:46:28.016 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:46:29.147 D/BluetoothAdapter: stopLeScan()
12-11 15:46:29.227 I/BeaconService: I no longer see a beacon
12-11 15:46:44.121 D/BluetoothAdapter: startLeScan(): null
12-11 15:46:44.141 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:46:44.402 I/BeaconService: I just saw a beacon for the first time! 20cae8a0-a9cf-11e3-a5e2-0800200c9a66 7 44512
12-11 15:46:45.282 D/BluetoothAdapter: stopLeScan()
12-11 15:47:00.197 D/BluetoothAdapter: startLeScan(): null
12-11 15:47:00.197 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:47:01.328 D/BluetoothAdapter: stopLeScan()
12-11 15:47:16.343 D/BluetoothAdapter: startLeScan(): null
12-11 15:47:16.363 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:47:17.514 D/BluetoothAdapter: stopLeScan()
12-11 15:47:32.418 D/BluetoothAdapter: startLeScan(): null
12-11 15:47:32.428 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:47:33.540 D/BluetoothAdapter: stopLeScan()
12-11 15:47:48.494 D/BluetoothAdapter: startLeScan(): null
12-11 15:47:48.504 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:47:49.635 D/BluetoothAdapter: stopLeScan()
12-11 15:48:04.620 D/BluetoothAdapter: startLeScan(): null
12-11 15:48:04.630 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:48:05.771 D/BluetoothAdapter: stopLeScan()
12-11 15:48:20.706 D/BluetoothAdapter: startLeScan(): null
12-11 15:48:20.706 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:48:21.827 D/BluetoothAdapter: stopLeScan()
12-11 15:48:36.821 D/BluetoothAdapter: startLeScan(): null
12-11 15:48:36.831 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:48:37.962 D/BluetoothAdapter: stopLeScan()
12-11 15:48:52.917 D/BluetoothAdapter: startLeScan(): null
12-11 15:48:52.927 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:48:54.058 D/BluetoothAdapter: stopLeScan()
12-11 15:49:09.003 D/BluetoothAdapter: startLeScan(): null
12-11 15:49:09.003 D/BluetoothAdapter: onClientRegistered() - status=0 clientIf=5
12-11 15:49:10.154 D/BluetoothAdapter: stopLeScan()
12-11 15:49:10.214 I/BeaconService: I no longer see a beacon

解决方案

A few tips:

  • Check the advertising rate of the Onyx Beacon One. For best results, beacons should advertise at a standard 10Hz. Some manufacturers slow down this advertising rate to save battery life. If you have a beacon that is advertising at 1Hz or less often, it can start to be missed periodically by beacon scanners. (Not 100% of packets are detected due to bluetooth radio noise.)

  • If you find that the advertising rate is 1 Hz or less, increase the advertising rate if possible and the issue should go away.

  • If you cannot increase the advertising rate of the beacon, you can slow down the scan rate of the Android Beacon Library to give it more time to collect beacon packets. By default, it uses a scan cycle of 1.1 seconds. You can use a line of code like below to slow this down to 5 seconds. This will work more reliably with beacons that don't advertise very often, but will also give you slower response times to region entries/exits. beaconManager.setForegroundScanPeriod(5000l);

这篇关于AltBeacon对于OnyxBeacons不稳定,反复循环通过didEnterRegion和didExitRegion的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆