【问题标题】:Android Estimote Region MonitoringAndroid Estimote 区域监控
【发布时间】:2015-03-23 03:35:02
【问题描述】:

我正在尝试将 Estimote SDK 添加到我的 Android 应用中。我已经很接近了,但是我在监视某个区域时遇到了一些麻烦。我正在关注 GitHub 上的 Estimote Android SDK 指南https://github.com/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) {

        }
    }
}

【问题讨论】:

    标签: android estimote region-monitoring


    【解决方案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();
    }
    

    基本上,测距和监控需要在beacon服务准备好后启动,利用上面的回调很容易实现。

    【讨论】:

    • 谢谢,我还是有问题。我的代码现在无法编译,我收到一条错误消息,上面写着 MainActivity has leaked ServiceConnection com.estimote.sdk.BeaconManager$InternalServiceConnection
    • 对,我在onDestroy 中忘记了beaconManager.disconnect();。我用代码 sn-p 更新了我的答案。
    猜你喜欢
    • 2014-07-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-13
    • 2018-08-04
    相关资源
    最近更新 更多