Skip to content

Commit

Permalink
fix issue #5
Browse files Browse the repository at this point in the history
  • Loading branch information
dwlee committed May 27, 2014
1 parent a5f807a commit fa5e6e9
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 43 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;



Expand Down Expand Up @@ -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<URL, Void, String> asyncTask = new AsyncTask<URL, Void, String>() {
@Override
protected String doInBackground(URL... urls) {
Expand All @@ -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;
Expand Down Expand Up @@ -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());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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<ScanResult> 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
Expand All @@ -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++;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.github.rosjava.android_remocons.headless_launcher;


import android.app.Activity;
import android.content.Context;
import android.content.Intent;
Expand Down Expand Up @@ -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 [email protected] (Jorge Santos Simon)
*/

public class NfcLauncherActivity extends Activity {

private enum Step {
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit fa5e6e9

Please sign in to comment.