我在 Github repo 上使用了Altbeacon
( ) 提供的基本示例应用程序。Radio Network
一切似乎都适用于我的 iBeacon 从Monitoring
到Ranging
但仅在前台
我已完成以下步骤以使其在前台工作。
public class BeaconReferenceApplication extends Application implements BootstrapNotifier, RangeNotifier
{
private static final String TAG = "BeaconReferenceApplication";
private BeaconManager mBeaconManager;
private Region mAllBeaconsRegion;
private BackgroundPowerSaver mBackgroundPowerSaver;
private RegionBootstrap mRegionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
@Override
public void onCreate()
{
mAllBeaconsRegion = new Region(getPackageName(), null, null, null);
mBeaconManager = BeaconManager.getInstanceForApplication(this);
mBackgroundPowerSaver = new BackgroundPowerSaver(this);
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"));
backgroundPowerSaver = new BackgroundPowerSaver(this);
}
@Override
public void didRangeBeaconsInRegion(Collection<Beacon> arg0, Region arg1)
{
System.out.println("app-> didRangeBeaconsInRegion");
}
@Override
public void didDetermineStateForRegion(int arg0, Region arg1)
{
System.out.println("app-> didDetermineStateForRegion");
}
@Override
public void didEnterRegion(Region arg0)
{
System.out.println("app-> didEnterRegion");
try
{
Log.d(TAG, "entered region. starting ranging");
mBeaconManager.setRangeNotifier(this);
mBeaconManager.startRangingBeaconsInRegion(mAllBeaconsRegion);
}
catch(RemoteException e)
{
Log.e(TAG, "Cannot start ranging");
}
Intent intentMainActivity = new Intent(this,MonitoringAndRangingActivity.class);
intentMainActivity.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(intentMainActivity);
}
@Override
public void didExitRegion(Region arg0)
{
System.out.println("app-> didExitRegion");
}
public void setMonitoringAndRangingActivity(MonitoringAndRangingActivity activity)
{
mBeaconManager.setBackgroundMode((activity==null)?true:false);
}
}
Edit:
我已经添加了该方法mBeaconManager.setBackgroundMode(true);
但没有帮助,我只是从中获取了 stopLE scan() 消息BlutoothAdapter
并且一切都停止了,我需要在服务中实现它还是其他任何东西?
但是,一旦我进入后台(按返回或主页),日志就会callback methods
停止响应。我是否缺少任何东西来激活后台 iBeacon 检测。
任何输入都会像一个福音。