我正在试验 Android 信标库,并且我能够使其与 Apple 兼容的信标一起用于监视和测距,并添加一个自定义解析器(请参阅这是使用 AltBeacon 的 Android 信标库检测 iBeacon 的正确布局吗?)
现在我正在尝试使用此处显示的示例代码编写一个在后台启动的应用程序:
http://altbeacon.github.io/android-beacon-library/samples.html
这是我的代码:
public class IBeaconBootstrap extends Application implements BootstrapNotifier {
private RegionBootstrap regionBootstrap;
@Override
public void onCreate() {
super.onCreate();
Log.d("IBeaconBootstrap", "App started up");
// wake up the app when any beacon is seen (you can specify specific id
// filers in the parameters below)
Region region = new Region("MyRegion", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
}
@Override
public void didDetermineStateForRegion(int state, Region region) {
// Don't care
// MonitorNotifier.INSIDE
Log.d("Boostrap didDetermineStateForRegion", "Region " + region.toString());
}
@Override
public void didEnterRegion(Region region) {
Log.d("Boostrap didEnterRegion", "Got a didEnterRegion call");
// This call to disable will make it so the activity below only gets
// launched the first time a beacon is seen (until the next time the app
// is launched)
// if you want the Activity to launch every single time beacons come
// into view, remove this call.
regionBootstrap.disable();
Intent intent = new Intent(this, MainActivity.class);
// IMPORTANT: in the AndroidManifest.xml definition of this activity,
// you must set android:launchMode="singleInstance" or you will get two
// instances
// created when a user launches the activity manually and it gets
// launched from here.
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}
@Override
public void didExitRegion(Region region) {
Log.d("Boostrap didExitRegion", "Got a didExitRegion call");
}
}
不幸的是,它不起作用。这些函数永远不会被调用:
- 公共无效 didDetermineStateForRegion(int arg0,区域 arg1)
- 公共无效 didEnterRegion(区域 arg0)
- 公共无效 didExitRegion(区域 arg0)
我预计至少 didDetermineStateForRegion 被称为创建 RegionBootstrap。
问题:
0)我错过了什么?
1) 此功能是否也适用于 Apple 兼容的 iBeacons?
2)我必须添加自定义解析器吗?在哪里/如何?
先感谢您。
更新 0:
按照 davidgyoung 的指示,我最终让它工作,改变 onCreate 函数如下:
@Override
public void onCreate() {
Log.d("IBeaconBootstrap", "App started up");
// wake up the app when any beacon is seen (you can specify specific id
// filers in the parameters below)
Region region = new Region("MyRegion", null, null, null);
regionBootstrap = new RegionBootstrap(this, region);
BeaconManager.getInstanceForApplication(this).getBeaconParsers().add(new BeaconParser().setBeaconLayout("m:0-3=4c000215,i:4-19,i:20-21,i:22-23,p:24-24"));
super.onCreate();
}
我还有两个问题:
0) 应用程序每 3-5 分钟检查一次 iBeacons 的存在,有没有办法静态或动态更改此间隔?
1)显然,当检测到 iBeacon(如果它正在运行)时,应用程序会进入前台,但如果应用程序未运行,则不会发生任何事情。这是预期的行为还是应用程序在未运行时应该启动?