Friday, April 3, 2015

WiFiBeacon demo – sources

I’ve published source codes of WiFiBeacon app from the previous post. Any comments and pull requests are welcome! But first, let me guide you through the app structure a little bit. There are few classes worth mentioning.

This class is used to decorate ordinary ScanResult with RSSI averaging capabilities. The algorithm used here is a modified version of averaging algorithm from newer versions of Android.  Advantage of averaging RSSI is a slightly better performance gain when calculating range from AP while standing at one place. This technique is useful only in quick successive scans.

ScanFilter is the main class for filtering scan results. As described earlier, ScanResults are filtered by their BSSID (MAC), SSID, channels, and/or calculated proximity. This filtering may be based either on equality of filtered parameters, or on prefix equality. Note that adding a list of filtering BSSIDs and SSIDs is not allowed, since it disallows use of general filtering parameters and would bloat the code with ineffective iterating over the list of (B)SSIDs.

Proximity is calculated using Free-space path loss formula. Since in “real” environments, it is almost impossible to achieve clear line of sight to an AP, calculated results should be considered rough estimates, rather than accurate means of distance.

ProximityScanner is the core class for performing WiFi scans. You can set it to monitor and/or range surrounding WiFiBeacons. Ranging is intended to be used in situations, when we care simply for delivery of filtered ScanResults, while monitoring should be used with notifications, since it’s meant to delimit regions/areas/zones defined by ScanFilter.  Notifications are: onEnterRegion – used when you enter region specified by ScanFilter for the first time; onDwellRegion – used when persisting in the same region; onExitRegion – used when no APs matching ScanFilter are found. Note that in Activities, ProximityScanner’s start and stop methods should be called in corresponding lifecycle methods.


This service is an example of performing WiFiBeacon monitoring in background. It’s activated on boot or on WiFi enable event. On APIs < 18 we need enabled WiFi and wake lock for scanning, otherwise we just need isScanAlwaysAvailable activated, then no special permissions are needed.


Post a Comment