From f7097d0cb5640ebc9e385a30378557d52515ddbb Mon Sep 17 00:00:00 2001 From: Gerome Bochmann Date: Tue, 24 Feb 2015 21:02:37 +0100 Subject: [PATCH 1/2] Fixed API lvl < 17 quotation mark issue and wifi deactivation on CompoundButton isChecked==true --- app/app.iml | 4 +++- .../tueroeffner/DerivBroadcastReceiver.java | 7 +++++++ .../datenknoten/tueroeffner/MainActivity.java | 20 ++++++++++++++----- 3 files changed, 25 insertions(+), 6 deletions(-) diff --git a/app/app.iml b/app/app.iml index f15f948..846b0ee 100644 --- a/app/app.iml +++ b/app/app.iml @@ -1,5 +1,5 @@ - + @@ -9,6 +9,7 @@ + diff --git a/app/src/main/java/me/datenknoten/tueroeffner/DerivBroadcastReceiver.java b/app/src/main/java/me/datenknoten/tueroeffner/DerivBroadcastReceiver.java index 88e14a9..8d9a2d6 100644 --- a/app/src/main/java/me/datenknoten/tueroeffner/DerivBroadcastReceiver.java +++ b/app/src/main/java/me/datenknoten/tueroeffner/DerivBroadcastReceiver.java @@ -7,6 +7,7 @@ import android.content.Intent; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.net.wifi.WifiManager; +import android.os.Build; import android.view.View; import android.widget.Button; import android.widget.Toast; @@ -34,6 +35,12 @@ public class DerivBroadcastReceiver extends BroadcastReceiver { if (nwInfo != null && nwInfo.isConnectedOrConnecting()) { String ssid = wifiInfo.getSSID(); + + // Build.VERSION.SDK_INT is incompatible with device API lvl < 4 use String.toInteger(Build.VERSION.SDK) or maybe find some other way ^_^ + if (Build.VERSION.SDK_INT > 17){ + targetSSID.replace("\"", ""); + } + if (ssid.equals(targetSSID)){ buttonActivator((Activity) context, true); Toast.makeText(context, context.getString(R.string.wlan_connected), Toast.LENGTH_SHORT).show(); diff --git a/app/src/main/java/me/datenknoten/tueroeffner/MainActivity.java b/app/src/main/java/me/datenknoten/tueroeffner/MainActivity.java index d6b1e9a..090b22f 100644 --- a/app/src/main/java/me/datenknoten/tueroeffner/MainActivity.java +++ b/app/src/main/java/me/datenknoten/tueroeffner/MainActivity.java @@ -48,7 +48,7 @@ import java.util.List; public class MainActivity extends ActionBarActivity { - final static String networkSSID = "KrautSpace"; + static String networkSSID = "KrautSpace"; @Override protected void onCreate(Bundle savedInstanceState) { @@ -60,6 +60,7 @@ public class MainActivity extends ActionBarActivity { .commit(); } + IntentFilter intentFilter = new IntentFilter(); intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) ; intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); @@ -132,7 +133,16 @@ public class MainActivity extends ActionBarActivity { Context context = rootView.getContext(); WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE); if (isChecked) { - WifiConfiguration conf = new WifiConfiguration(); + + WifiConfiguration wifiConfig = new WifiConfiguration(); + wifiConfig.SSID = String.format("\"%s\"", networkSSID); + + int netId = wifiManager.addNetwork(wifiConfig); + wifiManager.disconnect(); + wifiManager.enableNetwork(netId, true); + wifiManager.reconnect(); + + /*WifiConfiguration conf = new WifiConfiguration(); conf.SSID = networkSSID; conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); int retval = wifiManager.addNetwork(conf); @@ -147,10 +157,10 @@ public class MainActivity extends ActionBarActivity { } } wifiManager.disconnect(); - wifiManager.reconnect(); + wifiManager.reconnect();*/ Toast.makeText(buttonView.getContext(), getString(R.string.wlan_activated), Toast.LENGTH_SHORT).show(); } else { - List list = wifiManager.getConfiguredNetworks(); + /*List list = wifiManager.getConfiguredNetworks(); for( WifiConfiguration i : list ) { if(i.SSID != null && i.SSID.equals(networkSSID)) { wifiManager.removeNetwork(i.networkId); @@ -159,7 +169,7 @@ public class MainActivity extends ActionBarActivity { } } wifiManager.disconnect(); - wifiManager.reconnect(); + wifiManager.reconnect();*/ Toast.makeText(buttonView.getContext(), getString(R.string.wlan_deactivated), Toast.LENGTH_SHORT).show(); } } From a545bf8a2c8571358f296f4555c58f2aa782cc32 Mon Sep 17 00:00:00 2001 From: Gerome Bochmann Date: Tue, 24 Feb 2015 21:04:15 +0100 Subject: [PATCH 2/2] Fixed API lvl < 17 quotation mark issue and wifi deactivation on CompoundButton isChecked==true --- app/src/main/java/me/datenknoten/tueroeffner/MainActivity.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/src/main/java/me/datenknoten/tueroeffner/MainActivity.java b/app/src/main/java/me/datenknoten/tueroeffner/MainActivity.java index 090b22f..a37c1f8 100644 --- a/app/src/main/java/me/datenknoten/tueroeffner/MainActivity.java +++ b/app/src/main/java/me/datenknoten/tueroeffner/MainActivity.java @@ -125,7 +125,7 @@ public class MainActivity extends ActionBarActivity { }); Switch s = (Switch) rootView.findViewById(R.id.switchWLAN); - + // if (s != null) { s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override