diff --git a/app/app.iml b/app/app.iml index ea0499c..30d0ce8 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 99b3aa0..1ada524 100644 --- a/app/src/main/java/me/datenknoten/tueroeffner/MainActivity.java +++ b/app/src/main/java/me/datenknoten/tueroeffner/MainActivity.java @@ -63,6 +63,7 @@ public class MainActivity extends ActionBarActivity { final static String networkSSID = "KrautSpace"; private DerivBroadcastReceiver receiver = null; + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -73,6 +74,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); @@ -146,7 +148,7 @@ public class MainActivity extends ActionBarActivity { }); Switch s = (Switch) rootView.findViewById(R.id.switchWLAN); - + // if (s != null) { s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() { @Override @@ -155,34 +157,18 @@ public class MainActivity extends ActionBarActivity { Context context = rootView.getContext(); WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (isChecked) { - WifiConfiguration conf = new WifiConfiguration(); - conf.SSID = escaped_ssid; - conf.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); - int retval = wifiManager.addNetwork(conf); - List list = wifiManager.getConfiguredNetworks(); - for (WifiConfiguration i : list) { - if (i.SSID != null && i.SSID.equals(escaped_ssid)) { - wifiManager.enableNetwork(i.networkId, false); - } else { - wifiManager.disableNetwork(i.networkId); - } - } + WifiConfiguration wifiConfig = new WifiConfiguration(); + wifiConfig.SSID = String.format("\"%s\"", networkSSID); + + int netId = wifiManager.addNetwork(wifiConfig); wifiManager.disconnect(); + wifiManager.enableNetwork(netId, true); wifiManager.reconnect(); + Toast.makeText(buttonView.getContext(), getString(R.string.wlan_activated), Toast.LENGTH_SHORT).show(); } else { - List list = wifiManager.getConfiguredNetworks(); - for (WifiConfiguration i : list) { - if (i.SSID != null && i.SSID.equals(escaped_ssid)) { - wifiManager.removeNetwork(i.networkId); - } else { - wifiManager.enableNetwork(i.networkId, false); - } - } - wifiManager.disconnect(); - wifiManager.reconnect(); Toast.makeText(buttonView.getContext(), getString(R.string.wlan_deactivated), Toast.LENGTH_SHORT).show(); } }