【发布时间】:2014-08-18 13:22:50
【问题描述】:
我正在试验 Android Beacon 库,我能够使其用于监控和测距 Apple 兼容信标添加了自定义解析器(请参阅Is this the correct layout to detect iBeacons with AltBeacon's Android Beacon Library?)
现在我正在尝试使用此处显示的示例代码编写一个在后台启动的应用程序:
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)
我预计至少 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(如果它正在运行)时,应用程序会进入前台,但如果应用程序未运行,则不会发生任何事情。这是预期的行为还是应用程序在未运行时应该启动?
【问题讨论】:
-
你打开蓝牙了吗?什么是移动操作系统版本以及蓝牙版本?一旦 bcz iBeacon 有一些规格,请检查这些信息
-
尝试移动 super.onCreate();到 onCreate() 方法的末尾。
-
启用蓝牙,我的设备支持 BLE 4.0。改变 super.onCreate() 的位置显然没有效果。
-
请将新问题作为实际不同的帖子发布...
-
是的,你想做的都是可能的。请将您的其他问题作为不同的帖子,我会回答。
标签: android ibeacon android-ibeacon altbeacon