iBeacon背景扫描RadiusNetworks库的PRO功能?

时间:2014-01-17 16:20:38

标签: android ibeacon ibeacon-android

我正在使用RadiusNetworks API处理iBeacons。

我已经使用了这个库并且它工作正常,但我不得不问我在做什么错误,我的应用程序中没有发生后台扫描?我错过了什么?

这是我到目前为止的实现,我只保留了与信标端相关的代码,但是将活动设置为背景会完全停止扫描......

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);
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您如何将活动发送到后台?你是按下后退按钮,主页按钮还是什么?

我怀疑发生的事情是Android实际上正在终止您的应用。您的onDestroy方法如下所示:

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

如果Android调用此方法,iBeaconManager将取消绑定到AndroidIBeaconService有效停止扫描。即使您删除此代码,如果Android决定终止该应用,它仍会自动终止该服务。

如果您想让它在后台运行,您需要将IBeaconManager附加到生命周期比该活动更长的内容上。最简单的方法是使用这样的自定义Android应用程序类(也必须在清单中声明):

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);
    }
}

BackgroundPowerSaver部分(仅限库的专业版)是可选的,但当您的应用在后台时,它会自动降低扫描频率以节省电量。如果您使用此功能,则无需再使用各种setBackgroundModeonPause方法拨打onResume