From fa5e6e94f37105b536acf8a7415c0cd7832912e6 Mon Sep 17 00:00:00 2001 From: DongWook Lee Date: Tue, 27 May 2014 17:47:50 +0900 Subject: [PATCH] fix issue #5 --- .../common_tools/rocon/AppLauncher.java | 26 ++--- .../common_tools/system/WifiChecker.java | 98 +++++++++++++------ .../NfcLauncherActivity.java | 8 +- 3 files changed, 89 insertions(+), 43 deletions(-) diff --git a/common_tools/src/main/java/com/github/rosjava/android_remocons/common_tools/rocon/AppLauncher.java b/common_tools/src/main/java/com/github/rosjava/android_remocons/common_tools/rocon/AppLauncher.java index 3534b66..405ed1b 100644 --- a/common_tools/src/main/java/com/github/rosjava/android_remocons/common_tools/rocon/AppLauncher.java +++ b/common_tools/src/main/java/com/github/rosjava/android_remocons/common_tools/rocon/AppLauncher.java @@ -40,7 +40,6 @@ import android.content.ActivityNotFoundException; import android.content.Intent; import android.net.Uri; -import android.os.AsyncTask; import android.util.Log; import android.util.Patterns; @@ -49,15 +48,11 @@ import org.yaml.snakeyaml.Yaml; -import java.io.IOException; -import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URISyntaxException; import java.net.URL; import java.net.URLEncoder; import java.util.Map; -import java.util.concurrent.TimeUnit; -import java.util.concurrent.TimeoutException; @@ -220,8 +215,12 @@ else if(checkAppType(app.getName()) == AppType.WEB_APP){ app_name = app.getName(); } - URL appURL = new URL(app_name); + URL appURL = new URL(app_name); + //2014.05.27 comment by dwlee + //reason of blocking, Not necessary in web app launcher. + /* + Log.i("AppLaunch", "Connection test (URI: " + app_name + ")"); AsyncTask asyncTask = new AsyncTask() { @Override protected String doInBackground(URL... urls) { @@ -236,10 +235,15 @@ protected String doInBackground(URL... urls) { } } }.execute(appURL); - String result = asyncTask.get(5, TimeUnit.SECONDS); + + String result = asyncTask.get(15, TimeUnit.SECONDS); + if (result == null || (result.startsWith("OK") == false && result.startsWith("ok") == false)) { + Log.i("AppLaunch", "Connection test Fail (URI: " + app_name + ")"); return Result.CANNOT_CONNECT.withMsg(result); } + Log.i("AppLaunch", "Connection test Success (URI: " + app_name + ")"); + */ // We pass concert URL, parameters and remaps as URL parameters String appUriStr = app_name; @@ -310,10 +314,10 @@ else if(app_type.equals("web_app")){ // This cannot happen for a web site, right? must mean that I have no web browser! return Result.NOT_INSTALLED.withMsg("Activity not found for view action??? muoia???"); } - catch (TimeoutException e) - { - return Result.CONNECT_TIMEOUT.withMsg("Timeout waiting for app"); - } + //catch (TimeoutException e) + //{ + // return Result.CONNECT_TIMEOUT.withMsg("Timeout waiting for app"); + //} catch (Exception e) { return Result.OTHER_ERROR.withMsg(e.getMessage()); diff --git a/common_tools/src/main/java/com/github/rosjava/android_remocons/common_tools/system/WifiChecker.java b/common_tools/src/main/java/com/github/rosjava/android_remocons/common_tools/system/WifiChecker.java index 9dbbff4..e2f4a1f 100644 --- a/common_tools/src/main/java/com/github/rosjava/android_remocons/common_tools/system/WifiChecker.java +++ b/common_tools/src/main/java/com/github/rosjava/android_remocons/common_tools/system/WifiChecker.java @@ -34,6 +34,7 @@ package com.github.rosjava.android_remocons.common_tools.system; +import android.net.wifi.ScanResult; import android.net.wifi.SupplicantState; import android.net.wifi.WifiConfiguration; import android.net.wifi.WifiInfo; @@ -42,6 +43,8 @@ import com.github.rosjava.android_remocons.common_tools.master.MasterId; +import java.util.List; + /** * Threaded WiFi checker. Checks and tests if the WiFi is configured properly and if not, connects to the correct network. * @@ -128,6 +131,17 @@ public static boolean wifiValid(MasterId masterId, WifiManager wifiManager) { return false; } + public String getScanResultSecurity(ScanResult scanResult) { + final String cap = scanResult.capabilities; + final String[] securityModes = { "WEP", "PSK", "EAP" }; + for (int i = securityModes.length - 1; i >= 0; i--) { + if (cap.contains(securityModes[i])) { + return securityModes[i]; + } + } + return "OPEN"; + } + private class CheckerThread extends Thread { private MasterId masterId; private WifiManager wifiManager; @@ -192,36 +206,62 @@ public void run() { //Add new network. if (n == -1) { Log.d("WiFiChecker", "WIFI Unknown"); - wc = new WifiConfiguration(); - wc.SSID = "\"" + masterId.getWifi() + "\""; - if (masterId.getWifiPassword() != null) { - wc.preSharedKey = "\"" + masterId.getWifiPassword() + "\""; - } else { - wc.preSharedKey = null; + + List scanResultList = null; + Log.d("WiFiChecker", "WIFI Scan Start"); + if(wifiManager.startScan()){ + Log.d("WiFiChecker", "WIFI Scan Success"); } - wc.hiddenSSID = true; - wc.status = WifiConfiguration.Status.DISABLED; - wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.LEAP); - wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); - wc.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); - wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); - wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); - wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); - wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); - wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); - wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.IEEE8021X); - wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); - wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); - wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); - wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); - wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); - wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); - - n = wifiManager.addNetwork(wc); - Log.d("WiFiChecker", "add Network returned " + n); - if (n == -1) { - failureCallback.handleFailure("Failed to configure WiFi"); + else{ + Log.d("WiFiChecker", "WIFI Scan Failure"); + failureCallback.handleFailure("wifi scan fail"); + } + + scanResultList = wifiManager.getScanResults(); + i = 0; + while (i < 30 && scanResultList.size()==0) { + scanResultList = wifiManager.getScanResults(); + Log.d("WiFiChecker", "Waiting for getting wifi list"); + Thread.sleep(1000L); + i++; } + wc = new WifiConfiguration(); + + for (ScanResult result : scanResultList) { + + if (result.SSID.equals(masterId.getWifi())) { + String securityMode = getScanResultSecurity(result); + Log.d("WiFiChecker", "WIFI mode: " + securityMode); + + wc.SSID = "\"" + masterId.getWifi() + "\""; + if (securityMode.equalsIgnoreCase("OPEN")) { + wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); + } else if (securityMode.equalsIgnoreCase("WEP")) { + wc.wepKeys[0] = "\"" + masterId.getWifiPassword() + "\""; + wc.wepTxKeyIndex = 0; + wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); + wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); + } else { + wc.preSharedKey = "\"" + masterId.getWifiPassword() + "\""; + wc.hiddenSSID = true; + wc.status = WifiConfiguration.Status.ENABLED; + wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); + wc.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); + wc.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); + wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); + wc.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); + wc.allowedProtocols.set(WifiConfiguration.Protocol.RSN); + wc.allowedProtocols.set(WifiConfiguration.Protocol.WPA); + } + n = wifiManager.addNetwork(wc); + break; + + } + } + } + Log.d("WiFiChecker", "add Network returned " + n); + if (n == -1) { + failureCallback.handleFailure("Failed to configure WiFi"); } //Connect to the network @@ -231,7 +271,7 @@ public void run() { wifiManager.reconnect(); Log.d("WiFiChecker", "Wait for wifi network"); i = 0; - while (i < 30 && !wifiValid()) { + while (i < 15 && !wifiValid()) { Log.d("WiFiChecker", "Waiting for network: " + i + " " + wifiManager.getWifiState()); Thread.sleep(1000L); i++; diff --git a/headless_launcher/src/main/java/com/github/rosjava/android_remocons/headless_launcher/NfcLauncherActivity.java b/headless_launcher/src/main/java/com/github/rosjava/android_remocons/headless_launcher/NfcLauncherActivity.java index 071376e..7422245 100644 --- a/headless_launcher/src/main/java/com/github/rosjava/android_remocons/headless_launcher/NfcLauncherActivity.java +++ b/headless_launcher/src/main/java/com/github/rosjava/android_remocons/headless_launcher/NfcLauncherActivity.java @@ -1,6 +1,5 @@ package com.github.rosjava.android_remocons.headless_launcher; - import android.app.Activity; import android.content.Context; import android.content.Intent; @@ -42,10 +41,10 @@ import static com.github.rosjava.android_remocons.common_tools.rocon.Constants.NFC_PAYLOAD_LENGTH; import static com.github.rosjava.android_remocons.common_tools.rocon.Constants.NFC_SSID_FIELD_LENGTH; - /** * @author jorge@yujinrobot.com (Jorge Santos Simon) */ + public class NfcLauncherActivity extends Activity { private enum Step { @@ -112,7 +111,7 @@ public void onCreate(Bundle savedInstanceState) { connectToSSID(); Log.i("NfcLaunch", "Connected to " + ssid); - toast("Connected to " + ssid, Toast.LENGTH_SHORT); + toast("Connected to " + ssid, Toast.LENGTH_LONG); //** Step 3. Validate the concert: check for specific topics on masterUri checkConcert(); @@ -180,6 +179,9 @@ private void parseNFCData() throws Exception { launchStep = launchStep.next(); } + + + private void connectToSSID() throws Exception { String masterUri = "http://" + masterHost + ":" + masterPort; String encryption = "WPA2"; // not needed