【问题标题】:Can I listen for Eddystone beacons when my app is not running?我可以在我的应用程序未运行时收听 Eddystone 信标吗?
【发布时间】:2015-07-14 18:07:19
【问题描述】:

通过Google's new Eddystone standard,他们将在Google Play 服务Nearby Api 中提供对android 的支持。我们可以注册 eddystone 信标并让我们的应用在应用未运行的情况下接收意图吗?

【问题讨论】:

  • 很难说 — 目前既没有更新的 Nearby API,也没有任何文档可用。我希望他们实现这一点,但这只是猜测。

标签: android google-play-services eddystone


【解决方案1】:

是的,使用Android Beacon Library 完全可以做到这一点,它完全支持Eddystone

后台启动应用程序的机制在 Eddystone 上的工作方式与在库支持的其他类型的信标上的工作方式相同。您在自定义 Application 类中使用 RegionBootstrap 对象。您可以阅读有关其工作原理的详细信息here

与 Eddystone 的唯一区别是您必须设置一个解码 Eddystone-UID 帧的 BeaconParser,然后设置一个与您的 Eddystone 命名空间 ID 匹配的 Region

public class MyApplicationName extends Application implements BootstrapNotifier {
    private static final String TAG = ".MyApplicationName";
    private RegionBootstrap regionBootstrap;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.d(TAG, "App started up");
        BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
        // Detect the main identifier (UID) frame:
        beaconManager.getBeaconParsers().add(new BeaconParser().setBeaconLayout("s:0-1=feaa,m:2-2=00,p:3-3:-41,i:4-13,i:14-19"));

        // wake up the app when a beacon matching myEddystoneNamespaceId is seen 
        myEddystoneNamespaceId = Identifier.parse("0x2f234454f4911ba9ffa6");
        Region region = new Region("com.example.myapp.boostrapRegion", myEddystoneNamespaceId, null, null);
        regionBootstrap = new RegionBootstrap(this, region);
    }

    @Override
    public void didDetermineStateForRegion(int arg0, Region arg1) {
        // Don't care
    }

    @Override
    public void didEnterRegion(Region arg0) {
        Log.d(TAG, "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 arg0) {
       // Don't care
    }        
}

【讨论】:

  • 听起来像这样仍然需要该应用程序正在运行 - 即仍然有它的进程。如果用户在哪里强制停止应用程序,它会不会收到回调,因为应用程序不会运行。
  • 是的,从技术上讲,该进程在后台运行,至少是为了寻找信标。但是,在用户界面出现之前,它不会显示在最近的应用程序列表(任务切换器)中。如果用户从最近的任务列表中滑出应用程序,Android Beacon 库会在 5 分钟内使用警报重新启动自身以在后台查找信标。
  • @davidgyoung 为什么不使用 IntentService(就像处理 GCM 消息一样)?
  • IntentService 是从 外部 或 GCM 等系统服务获取消息的好方法。使用 Android 信标库时,信标扫描服务与您的应用程序捆绑在一起,并作为内部服务在后台运行。
  • 必须只在Application 类上实现BootstrapNotifier,如果在Activity 上实现'BootstrapNotifier`,它的工作方式是否相同?
【解决方案2】:

好的,所以这很痛苦,但我终于设法使用Nearby Messages API 检测到信标。

  1. 首先创建一个Google Developer Console项目
  2. 为您刚刚创建的项目启用“附近消息 API”
  3. 为此项目创建一个 API 密钥,如 here 所述,并将其添加到清单中
  4. 按照here 的描述配置和注册您的信标;我要做的就是安装 Android 应用,打开它,选择我刚刚创建的项目,发现信标并将其注册到我的应用中
  5. 您必须在信标上附加一条消息,才能检测到它。我使用Beacon Tools app 完成此操作,单击注册的信标,然后点击“附件”
  6. 现在您可以按照代码示例here 进行操作,它应该会检测到您的信标

    Nearby.Messages.subscribe(googleApiClient, new MessageListener() {
        @Override
        public void onFound(Message message) {
            Log.i(TAG, "Found : " + message);
        }
    
        @Override
        public void onLost(Message message) {
            Log.i(TAG, "Lost : " + message);
        }
    }, new SubscribeOptions.Builder()
            .setStrategy(Strategy.BLE_ONLY)
            .build());
    

【讨论】:

    【解决方案3】:

    自 Google play services v8.4 SDK(2015 年 12 月)发布以来,这已经成为可能。

    查看以下链接了解更多信息: http://android-developers.blogspot.com/2015/12/google-play-services-84-sdk-is-available_18.html

    【讨论】:

      猜你喜欢
      • 2021-10-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多