当我的应用没有运行时,我可以听Eddystone信标吗? [英] Can I listen for Eddystone beacons when my app is not running?

查看:174
本文介绍了当我的应用没有运行时,我可以听Eddystone信标吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过 Google的新Eddystone标准他们将在Google Play服务 Nearby Api 中为Android提供支持。我们可以注册eddystone信标,并让我们的应用程序收到意图,即使应用程序没有运行吗? 是的,它是可以使用 Android信标库完成此操作,该库完全支持 Eddystone

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



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

  public class MyApplicationName extends应用程序实现BootstrapNotifier {
private static final String TAG =.MyApplicationName;
private RegionBootstrap regionBootstrap;

@Override
public void onCreate(){
super.onCreate();
Log.d(标签,应用程序启动);
BeaconManager beaconManager = BeaconManager.getInstanceForApplication(this);
//检测主标识符(UID)帧:
beaconManager.getBeaconParsers()。add(new BeaconParser()。setBeaconLayout(s:0-1 = feaa,m:2-2 = 00以下,p:3-3:-41,I:4-13,I:14-19\" ));

//当看到匹配myEddystoneNamespaceId的信标时唤醒应用
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){
//不关心
}

@Override
public void didEnterRegion(Region arg0){
Log.d(TAG,Got a didEnterRegion call);
//这个禁用的调用将使得下面的活动仅在第一次看到信标时启动(直到下次启动应用时)
//如果您希望活动启动每一次进入信号灯时,请移除此呼叫。
regionBootstrap.disable();
Intent intent = new Intent(this,MainActivity.class);
//重要提示:在此活动的AndroidManifest.xml定义中,您必须设置android:launchMode =singleInstance,否则您将在用户手动启动活动时创建两个实例
// //从这里启动。
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
this.startActivity(intent);
}

@Override
public void didExitRegion(Region arg0){
// Do not care
}
}


With Google's new Eddystone standard they will be providing support for android in Google Play services Nearby Api. Can we register for eddystone beacons and have our app receive an intent even if the app is not running?

解决方案

Yes, it is possible to do exactly this using the Android Beacon Library, which has full support for Eddystone.

The mechanism for background launching of your app works the same way on Eddystone as it does for other kinds of beacons supported by the library. You use a RegionBootstrap object in a custom Application class. You can read details about how this works here.

The only difference with Eddystone is that you have to set up a BeaconParser that decodes the Eddystone-UID frame, and then set up a Region that will match your Eddystone namespace id:

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
    }        
}

这篇关于当我的应用没有运行时,我可以听Eddystone信标吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