【问题标题】:Starting App in background when iBeacon detected检测到 iBeacon 时在后台启动应用程序
【发布时间】: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


【解决方案1】:

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

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

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

【讨论】:

  • 大卫,谢谢。这是我已经为监视和测距所做的。现在我的问题是当 iBeacon 在范围内时在后台启动应用程序。
  • 对,为了让它工作,你需要在你的 Application 类的 onCreate 方法中有这样一行。如果你把这条线放在那里,你就不需要它了。
  • 大卫,你的帮助拯救了我这一天。请看我的更新。我还有更多问题。谢谢。
猜你喜欢
  • 1970-01-01
  • 2012-01-18
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多