FileDocCategorySizeDatePackage
WifiScanner.javaAPI DocAndroid 5.1 API32887Thu Mar 12 22:22:44 GMT 2015android.net.wifi

WifiScanner

public class WifiScanner extends Object
This class provides a way to scan the Wifi universe around the device Get an instance of this class by calling {@link android.content.Context#getSystemService(String) Context.getSystemService(Context .WIFI_SCANNING_SERVICE)}.
hide

Fields Summary
public static final int
WIFI_BAND_UNSPECIFIED
no band specified; use channel list instead
public static final int
WIFI_BAND_24_GHZ
2.4 GHz band
public static final int
WIFI_BAND_5_GHZ
5 GHz band excluding DFS channels
public static final int
WIFI_BAND_5_GHZ_DFS_ONLY
DFS channels from 5 GHz band only
public static final int
WIFI_BAND_5_GHZ_WITH_DFS
5 GHz band including DFS channels
public static final int
WIFI_BAND_BOTH
Both 2.4 GHz band and 5 GHz band; no DFS channels
public static final int
WIFI_BAND_BOTH_WITH_DFS
Both 2.4 GHz band and 5 GHz band; with DFS channels
public static final int
MIN_SCAN_PERIOD_MS
Minimum supported scanning period
public static final int
MAX_SCAN_PERIOD_MS
Maximum supported scanning period
public static final int
REASON_SUCCEEDED
No Error
public static final int
REASON_UNSPECIFIED
Unknown error
public static final int
REASON_INVALID_LISTENER
Invalid listener
public static final int
REASON_INVALID_REQUEST
Invalid request
public static final int
REASON_NOT_AUTHORIZED
Invalid request
public static final String
GET_AVAILABLE_CHANNELS_EXTRA
public static final int
REPORT_EVENT_AFTER_BUFFER_FULL
reports {@link ScanListener#onResults} when underlying buffers are full
public static final int
REPORT_EVENT_AFTER_EACH_SCAN
reports {@link ScanListener#onResults} after each scan
public static final int
REPORT_EVENT_FULL_SCAN_RESULT
reports {@link ScanListener#onFullResult} whenever each beacon is discovered
private static final String
TAG
private static final boolean
DBG
private static final int
BASE
public static final int
CMD_SCAN
public static final int
CMD_START_BACKGROUND_SCAN
public static final int
CMD_STOP_BACKGROUND_SCAN
public static final int
CMD_GET_SCAN_RESULTS
public static final int
CMD_SCAN_RESULT
public static final int
CMD_SET_HOTLIST
public static final int
CMD_RESET_HOTLIST
public static final int
CMD_AP_FOUND
public static final int
CMD_AP_LOST
public static final int
CMD_START_TRACKING_CHANGE
public static final int
CMD_STOP_TRACKING_CHANGE
public static final int
CMD_CONFIGURE_WIFI_CHANGE
public static final int
CMD_WIFI_CHANGE_DETECTED
public static final int
CMD_WIFI_CHANGES_STABILIZED
public static final int
CMD_OP_SUCCEEDED
public static final int
CMD_OP_FAILED
public static final int
CMD_PERIOD_CHANGED
public static final int
CMD_FULL_SCAN_RESULT
private android.content.Context
mContext
private IWifiScanner
mService
private static final int
INVALID_KEY
private static int
sListenerKey
private static final android.util.SparseArray
sListenerMap
private static final Object
sListenerMapLock
private static com.android.internal.util.AsyncChannel
sAsyncChannel
private static CountDownLatch
sConnected
private static final Object
sThreadRefLock
private static int
sThreadRefCount
private static android.os.HandlerThread
sHandlerThread
Constructors Summary
public WifiScanner(android.content.Context context, IWifiScanner service)
Create a new WifiScanner instance. Applications will almost always want to use {@link android.content.Context#getSystemService Context.getSystemService()} to retrieve the standard {@link android.content.Context#WIFI_SERVICE Context.WIFI_SERVICE}.

param
context the application context
param
service the Binder interface
hide


                                          
         
        mContext = context;
        mService = service;
        init();
    
Methods Summary
public voidconfigureWifiChange(int rssiSampleSize, int lostApSampleSize, int unchangedSampleSize, int minApsBreachingThreshold, int periodInMs, android.net.wifi.WifiScanner$BssidInfo[] bssidInfos)
configure WifiChange detection

param
rssiSampleSize number of samples used for RSSI averaging
param
lostApSampleSize number of samples to confirm an access point's loss
param
unchangedSampleSize number of samples to confirm there are no changes
param
minApsBreachingThreshold minimum number of access points that need to be out of range to detect WifiChange
param
periodInMs indicates period of scan to find changes
param
bssidInfos access points to watch


    

                                                                                                           
      
                                          /* sample size for RSSI averaging */
                                        /* samples to confirm AP's loss */
                                     /* samples to confirm no change */
                                /* change threshold to trigger event */
                                              /* period of scan */
                                       /* signal thresholds to crosss */
            
    
        validateChannel();

        WifiChangeSettings settings = new WifiChangeSettings();
        settings.rssiSampleSize = rssiSampleSize;
        settings.lostApSampleSize = lostApSampleSize;
        settings.unchangedSampleSize = unchangedSampleSize;
        settings.minApsBreachingThreshold = minApsBreachingThreshold;
        settings.periodInMs = periodInMs;
        settings.bssidInfos = bssidInfos;

        configureWifiChange(settings);
    
public voidconfigureWifiChange(android.net.wifi.WifiScanner$WifiChangeSettings settings)

hide

        validateChannel();
        sAsyncChannel.sendMessage(CMD_CONFIGURE_WIFI_CHANGE, 0, 0, settings);
    
public java.util.ListgetAvailableChannels(int band)
gives you all the possible channels; channel is specified as an integer with frequency in MHz i.e. channel 1 is 2412

hide


                
    
        
          
             
    

                               
        
        try {
            Bundle bundle =  mService.getAvailableChannels(band);
            return bundle.getIntegerArrayList(GET_AVAILABLE_CHANNELS_EXTRA);
        } catch (RemoteException e) {
            return null;
        }
    
private static java.lang.ObjectgetListener(int key)

        if (key == INVALID_KEY) return null;
        synchronized (sListenerMapLock) {
            Object listener = sListenerMap.get(key);
            return listener;
        }
    
private static intgetListenerKey(java.lang.Object listener)

        if (listener == null) return INVALID_KEY;
        synchronized (sListenerMapLock) {
            int index = sListenerMap.indexOfValue(listener);
            if (index == -1) {
                return INVALID_KEY;
            } else {
                return sListenerMap.keyAt(index);
            }
        }
    
public ScanResult[]getScanResults()
retrieves currently available scan results

        validateChannel();
        Message reply = sAsyncChannel.sendMessageSynchronously(CMD_GET_SCAN_RESULTS, 0);
        ScanResult[] results = (ScanResult[]) reply.obj;
        return results;
    
private voidinit()

        synchronized (sThreadRefLock) {
            if (++sThreadRefCount == 1) {
                Messenger messenger = null;
                try {
                    messenger = mService.getMessenger();
                } catch (RemoteException e) {
                    /* do nothing */
                } catch (SecurityException e) {
                    /* do nothing */
                }

                if (messenger == null) {
                    sAsyncChannel = null;
                    return;
                }

                sHandlerThread = new HandlerThread("WifiScanner");
                sAsyncChannel = new AsyncChannel();
                sConnected = new CountDownLatch(1);

                sHandlerThread.start();
                Handler handler = new ServiceHandler(sHandlerThread.getLooper());
                sAsyncChannel.connect(mContext, handler, messenger);
                try {
                    sConnected.await();
                } catch (InterruptedException e) {
                    Log.e(TAG, "interrupted wait at init");
                }
            }
        }
    
private static intputListener(java.lang.Object listener)

        if (listener == null) return INVALID_KEY;
        int key;
        synchronized (sListenerMapLock) {
            do {
                key = sListenerKey++;
            } while (key == INVALID_KEY);
            sListenerMap.put(key, listener);
        }
        return key;
    
private static java.lang.ObjectremoveListener(int key)

        if (key == INVALID_KEY) return null;
        synchronized (sListenerMapLock) {
            Object listener = sListenerMap.get(key);
            sListenerMap.remove(key);
            return listener;
        }
    
private static intremoveListener(java.lang.Object listener)

        int key = getListenerKey(listener);
        if (key == INVALID_KEY) return key;
        synchronized (sListenerMapLock) {
            sListenerMap.remove(key);
            return key;
        }
    
public voidstartBackgroundScan(android.net.wifi.WifiScanner$ScanSettings settings, android.net.wifi.WifiScanner$ScanListener listener)
start wifi scan in background

param
settings specifies various parameters for the scan; for more information look at {@link ScanSettings}
param
listener specifies the object to report events to. This object is also treated as a key for this scan, and must also be specified to cancel the scan. Multiple scans should also not share this object.

    

                    
         
                                              
           
                       
           
                            
           
    

                                                                                                  
          
        validateChannel();
        sAsyncChannel.sendMessage(CMD_START_BACKGROUND_SCAN, 0, putListener(listener), settings);
    
public voidstartTrackingBssids(android.net.wifi.WifiScanner$BssidInfo[] bssidInfos, int apLostThreshold, android.net.wifi.WifiScanner$BssidListener listener)
set interesting access points to find

param
bssidInfos access points of interest
param
apLostThreshold number of scans needed to indicate that AP is lost
param
listener object provided to report events on; this object must be unique and must also be provided on {@link #stopTrackingBssids}

    

                                                                      
       
                                        
        validateChannel();
        HotlistSettings settings = new HotlistSettings();
        settings.bssidInfos = bssidInfos;
        sAsyncChannel.sendMessage(CMD_SET_HOTLIST, 0, putListener(listener), settings);
    
public voidstartTrackingWifiChange(android.net.wifi.WifiScanner$WifiChangeListener listener)
track changes in wifi environment

param
listener object to report events on; this object must be unique and must also be provided on {@link #stopTrackingWifiChange}

        validateChannel();
        sAsyncChannel.sendMessage(CMD_START_TRACKING_CHANGE, 0, putListener(listener));
    
public voidstopBackgroundScan(android.net.wifi.WifiScanner$ScanListener listener)
stop an ongoing wifi scan

param
listener specifies which scan to cancel; must be same object as passed in {@link #startBackgroundScan}

        validateChannel();
        sAsyncChannel.sendMessage(CMD_STOP_BACKGROUND_SCAN, 0, removeListener(listener));
    
public voidstopTrackingBssids(android.net.wifi.WifiScanner$BssidListener listener)
remove tracking of interesting access points

param
listener same object provided in {@link #startTrackingBssids}

        validateChannel();
        sAsyncChannel.sendMessage(CMD_RESET_HOTLIST, 0, removeListener(listener));
    
public voidstopTrackingWifiChange(android.net.wifi.WifiScanner$WifiChangeListener listener)
stop tracking changes in wifi environment

param
listener object that was provided to report events on {@link #stopTrackingWifiChange}

        validateChannel();
        sAsyncChannel.sendMessage(CMD_STOP_TRACKING_CHANGE, 0, removeListener(listener));
    
private voidvalidateChannel()

        if (sAsyncChannel == null) throw new IllegalStateException(
                "No permission to access and change wifi or a bad initialization");