在android平台上使用altbeacon库对Eddystone
信标进行后台监控可能吗?我怎样才能做到这一点?
以下是我可以在应用程序启动时检测具有指定UUID的信标的代码,但我希望在应用程序未运行时实现相同的功能。
public class MainActivity extends ActionBarActivity implements BeaconConsumer,MonitorNotifier
{
private BeaconManager beaconManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
protected void onResume() {
super.onResume();
beaconManager = BeaconManager.getInstanceForApplication(this.getApplicationContext());
beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));
beaconManager.bind(this);
}
@Override
public void onBeaconServiceConnect() {
Identifier myBeaconNamespaceId = Identifier.parse("0xe2bfcc3cc2370789caef");
Region region = new Region("my-beacon-region", myBeaconNamespaceId, null, null);
beaconManager.setMonitorNotifier(this);
try {
beaconManager.startMonitoringBeaconsInRegion(region);
} catch (RemoteException e) {
e.printStackTrace();
}
}
@Override
public void didEnterRegion(Region region) {
Log.d("radbeacon", "Beacon detected with namespace id " + region.getId1() +" and instance id: " + region.getId2());
}
@Override
public void didExitRegion(Region region) {
Log.d("radbeacon", "Beacon out of region with namespace id " + region.getId1() +" and instance id: " + region.getId2());
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
//Ignore
}
}
是的,可以使用Android Beacon Library在后台检测埃迪斯通信标你这样做的方式与AltBeacon或iBeacon相同。有关详细信息,请参阅示例的"背景"部分的"启动应用程序"。
编辑:作为库2.7版本,添加了对Eddystone帧的硬件加速发现的支持,这意味着在Android 5+设备上,您可以在大约5秒内获得背景检测。
基本思想是,您需要为应用程序创建一个中央android Application
类,并在该类的onCreate
方法中创建一个RegionBootstrap
对象。重要的是要记住,您必须在清单中注册这个Application
类。上面链接的示例代码向您展示了如何做到这一点。
所以你最终会得到下面这样的东西:
public class MyApplication extends Application implements BootstrapNotifier {
private static final String TAG = "MyApplication";
private RegionBootstrap regionBootstrap;
private BackgroundPowerSaver backgroundPowerSaver;
private BeaconManager mBeaconManager;
public void onCreate() {
super.onCreate();
mBeaconManager = org.altbeacon.beacon.BeaconManager.getInstanceForApplication(this);
mBeaconManager.getBeaconParsers().clear();
mBeaconManager.getBeaconParsers().add(new BeaconParser().
setBeaconLayout(BeaconParser.EDDYSTONE_UID_LAYOUT));
Identifier myBeaconNamespaceId = Identifier.parse("0xe2bfcc3cc2370789caef");
Region region = new Region("my-beacon-region", myBeaconNamespaceId, null, null);
regionBootstrap = new RegionBootstrap(this, region);
backgroundPowerSaver = new BackgroundPowerSaver(this);
}
@Override
public void didEnterRegion(Region region) {
Log.d("radbeacon", "Beacon detected with namespace id " + region.getId1() +" and instance id: " + region.getId2());
}
@Override
public void didExitRegion(Region region) {
Log.d("radbeacon", "Beacon out of region with namespace id " + region.getId1() +" and instance id: " + region.getId2());
}
@Override
public void didDetermineStateForRegion(int i, Region region) {
//Ignore
}
...
}
需要考虑的另一件非常重要的事情是将Application类名添加到AndroidManifest.xml,只需将android:name=".yourApplication"
添加到<application>
标记即可。
是否有在应用程序类中同时执行监视和测距的示例?