首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AltBeacon不稳定用于OnyxBeacons,通过didEnterRegion和didExitRegion反复循环

AltBeacon不稳定用于OnyxBeacons,通过didEnterRegion和didExitRegion反复循环
EN

Stack Overflow用户
提问于 2015-12-11 08:06:57
回答 1查看 253关注 0票数 1

我正在构建一个使用AltBeacon库来支持Onyx信标(信标一号)万向节信标(系列21)的应用程序

我的测试设备是Nexus 7,2013年,配备了Android4.4.4 KitKat和Onyx信标。灯塔就在我的设备旁边不到一米的地方,我不会移动它。该信标被检测到并到达didEnterRegion,然后在大约一分钟后到达didExitRegion,然后循环重复。

我们还用不同的(Onyx)信标在不同的设备(三星手机)上测试了这一点。问题仍在发生。请注意,这只发生在Onyx信标(信标一号)上。万向节信标(系列21)只在范围内触发一次。

我用的是正确的灯塔分析器字符串吗?我是否正确地实现了这一点?我真的没有主意了,我想支持这两个信标。

BaseApplication

代码语言:javascript
复制
public class BaseApplication extends Application {

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

}

BeaconMonitoringService

代码语言:javascript
复制
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());

    }

}

日志

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2015-12-11 14:32:31

以下是一些小窍门:

  • 检查Onyx的广告率。为了获得最好的效果,信标应该在标准的10赫兹上做广告。为了节省电池寿命,一些制造商放慢了这一广告速度。如果你有一个以1Hz或更少频率做广告的信标,信标扫描器可能会周期性地错过它。(由于蓝牙无线电噪声,无法检测到100%的数据包。)
  • 如果你发现广告的频率是1Hz或更低,会在可能的情况下提高的广告率,这个问题应该解决。
  • 如果不能提高信标的广告率,则可以减慢 Android灯塔图书馆的扫描速率,使其有更多的时间收集信标数据包。默认情况下,它使用1.1秒的扫描周期。您可以像下面这样使用一行代码来将速度降低到5秒。这将更可靠地工作与信标,不经常做广告,但也会给你更慢的响应时间的区域入口/出口。beaconManager.setForegroundScanPeriod(5000l);
票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/34218739

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档