使用AltBeacon:从应用程序设置开始和停止扫描 [英] Using AltBeacon: start and stop scanning from app settings

查看:171
本文介绍了使用AltBeacon:从应用程序设置开始和停止扫描的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在将AltBeacon API集成到现有应用程序中.当用户进入或离开蓝牙区域"时,该应用应在后台中启动操作.

I am integrating the AltBeacon API in an existing app. The app shall start actions in the background, when the user enters or leaves a "bluetooth zone".

此功能应该是可选的,用户应从设置中启动和停止扫描.

This functionality should be optional and the user should start and stop the scanning from the settings.

我尝试了绑定/取消绑定BeaconConsumer ,但是我看到 BluetoothLeScanner 在后台不断扫描!如何停止BluetoothLeScanner的扫描?这是正确的方法吗?

I tryed bind/unbind the BeaconConsumer, but I saw, that in the background the BluetoothLeScanner is keeping scanning! How can I stop the BluetoothLeScanner from scanning? Is this the right way?

这是代码:

@Override
    public void onCreate() {
        super.onCreate();
        LogManager.setVerboseLoggingEnabled(true);
        log.debug("onCreate");
        mAllBeaconsRegion = new Region(ALLBEACONS, null, null, null);
        mBeaconManager = BeaconManager.getInstanceForApplication(this);
        mBackgroundPowerSaver = new BackgroundPowerSaver(this);
        mBeaconManager.setBackgroundBetweenScanPeriod(60000L);
        mBeaconManager.setBackgroundScanPeriod(2100L);
        // wake up the app when a beacon is seen
        mRegionBootstrap = new RegionBootstrap(this, mAllBeaconsRegion);
        mBeaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:2-3=0215,i:4-19,i:20-21,i:22-23,p:24-24,d:25-25"));
        dsGlobal = new DbGlobalsHelper(getApplicationContext());
        boolean isBeaconScan = Utils.isBoolean(dsGlobal.getCursorGlobalsByKey(Constants.DB_KEY_BEACON_SCAN));
        if(isBeaconScan){
            mBeaconManager.bind(this);
        }else{
            mBeaconManager.unbind(this);
        }
    }

    // Set Regions to monitor at boot/startup
    private void setRegionsAtBoot(){
        Log.i(TAG, "setRegionsAtBoot");
        log.info("setRegionsAtBoot");
        SimpleBeaconStore beaconStore = new SimpleBeaconStore(this);
        List<SimpleBeacon> beacons = beaconStore.getBeacons();
        for (Iterator<SimpleBeacon> iterator = beacons.iterator(); iterator.hasNext();) {
            SimpleBeacon simpleBeacon = iterator.next();
            List<Identifier> listB = new ArrayList<Identifier>();
            StringTokenizer st = new StringTokenizer(simpleBeacon.getBeaconUuid(), "#");
            while (st.hasMoreTokens()) {
                listB.add(new Identifier(Identifier.parse(st.nextToken())));
            }
            try {
                Log.i(TAG, "setRegionsAtBoot " + simpleBeacon.getId());
                log.info("setRegionsAtBoot " + simpleBeacon.getId());
                Region region = new Region(simpleBeacon.getId(), listB);
                mBeaconManager.startRangingBeaconsInRegion(region);
            } catch (RemoteException e) {
                e.printStackTrace();
            }
        }
    }

    @Override
    public void didRangeBeaconsInRegion(Collection<Beacon> beacons, Region region) {
        // Start actions
        if (region.getUniqueId().equals(ALLBEACONS)){
            return;
        }
        if (beacons.size() > 0) {
            for (Beacon beacon: beacons) {
                Log.d(TAG, "Beacon "+beacon.toString()+" is about "+beacon.getDistance()+" meters away, with Rssi: "+beacon.getRssi());
                log.debug("Beacon " + beacon.toString() + " is about " + beacon.getDistance() + " meters away, with Rssi: " + beacon.getRssi());

                datasource = new DbZoneHelper(getApplicationContext());
                ZoneEntity ze = datasource.getCursorZoneByName(region.getUniqueId());

                if (beacon.getDistance() < ze.getRadius() && !ze.isStatus()) {
                    // 
                    Log.i(TAG, "Beacon " + beacon.toString() + " Just became less than " + ze.getRadius() + " meters away.");
                    log.info("*** Beacon " + beacon.toString() + " Just became less than " + ze.getRadius() + " meters away. ***");
                    String transitionType = getTransitionString(1);
                    NotificationUtil.sendNotification(getApplicationContext(), transitionType, region.getUniqueId());
                    worker = new Worker(getApplicationContext());
                    worker.handleTransition(1, region.getUniqueId(), Constants.BEACON);

                }
                if (beacon.getDistance() > (ze.getRadius() * EXITMULTIPLICATOR) && ze.isStatus()) {
                    // 
                    Log.i(TAG, "Beacon "+ beacon.toString()+" Just became over " + ze.getRadius() * EXITMULTIPLICATOR + " meters away.");
                    log.info("*** Beacon " + beacon.toString() + " Just became over " + ze.getRadius() * EXITMULTIPLICATOR + " meters away. ***");
                    String transitionType = getTransitionString(2);
                    NotificationUtil.sendNotification(getApplicationContext(), transitionType, region.getUniqueId());

                    worker = new Worker(getApplicationContext());
                    // 
                    worker.handleTransition(2, region.getUniqueId(), Constants.BEACON);

                }
            }
        }
    }

    @Override
    public void didDetermineStateForRegion(int arg0, Region arg1) {
    }

    @Override
    public void didEnterRegion(Region region) {

    }

    @Override
    public void didExitRegion(Region region) {

        if (region.getUniqueId().equals(ALLBEACONS)){
            return;
        }

        datasource = new DbZoneHelper(getApplicationContext());
        ZoneEntity ze = datasource.getCursorZoneByName(region.getUniqueId());

        if (ze.isStatus()) {
            // 
            Log.i(TAG, "Beacon " + region.getUniqueId() + " Just exited region.");
            log.info("Beacon " + region.getUniqueId() + " Just exited region.");
            String transitionType = getTransitionString(2);
            NotificationUtil.sendNotification(getApplicationContext(), transitionType, region.getUniqueId());

            worker = new Worker(getApplicationContext());

            worker.handleTransition(2, region.getUniqueId(), Constants.BEACON);
        }
    }

    @Override
    public void onBeaconServiceConnect() {
        Log.d(TAG, "onBeaconServiceConnect.");
        log.debug("onBeaconServiceConnect");
        setRegionsAtBoot();
        mBeaconManager.setRangeNotifier(this);
    }

    public void bind(){
        mBeaconManager.bind(this);
    }

    public void unbind(){
        mBeaconManager.unbind(this);
    }

