Question

Normally, it takes 1sec to have a new scan.
If the scanning is 1sec, I may not do too much things if I want to parse the data.
How to change that value?

Also, for the background service, how to change the value of freq ?

Was it helpful?

Solution

If you want to change the Android iBeacon Library's default scan interval, you can do so with code like this:

 try {
     iBeaconManager.setForegroundScanPeriod(1100l); // 1100 mS
     iBeaconManager.setForegroundBetweenScanPeriod(0l); // 0ms
     iBeaconManager.updateScanPeriods();
  }
  catch (RemoteException e) {
     Log.e(TAG, "Cannot talk to service");
  }

The code above sets the scans to last 1100ms and wait for 0ms between scans. This is the default behavior.

If you wish to slow down the scans, you generally set the betweenScanPeriod to a nonzero value, for example 30 seconds. When you do this, you should set the scanPeriod to be at least the interval that your iBeacons are broadcasting -- 1100ms is generally sufficient to catch iBeacons advertising at a 1Hz rate.

 try {
     iBeaconManager.setForegroundScanPeriod(1100l); // 1100 mS
     iBeaconManager.setForegroundBetweenScanPeriod(30000l); // 30,000ms = 30 seconds
     iBeaconManager.updateScanPeriods();
  }
  catch (RemoteException e) {
     Log.e(TAG, "Cannot talk to service");
  }

It is worth noting that there are actually two such settings on the iBeaconManager, one for the foreground, and one for the background. This is purely for convenience, because you generally want scans to happen less frequently when your app is in the background. Whether you customize both the foreground and background settings or accept the defaults, you can activate the background mode settings with:

 try {
     iBeaconManager.setBackgroundMode(myActivity, true);
  }
  catch (RemoteException e) {
     Log.e(TAG, "Cannot talk to service");
  }

There is also a Pro version of the Android iBeacon Library which builds on the above to automatically put your app into foreground or background mode depending on whether an Activity is visible. This is the easiest way to set up background power savings on Android.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top