Estimote iBeacon: Monitoring in background (Android)

只愿长相守 提交于 2019-11-30 07:55:07

You should hold BeaconManager in your application class not in the activity.

Activity will be stopped, destroyed and BeaconManager will stop monitoring. Application on the other hand will still hold reference and will continue to monitor.

When a beacon is being found while monitoring your application class can post a notification. It trigger some activity when user decides to tap on it.

You should create service.

In your code you should do some changes.

After

beaconManager = new BeaconManager(this);

you should start "beacon service"

      if (!beaconManager.isBluetoothEnabled()) {
          stopSelf();
      }

          beaconManager.connect(new BeaconManager.ServiceReadyCallback() {
              @Override
              public void onServiceReady() {
                try {
                  beaconManager.startRanging(ALL_ESTIMOTE_BEACONS_REGION);
                } catch (RemoteException e) {
                  Log.e("error", "Cannot start ranging", e);
                }
              }
          });

Also check if Bluetooth is active on you device.

In procedure beaconManager.setMonitoringListener(new MonitoringListener() add commands to create Notifications.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!