首页 文章

检测到iBeacon时在后台启动应用程序

提问于
浏览
2

我正在试验Android Beacon Library,我能够使用Apple兼容信标进行监控和测距,添加自定义Parser(参见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)我是否必须添加自定义分析器?在哪里/如何?

先感谢您 .

UPDATE 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时,应用程序会前进,如果它正在运行但是如果应用程序没有运行则没有任何反应 . 这是预期的行为还是应用程序如果没有运行应该启动?

1 回答

  • 2

    了解开箱即用,该库仅适用于支持open AltBeacon标准的信标 . 这是必要的,以便将所有知识产权保留在开源库之外 . 如果您希望使库使用专有信标,则必须在 onCreate 方法中添加一行以添加不同的BeaconParser实例 .

    BeaconManager.getInstanceForApplication().getBeaconParsers()
      .add(new BeaconParser().setBeaconLayout("put-your-parser-expression-here"));
    

    您将需要将字符串“put-your-parser-expression-here”替换为将检测您正在使用的专有信标的字符串 . 为了找到各种专有信标的解析器表达式,请尝试在Google上搜索“getBeaconParser”(在搜索中包含引号) .

相关问题