Back to project page yousense-android-tracker.
The source code is released under:
Energy-efficent motion and location tracker for Android. Based on Mattias's power and tracking work. I plan to release it as GPL, once I have a paper published that goes with it. Might also release i...
If you think the Android project yousense-android-tracker listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package com.linnap.locationtracker.wifi; /*from ww w .j a va 2s. c o m*/ import java.util.ArrayList; import java.util.List; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.wifi.ScanResult; import android.net.wifi.WifiManager; import android.os.Handler; import com.linnap.locationtracker.LocationTrackerService; import com.linnap.locationtracker.SensorConfig; /** * Wifi Scanner with timeouts. * This is a single-use class, create a new one each time. */ public class TimeoutScan { LocationTrackerService service; Handler handler; WifiManager wifiManager; WifiScanFinished listener; boolean scanStarted; boolean listenerCalled; public TimeoutScan(LocationTrackerService service, Handler handler, WifiScanFinished listener) { this.service = service; this.handler = handler; this.wifiManager = (WifiManager)service.getSystemService(Context.WIFI_SERVICE); this.listener = listener; this.scanStarted = false; this.listenerCalled = false; } /** * Starts the scan. Returns itself, for create and start() in one line. */ public synchronized TimeoutScan start() { if (scanStarted || listenerCalled) { throw new IllegalStateException("TimeoutScan is a single-use class."); } else { scanStarted = true; handler.postDelayed(timeout, SensorConfig.WIFI_SCAN_TIMEOUT_MILLIS); service.registerReceiver(scanResultsReceiver, new IntentFilter(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION), null, handler); boolean success = wifiManager.startScan(); // If startScan() fails, still not calling listener right now. Perhaps easier to think about threads. service.event("wifi_scan_started", new ScanStartedData(!success)); return this; } } public interface WifiScanFinished { public void wifiScanFinished(List<ScanResult> results, boolean failed); } /// Implementation private synchronized void reportResults(List<ScanResult> results, boolean failed) { if (!listenerCalled) { listenerCalled = true; // Unregister BroadcastReceiver only once service.unregisterReceiver(scanResultsReceiver); service.event("wifi_scan_results", new ScanResultsData(results, failed)); listener.wifiScanFinished(results, failed); // Do not bother unregistering timeout handler. } else { // Listener already called. Ignoring double reports. } } Runnable timeout = new Runnable() { public void run() { reportResults(new ArrayList<ScanResult>(), true); } }; BroadcastReceiver scanResultsReceiver = new BroadcastReceiver() { public void onReceive(Context _context, Intent intent) { List<ScanResult> results = wifiManager.getScanResults(); if (results == null) { reportResults(new ArrayList<ScanResult>(), true); } else { reportResults(results, false); } } }; }