Android的Estimote地区监测 [英] Android Estimote Region Monitoring

查看:194
本文介绍了Android的Estimote地区监测的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想给Estimote SDK加入到我的Andr​​oid应用程序。我得到pretty接近,但我有一些麻烦监测的区域。我下面在GitHub上Estimote的Andr​​oid SDK指南: https://github.com/Estimote/Android-SDK

有关某种原因,onEnteredRegion和onExitedRegion方法不开火的。我想他们触发任何时候应用程序看到一个Estimote灯塔。谢谢!

这里是code我有这么远。没有什么太复杂的:

 公共类MainActivity延伸活动{    私有静态最后的地区ALL_ESTIMOTE_BEACONS =新的区域(regionId,B9407F30-F5F8-466E-AFF9-25556B57FE6D,NULL,NULL);    BeaconManager beaconManager;    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);        最后AlertDialog.Builder建设者=新AlertDialog.Builder(本);        beaconManager =新BeaconManager(本);
        beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1),0);        beaconManager.setMonitoringListener(新MonitoringListener(){            @覆盖
            公共无效onEnteredRegion(区区域,列表与LT;烽火台>信标){
                builder.setTitle(输入区)
                        .setMessage()
                        .setNeutralButton(OK,NULL);
                AlertDialog对话框= builder.create();
                dialog.show();
            }            @覆盖
            公共无效onExitedRegion(地区区域){
                builder.setTitle(已退出区)
                        .setMessage()
                        .setNeutralButton(OK,NULL);
                AlertDialog对话框= builder.create();
                dialog.show();
            }
        });
    }    保护无效调用onStart(){
        super.onStart();
        尝试{
            beaconManager.startMonitoring(ALL_ESTIMOTE_BEACONS);
        }
        赶上(RemoteException的E){        }
    }
}


解决方案

尝试把这个在你的在onStart()方法:

  beaconManager.connect(新BeaconManager.ServiceReadyCallback(){
  @覆盖
  公共无效onServiceReady(){
    尝试{
      beaconManager.startMonitoring(区);
    }赶上(RemoteException的E){
      Log.d(TAG,错误而启动监控);
    }
  }

您还需要记住有关从BeaconManager断开连接时,它不再需要,例如与此的onDestroy 实施

  @覆盖
保护无效的onDestroy(){
    beaconManager.disconnect();
    super.onDestroy();
}

基本上,测距和监测所需要的信标服务准备好之后被启动,这可以通过利用上面所示的回调很容易地实现。

I am trying to add the Estimote SDK into my Android app. I'm getting pretty close, but I'm having some trouble monitoring for a region. I am following the Estimote Android SDK Guide on GitHub at https://github.com/Estimote/Android-SDK.

For some reason, the onEnteredRegion and onExitedRegion methods are not firing at all. I'm would like them to trigger any time the app sees an Estimote beacon. Thanks!

Here is the code I have so far. Nothing too complicated:

public class MainActivity extends Activity {

    private static final Region ALL_ESTIMOTE_BEACONS = new Region("regionId", "B9407F30-F5F8-466E-AFF9-25556B57FE6D", null, null);

    BeaconManager beaconManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        final AlertDialog.Builder builder = new AlertDialog.Builder(this);

        beaconManager = new BeaconManager(this);
        beaconManager.setBackgroundScanPeriod(TimeUnit.SECONDS.toMillis(1), 0);

        beaconManager.setMonitoringListener(new MonitoringListener() {

            @Override
            public void onEnteredRegion(Region region, List<Beacon> beacons) {
                builder.setTitle("Entered Region")
                        .setMessage("")
                        .setNeutralButton("OK", null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }

            @Override
            public void onExitedRegion(Region region) {
                builder.setTitle("Exited Region")
                        .setMessage("")
                        .setNeutralButton("OK", null);
                AlertDialog dialog = builder.create();
                dialog.show();
            }
        });
    }

    protected void onStart() {
        super.onStart();
        try {
            beaconManager.startMonitoring(ALL_ESTIMOTE_BEACONS);
        }
        catch (RemoteException e) {

        }
    }
}

解决方案

Try putting this in your onStart() method:

beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
  @Override
  public void onServiceReady() {
    try {
      beaconManager.startMonitoring(region);
    } catch (RemoteException e) {
      Log.d(TAG, "Error while starting monitoring");
    }
  }

You also need to remember about disconnecting from the BeaconManager when it's no longer needed, e.g. with this onDestroy implementation:

@Override
protected void onDestroy() {
    beaconManager.disconnect();
    super.onDestroy();
}

Basically, ranging and monitoring need to be started after the beacon service is made ready, which can be easily achieved by utilizing the callback shown above.

这篇关于Android的Estimote地区监测的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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