WifiChecker.java
Go to the documentation of this file.
00001 /*
00002  * Software License Agreement (BSD License)
00003  *
00004  * Copyright (c) 2011, Willow Garage, Inc.
00005  * All rights reserved.
00006  * Redistribution and use in source and binary forms, with or without
00007  * modification, are permitted provided that the following conditions
00008  * are met:
00009  *
00010  *  * Redistributions of source code must retain the above copyright
00011  *    notice, this list of conditions and the following disclaimer.
00012  *  * Redistributions in binary form must reproduce the above
00013  *    copyright notice, this list of conditions and the following
00014  *    disclaimer in the documentation and/or other materials provided
00015  *    with the distribution.
00016  *  * Neither the name of Willow Garage, Inc. nor the names of its
00017  *    contributors may be used to endorse or promote products derived
00018  *    from this software without specific prior written permission.
00019  *
00020  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00021  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00022  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
00023  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
00024  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
00025  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
00026  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00027  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00028  * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00029  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
00030  * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00031  * POSSIBILITY OF SUCH DAMAGE.
00032  */
00033 
00034 package com.github.rosjava.android_apps.application_management;
00035 
00036 import android.util.Log;
00037 import android.net.wifi.WifiManager;
00038 import android.net.wifi.WifiConfiguration;
00039 import android.net.wifi.SupplicantState;
00040 import android.net.wifi.WifiInfo;
00046 public class WifiChecker {
00047   public interface SuccessHandler {
00049     void handleSuccess();
00050   }
00051   public interface FailureHandler {
00056     void handleFailure(String reason);
00057   }
00058   public interface ReconnectionHandler {
00060     boolean doReconnection(String from, String to);
00061   }
00062   private CheckerThread checkerThread;
00063   private SuccessHandler foundWiFiCallback;
00064   private FailureHandler failureCallback;
00065   private ReconnectionHandler reconnectionCallback;
00067   public WifiChecker(SuccessHandler foundWiFiCallback, FailureHandler failureCallback, ReconnectionHandler reconnectionCallback) {
00068     this.foundWiFiCallback = foundWiFiCallback;
00069     this.failureCallback = failureCallback;
00070     this.reconnectionCallback = reconnectionCallback;
00071   }
00076   public void beginChecking(RobotId robotId, WifiManager manager) {
00077     stopChecking();
00078     //If there's no wifi tag in the robot id, skip this step
00079     if (robotId.getWifi() == null) {
00080       foundWiFiCallback.handleSuccess();
00081       return;
00082     }
00083     checkerThread = new CheckerThread(robotId, manager);
00084     checkerThread.start();
00085   }
00087   public void stopChecking() {
00088     if (checkerThread != null && checkerThread.isAlive()) {
00089       checkerThread.interrupt();
00090     }
00091   }
00092   public static boolean wifiValid(RobotId robotId, WifiManager wifiManager) {
00093     WifiInfo wifiInfo = wifiManager.getConnectionInfo();
00094     if (robotId.getWifi() == null) { //Does not matter what wifi network, always valid.
00095       return true;
00096     }
00097     if (wifiManager.isWifiEnabled()) {
00098       if (wifiInfo != null) {
00099         Log.d("WiFiChecker", "WiFi Info: " + wifiInfo.toString() + " IP " + wifiInfo.getIpAddress());
00100         if (wifiInfo.getSSID() != null && wifiInfo.getIpAddress() != 0
00101             && wifiInfo.getSupplicantState() == SupplicantState.COMPLETED) {
00102           if (wifiInfo.getSSID().equals(robotId.getWifi())) {
00103             return true;
00104           }
00105         }
00106       }
00107     }
00108     return false;
00109   }
00110   private class CheckerThread extends Thread {
00111     private RobotId robotId;
00112     private WifiManager wifiManager;
00113     public CheckerThread(RobotId robotId, WifiManager wifi) {
00114       this.robotId = robotId;
00115       this.wifiManager = wifi;
00116       setDaemon(true);
00117       // don't require callers to explicitly kill all the old checker threads.
00118       setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
00119         @Override
00120         public void uncaughtException(Thread thread, Throwable ex) {
00121           failureCallback.handleFailure("exception: " + ex.getMessage());
00122         }
00123       });
00124     }
00125     private boolean wifiValid() {
00126       return WifiChecker.wifiValid(robotId, wifiManager);
00127     }
00128     @Override
00129     public void run() {
00130       try {
00131         if (wifiValid()) {
00132           foundWiFiCallback.handleSuccess();
00133         } else if (reconnectionCallback.doReconnection(wifiManager.getConnectionInfo().getSSID(), robotId.getWifi())) {
00134           Log.d("WiFiChecker", "Wait for networking");
00135           wifiManager.setWifiEnabled(true);
00136           int i = 0;
00137           while (i < 30 && !wifiManager.isWifiEnabled()) {
00138             Log.d("WiFiChecker", "Waiting for WiFi enable");
00139             Thread.sleep(1000L);
00140             i++;
00141           }
00142           if (!wifiManager.isWifiEnabled()) {
00143             failureCallback.handleFailure("Un-able to connect to WiFi");
00144             return; 
00145           }
00146           int n = -1;
00147           int priority = -1;
00148           WifiConfiguration wc = null;
00149           String SSID = "\"" + robotId.getWifi() + "\"";
00150           for (WifiConfiguration test : wifiManager.getConfiguredNetworks()) {
00151             Log.d("WiFiChecker", "WIFI " + test.toString());
00152             if (test.priority > priority) {
00153               priority = test.priority;
00154             }
00155             if (test.SSID.equals(SSID)) {
00156               n = test.networkId;
00157               wc = test;
00158             }
00159           }
00160           if (wc != null) {
00161             if (wc.priority != priority) {
00162               wc.priority = priority + 1;
00163             }
00164             wc.status = WifiConfiguration.Status.DISABLED;
00165             wifiManager.updateNetwork(wc);
00166           }
00167        
00168           //Add new network.
00169           if (n == -1) {
00170             Log.d("WiFiChecker", "WIFI Unknown");
00171             wc = new WifiConfiguration();
00172             wc.SSID = "\"" + robotId.getWifi() + "\"";
00173             if (robotId.getWifiPassword() != null) {
00174               wc.preSharedKey  = "\"" + robotId.getWifiPassword() + "\"";
00175             } else {
00176               wc.preSharedKey = null;
00177             }
00178             wc.hiddenSSID = true;
00179             wc.status = WifiConfiguration.Status.DISABLED;
00180             wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.LEAP);
00181             wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN);
00182             wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED);
00183             wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP);
00184             wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP);
00185             wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104);
00186             wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40);
00187             wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
00188             wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X);
00189             wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP);
00190             wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK);
00191             wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP);
00192             wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP);
00193             wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN);
00194             wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA);
00195          
00196             n = wifiManager.addNetwork(wc);
00197             Log.d("WiFiChecker", "add Network returned " + n);
00198             if (n == -1) {
00199               failureCallback.handleFailure("Failed to configure WiFi");
00200             }
00201           }
00202          
00203           //Connect to the network
00204           boolean b = wifiManager.enableNetwork(n, true);
00205           Log.d("WiFiChecker", "enableNetwork returned " + b);
00206           if (b) {
00207             wifiManager.reconnect();
00208             Log.d("WiFiChecker", "Wait for wifi network");
00209             i = 0;
00210             while (i < 30 && !wifiValid()) {
00211               Log.d("WiFiChecker", "Waiting for network: " + i + " " + wifiManager.getWifiState());
00212               Thread.sleep(1000L);
00213               i++;
00214             }
00215             if (wifiValid()) {
00216               foundWiFiCallback.handleSuccess();
00217             } else {
00218               failureCallback.handleFailure("WiFi connection timed out");
00219             }
00220           }
00221         } else {
00222           failureCallback.handleFailure("Wrong WiFi network");
00223         }
00224       } catch (Throwable ex) {
00225         Log.e("RosAndroid", "Exception while searching for WiFi for "
00226               + robotId.getWifi(), ex);
00227         failureCallback.handleFailure(ex.toString());
00228       }
00229     }
00230   }
00231 }


android_apps
Author(s): Daniel Stonier , Kazuto Murase
autogenerated on Fri Aug 28 2015 10:04:40