Android Beacon Library重启监控,动态区域

时间:2016-04-06 09:16:58

标签: android android-asynctask beacon

首先,如果您发现错误,请原谅我的英语并感谢您的帮助。我有一个程序使用API​​ REST服务来检索每隔X秒监视一次的区域列表。我正在使用Android Beacon Library监控区域,我希望能够停止监控这些区域,获取新列表,然后开始监控新区域。到目前为止,我有这个代码(简化):

public class MainActivity extends Activity implements BeaconConsumer {
  // Code
  Timer task = new TimerTask() {
    @Override
    public void run() {
      handler.post(new rRunnable() {
        @Override
        public void run() {
          new MyAsyncTask().execute(some_params);
        }
      });
    }
  }

  @Override
  protected void onCreate(Buncle savedInstanceState) {
    // Code
    beaconManager = BeaconManager.getInstanceForApplication(this);
    // Set parsers (iBeacon, Eddystone-UID, Eddystone-URL)
    beaconManager.bind(this);
    Log.i(TAG, "Using Android Beacon Library version: "+ org.altbeacon.beacon.BuildConfig.VERSION_NAME);
  }

  @Override
  public void onBeaconServiceConnect() {
    @Override
    public void didEnterRegion(Region region) {
      // Get info of regions and show a list of regions detected on UI
      Log.i(TAG, "Inside of: " + region.toString());
    }

    @Override
    public void didExitRegion(Region region) {
      // Get info of regions and show a list of regions detected on UI
      Log.i(TAG, "Outside of: " + region.toString());
    }

    @Override
    public void didDetermineStateForRegion(int state, Region region) {
      Log.i(TAG, "State: " + state);
    }

    // execute async task to stop monitoring, get new regions list and then
    // start monitoring again
    timer.schedule(task, 0, 30000); 
  }

  private class MyAsyncTask extends AyncTask<String, Void, List<Region>> {
    @Override
    protected List<Region> doInBackground(String... params) {
      // Get list of regions from server
    }

    @Override
    protected void onPostExecute(List<Region> result) {
      // For loop to stop monitoring all regions
      for (Region region : beaconManager.getMonitoredRegions()) {
        try {
          beaconManager.stopMonitoringBeaconsInRegion(region);
          Log.i(TAG, "Stop monitoring: " + region.toString());
        } catch (RemoteException e) { }
      }
      Log.i(TAG, "Nº of regions: " + beaconManager.getMonitoredRegions().size());
      // For loop to start monitoring new regions
      for (Region region : result) {
        try {
          beaconManager.startMonitoringBeaconsInRegion(region);
          Log.i(TAG, "Start monitoring: " + region.toString());
        } catch (RemoteException e) { }
      }
      Log.i(TAG, "Nº of regions: " + beaconManager.getMonitoredRegions().size());
    }
  }

代码中没有语法错误,应用程序不会崩溃。当我执行应用程序时,它会调用服务器,获取要监视的区域列表,然后开始监视区域。然后,当信标在范围内时,屏幕上会显示监控区域的列表。现在一切似乎都好。然后,应用程序停止监视区域,请求新的区域列表并开始监视新区域。我第一次尝试了3个区域,第二次尝试了2个,并且应用程序记录了它开始监视3个区域,然后停止监视3个区域,然后开始监视2个新区域。一切似乎都很好但是虽然它开始监控新区域,但当信标在范围内时,应用程序永远不会再次显示屏幕上监控的区域。

我想知道问题是否是我建立流程以获取区域并监控它们的方式。

似乎在使用新区域重新启动监视后,它不会触发didEnterRegion和didExitRegion。

编辑:我正在测试3个信标和3个区域的示例。第一个信标使用iBeacon,第二个使用Eddystone-UID,第三个使用Eddystone-URL。每个区域都配置为检测一个特定的信标。

Logcat(简化,信标和区域的ID都是正确的):

Using Android Beacon Library version: 2.7
Nº of regions: 0
Start monitoring: id1: ibeacon_id id2: ibeacon_major id3: ibeacon_minor
Start monitoring: id1: eddystone_namespace id2: eddystone_id id3: null
Start monitoring: id1: eddystone_url id2: null id3: null
Nº of regions: 3
State: 1
Inside of: id1: eddystone_url id2: null id3: null
State: 1
Inside of: id1: eddystone_namespace id2: eddystone_id id3: null
State: 1
Inside of: id1: ibeacon_id id2: ibeacon_major id3: ibeacon_minor

30 seconds later (more or less)...
I have not changed the regions provided by the server.

Stop monitoring: id1: ibeacon_id id2: ibeacon_major id3: ibeacon_minor
Stop monitoring: id1: eddystone_namespace id2: eddystone_id id3: null
Stop monitoring: id1: eddystone_url id2: null id3: null
Nº of regions: 0
Start monitoring: id1: ibeacon_id id2: ibeacon_major id3: ibeacon_minor
Start monitoring: id1: eddystone_namespace id2: eddystone_id id3: null
Start monitoring: id1: eddystone_url id2: null id3: null
Nº of regions: 3

30 seconds later (more or less)...

Stop monitoring: id1: ibeacon_id id2: ibeacon_major id3: ibeacon_minor
Stop monitoring: id1: eddystone_namespace id2: eddystone_id id3: null
Stop monitoring: id1: eddystone_url id2: null id3: null
Nº of regions: 0
Start monitoring: id1: ibeacon_id id2: ibeacon_major id3: ibeacon_minor
Start monitoring: id1: eddystone_namespace id2: eddystone_id id3: null
Start monitoring: id1: eddystone_url id2: null id3: null
Nº of regions: 3

Keep repeating...

正如我们所看到的,事件didEnterRegion,didExitRegion和didDetermineStateForRegion不再被触发。

1 个答案:

答案 0 :(得分:0)

Android Beacon Library的2.7版本中,有一个未被发现的(直到现在)错误,在所有地区停止扫描后停止在Android 5+设备上进行扫描。如果没有从服务中解除绑定并重新绑定到它,扫描将不会再次启动。

启用调试日志记录后,显示为:

04-11 08:35:07.655  D/BeaconService: stopMonitoring called
04-11 08:35:07.655  D/BeaconService: Currently monitoring 0 regions.
04-11 08:35:07.655  D/CycledLeScanner: stop called
04-11 08:35:07.655  D/CycledLeScanner: disabling scan
...
04-11 08:35:07.663  D/BeaconService: Currently monitoring 1 regions.
04-11 08:35:07.663  D/CycledLeScanner: start called
04-11 08:35:07.663  D/CycledLeScanner: scanning already started

在2.8版本中对其工作方式进行了更改,并且我已经验证2.8中不再存在该错误。 2.8版本,后半部分如下:

04-12 18:15:07.623  D/BeaconService: Currently monitoring 1 regions.
04-12 18:13:06.938  D/CycledLeScanner: start called
04-12 18:13:06.938  D/CycledLeScanner: starting a new scan cycle
04-12 18:13:06.939  D/CycledLeScanner: starting a new bluetooth le scan

使用2.8,您可以停止监控所有区域,开始监控新区域,如果信标位于附近,您将立即获得条目回调。简单的解决方案是通过JCenter或直接下载here.

升级到2.8