Android Estimote区域监控

时间:2015-01-24 00:42:29

标签: android estimote region-monitoring

我正在尝试将Estimote SDK添加到我的Android应用中。我变得非常接近,但我在监控某个地区时遇到了一些麻烦。我正在https://github.com/Estimote/Android-SDK的GitHub上关注Estimote Android SDK指南。

出于某种原因,onEnteredRegion和onExitedRegion方法根本没有触发。我希望他们能够在应用程序看到Estimote信标的任何时候触发。谢谢!

这是我到目前为止的代码。没有太复杂的事情:

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) {

        }
    }
}

1 个答案:

答案 0 :(得分:1)

尝试将其放入onStart()方法:

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

您还需要记住在不再需要时断开与BeaconManager的连接,例如:使用此onDestroy实现:

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

基本上,需要在信标服务准备就绪后启动测距和监控,这可以通过利用上面显示的回调轻松实现。