iBeacon显示背景扫描RadiusNetworks库PRO功能? [英] iBeacon background scanning PRO feature of RadiusNetworks library?

查看:413
本文介绍了iBeacon显示背景扫描RadiusNetworks库PRO功能?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用RadiusNetworks API与iBeacons工作。

我已经使用这个库和它工作得很好,不过,我要问什么我做错了,该后台扫描不会在应用程序中发生的呢?我缺少什么?

下面是执行我到目前为止,我只保留了相关的灯塔边code,但是设置活动背景停止扫描完全...

 包ro.gebs.zonizbeacon;公共类MainActivity扩展FragmentActivity
        实现NavigationDrawerFragment.NavigationDrawerCallbacks,SearchView.OnQueryTextListener,IBeaconConsumer,RangeNotifier,IBeaconDataNotifier {
    / **
     *片段管理导航抽屉的行为,相互作用和presentation。
     * /
    私人NavigationDrawerFragment mNavigationDrawerFragment;
    私人MainOffersFragment mOffersFragment;    受保护的静态最后弦乐TAG =BeaconActivity;
    私人IBeaconManager iBeaconManager = IBeaconManager.getInstanceForApplication(本);
    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);        //getActionBar().setIcon(R.drawable.home);
        的setContentView(R.layout.activity_main);        BeaconUtils.verifyBluetooth(MainActivity.this);
        iBeaconManager.bind(本);
    }
    @覆盖
    保护无效的onDestroy(){
        super.onDestroy();
        iBeaconManager.unBind(本);
    }    @覆盖
    保护无效的onPause(){
        super.onPause();
        如果(iBeaconManager.isBound(本))iBeaconManager.setBackgroundMode(这一点,真正的);
    }    @覆盖
    保护无效onResume(){
        super.onResume();
        如果(iBeaconManager.isBound(本))iBeaconManager.setBackgroundMode(这一点,假);    }
    @覆盖
    公共无效onIBeaconServiceConnect(){
        地区区域=新区域(MainActivityRanging,NULL,NULL,NULL);
        尝试{
            iBeaconManager.startRangingBeaconsInRegion(区);
            iBeaconManager.setRangeNotifier(本);
        }赶上(RemoteException的E){
            e.printStackTrace();
        }
    }    @覆盖
    公共无效iBeaconDataUpdate(iBeacon显示iBeacon显示,IBeaconData iBeaconData,DataProviderException E){
        如果(E!= NULL){
            Log.d(TAG,取数据错误:+ E);
        }
        如果(iBeaconData!= NULL){
            字符串displayString = iBeacon.getProximityUuid()++ iBeacon.getMajor()++ iBeacon.getMinor()+\\ n+欢迎信息:+ iBeaconData.get(welcomeMessage);
            Log.d(TAG,displayString);
        }
    }    @覆盖
    公共无效didRangeBeaconsInRegion(收集和LT; iBeacon显示> iBeacons,地区区){
        对于(iBeacon显示iBeacon显示:iBeacons){
            iBeacon.requestData(本);
            字符串displayString = iBeacon.getProximityUuid()++ iBeacon.getMajor()++ iBeacon.getMinor()+\\ n;
            Log.d(TAG,displayString);
        }
    }
}


解决方案

你是如何发送您的活动的背景?你打后退按钮,home键还是什么?

我怀疑是什么情况是,Android是实际上终止您的应用程序。你的的onDestroy 方法是这样的:

  @覆盖
保护无效的onDestroy(){
    super.onDestroy();
    iBeaconManager.unBind(本);
}

如果Android把这种方法, iBeaconManager 将取消绑定到 AndroidIBeaconService 有效地停止扫描。即使你删除此code,Android将仍然会自动杀死服务,如果它决定终止应用程序。

如果你想保持这个在后台运行,你需要在 IBeaconManager 附加的东西,具有比活性更长的生命周期。要做到这一点最简单的方法是这样的定制的Andr​​oid应用程序类(这还必须在你的清单中声明):

 公共类MyTestIBeaconApplication扩展应用{
    私人BackgroundPowerSaver backgroundPowerSaver;
    私人IBeaconManager iBeaconManager;    公共无效的onCreate(){
        super.onCreate();
        //简单地构建这个类并保持对它的引用
        //使约60%的自动省电
        backgroundPowerSaver =新BackgroundPowerSaver(本);
        iBeaconManager = IBeaconManager.getInstanceForApplication(本);
    }
}

