我正在试验Android Beacon Library,我能够使它用于监控和测距Apple兼容的信标添加了自定义解析器(参见这是使用AltBeacon's Android Beacon Library检测iBeacons的正确布局吗?)
现在我正试图写一个应用程序,在后台开始使用这里所示的示例代码:
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");
}
}
不幸的是,它不起作用。这些函数永远不会被调用:
- public void didDetermineStateForRegion(int arg0, Region arg1)
- public void didEnterRegion(Region arg0)
- public void didExitRegion(Region arg0)
我希望在创建RegionBootstrap时至少调用didDetermineStateForRegion。
问题:
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正在运行时,应用程序将进入前台,但如果应用程序未运行,则不会发生任何事情。这是预期的行为还是应用程序应该启动,如果它没有运行?
了解开箱即用,该库仅适用于支持开放AltBeacon标准的信标。为了将所有知识产权排除在开源库之外,这是必要的。如果您希望使库与专有信标一起工作,您必须在onCreate
方法中添加一行来添加不同的BeaconParser实例。
BeaconManager.getInstanceForApplication().getBeaconParsers()
.add(new BeaconParser().setBeaconLayout("put-your-parser-expression-here"));
您需要将字符串"put-your-parser-expression-here"替换为一个将检测您正在使用的专有信标的字符串。为了找到各种专有信标的解析器表达式,请尝试在Google上搜索"getBeaconParser"(在搜索中包含引号)