我还从设置中调用绑定/取消绑定.

I am also calling the bind/unbind from my settings.

推荐答案

所示代码使用RegionBootstrap类在后台连续扫描信标.此类的基本目的是永不停止扫描信标. RegionBootstrap在构造时绑定到扫描服务,并且永远不会解除绑定.因此,显示的手动绑定和解除绑定的代码无效.

The code shown uses a RegionBootstrap class to continually scan for beacons in the background. This class is essentially designed to never stop scanning for beacons. The RegionBootstrap binds to the scanning service when constructed and effectively never unbinds. The code shown that manually binds and unbinds therefore has no effect.

如果要在不同时间启动和停止扫描,请不要使用RegionBootstrap类,而应使ApplicationActivity实现BeaconConsumer并按照监视中的说明进行绑定/取消绑定此处的示例代码: http://altbeacon.github.io/android-beacon- library/samples.html

If you want to start and stop scanning at various times, then don't use the RegionBootstrap class, and instead make your Application or Activity implement BeaconConsumer and bind/unbind as described in the monitoring example code here: http://altbeacon.github.io/android-beacon-library/samples.html

EDIT :已将新方法添加到RegionBootstrap中,以使动态区域监视更加容易.现在,您可以使用:regionBootstrap.disable()停止完全扫描,或regionBootstrap.removeRegion(region)停止寻找单个区域.

EDIT: New methods have been added to RegionBootstrap to make dynamic region monitoring easier. You can now use: regionBootstrap.disable() to stop scanning entirely, or regionBootstrap.removeRegion(region) to stop looking for a single region.

这篇关于使用AltBeacon:从应用程序设置开始和停止扫描的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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