BackgroundPowerSaver 部分(仅在库的Pro版)是可选的,但是当你的应用程序在后台以节省电池会自动减慢扫描频率。如果你使用它,你再也不用在您的各种的onPause setBackgroundMode 通话> onResume 方法。

I am using the RadiusNetworks API to work with iBeacons.

I have made use of this library and it works fine, however i would have to ask what i am doing wrong that the background scanning does not happen in my application? What am I missing?

Here's the implementation i have so far, i only kept the code relevant to the beacon side, however setting the activity to background stops the scanning completely...

package ro.gebs.zonizbeacon;



public class MainActivity extends FragmentActivity
        implements NavigationDrawerFragment.NavigationDrawerCallbacks, SearchView.OnQueryTextListener, IBeaconConsumer, RangeNotifier, IBeaconDataNotifier {
    /**
     * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
     */
    private NavigationDrawerFragment mNavigationDrawerFragment;
    private MainOffersFragment mOffersFragment;

    protected static final String TAG = "BeaconActivity";
    private IBeaconManager iBeaconManager = IBeaconManager.getInstanceForApplication(this);


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        //getActionBar().setIcon(R.drawable.home);
        setContentView(R.layout.activity_main);



        BeaconUtils.verifyBluetooth(MainActivity.this);
        iBeaconManager.bind(this);
    }




    @Override
    protected void onDestroy() {
        super.onDestroy();
        iBeaconManager.unBind(this);
    }

    @Override
    protected void onPause() {
        super.onPause();
        if (iBeaconManager.isBound(this)) iBeaconManager.setBackgroundMode(this, true);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (iBeaconManager.isBound(this)) iBeaconManager.setBackgroundMode(this, false);

    }


    @Override
    public void onIBeaconServiceConnect() {
        Region region = new Region("MainActivityRanging", null, null, null);
        try {
            iBeaconManager.startRangingBeaconsInRegion(region);
            iBeaconManager.setRangeNotifier(this);
        } catch (RemoteException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void iBeaconDataUpdate(IBeacon iBeacon, IBeaconData iBeaconData, DataProviderException e) {
        if (e != null) {
            Log.d(TAG, "data fetch error:" + e);
        }
        if (iBeaconData != null) {
            String displayString = iBeacon.getProximityUuid() + " " + iBeacon.getMajor() + " " + iBeacon.getMinor() + "\n" + "Welcome message:" + iBeaconData.get("welcomeMessage");
            Log.d(TAG, displayString);
        }
    }

    @Override
    public void didRangeBeaconsInRegion(Collection<IBeacon> iBeacons, Region region) {
        for (IBeacon iBeacon : iBeacons) {
            iBeacon.requestData(this);
            String displayString = iBeacon.getProximityUuid() + " " + iBeacon.getMajor() + " " + iBeacon.getMinor() + "\n";
            Log.d(TAG, displayString);
        }
    }
}

解决方案

How are you sending your activity to the background? Are you hitting the back button, the home button or what?

I suspect what is happening is that Android is actually terminating your App. Your onDestroy method looks like this:

@Override
protected void onDestroy() {
    super.onDestroy();
    iBeaconManager.unBind(this);
}

If Android calls this method, the iBeaconManager will unbind to the AndroidIBeaconService effectively stopping scanning. Even if you remove this code, Android will still kill the service automatically if it decides to terminate the app.

If you want to keep this running in the background, you need to attach the IBeaconManager to something that has a longer life cycle than that activity. The easiest way to do this is with a custom Android Application class like this (which must also be declared in your manifest):

public class MyTestIBeaconApplication extends Application {
    private BackgroundPowerSaver backgroundPowerSaver;
    private IBeaconManager iBeaconManager;

    public void onCreate() {
        super.onCreate();
        // Simply constructing this class and holding a reference to it 
        // enables auto battery saving of about 60%
        backgroundPowerSaver = new BackgroundPowerSaver(this);
        iBeaconManager = IBeaconManager.getInstanceForApplication(this);
    }
}

The BackgroundPowerSaver part (only in the Pro version of the library) is optional, but will automatically slow down the scans frequency when your app is in the background to save battery. If you use this, you no longer have to make calls to setBackgroundMode in your various onPause and onResume methods.

这篇关于iBeacon显示背景扫描RadiusNetworks库PRO功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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