Set an acquisition policy
We can set up a location services acquisition policy based on the requirements. For example, the policy could be set up to maximize positional accuracy, but with the capability of reducing accuracy if the device is known to be low on charge, to conserve battery usage.
An acquisition policy controls how data is collected from a sensor of a mobile device, using GPS positions and WiFi access points. To manage battery life appropriately, you should match the policy used to the needs. For example, while you might want to have a very accurate position for a geofence trigger, we may be able to save power using a different policy when the device is far away from the area of interest.
You set up an acquisition policy using the WL.Device.startAcquisition API.
We can specify a preset geo policy to use in the WL.Device.startAcquisition API. You do this using the WL.Device.Geo.Profiles API, in which we can specify one of the following functions, based on your requirements:
- LiveTracking. Use to get the most accurate and timely position information, but with heavy battery use.
- RoughTracking. Use to track devices, but when you do not need the most accurate or timely information. Use of power is less than for LiveTracking.
- PowerSaving. Use to get infrequent positional data at low accuracy levels, but with very good power conservation.
For information about the preset values for each function, see WL.Device.Geo.Profiles.
In addition to these three functions, we can specify many other configuration options as part of the WL.Device.startAcquisition API. At the most basic level, we can decide whether to allow for GPS use. This option is controlled by the enableHighAccuracy parameter. Note that you should set the permissions appropriately to use GPS. For information about permissions, see Location services permissions. If we decide not to use GPS, then a lower-power and less accurate position provider is used.
When the device for which you are acquiring data is plugged in, you might want to use the LiveTracking profile. Then, at different battery levels, switch to other options that save power. You might want similar behavior when the application goes to the background, or resumes. To fulfill these requirements, we can use Apache Cordova, and register for the appropriate event. Apache Cordova events provide you with the ability to monitor battery status, and respond appropriately based on the status. See Apache Cordova documentation at http://cordova.apache.org/docs/en/2.6.0/index.html, and search for "events".
- Decide on the requirements for the application policy.
- Optional: Call the WL.Device.Geo.Profiles API, specifying the required function.
- Optional: Use Apache Cordova to monitor your battery status.
- Call the WL.Device.startAcquisition API.
Example
In the code, triggers is a variable that stores the currently defined triggers, and failureFunctions is a variable that stores the functions to be called when acquisition fails.
For hybrid Android, iOS, or Windows Phone 8:
window.addEventListener("batterylow", goToPowerSaveMode, false); function goToPowerSaveMode() { WL.Device.startAcquisition( { Geo: WL.Device.Geo.Profiles.PowerSaving() }, triggers, failureFunctions ); }For native Android:
WLDevice wlDevice = WLClient.getInstance().getWLDevice(); wlDevice.startAcquisition( new WLLocationServicesConfiguration() .setPolicy(new WLAcquisitionPolicy() .setGeoPolicy(WLGeoAcquisitionPolicy.getPowerSavingProfile())) .setTriggers(triggers) .setFailureCallbacks(failureFunctions) );For native iOS:
id<WLDevice> wlDevice = [[WLClient sharedInstance] getWLDevice]; [ wlDevice startAcquisition: [ [ [ [[WLLocationServicesConfiguration alloc] init] setPolicy: [ [[WLAcquisitionPolicy alloc] init] setGeoPolicy: [WLGeoAcquisitionPolicy getPowerSavingProfile] ] ] setTriggers: triggers ] setFailureCallbacks: failureFunctions ] ];
Parent topic: Location services