我正在使用 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);
}
}
}