Wifi Connectivity Check fixed for HTC One S
This commit is contained in:
parent
4fc40bf02b
commit
221659c745
2 changed files with 41 additions and 27 deletions
|
@ -5,40 +5,54 @@ import android.content.BroadcastReceiver;
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.content.Intent;
|
import android.content.Intent;
|
||||||
import android.net.NetworkInfo;
|
import android.net.NetworkInfo;
|
||||||
import android.net.wifi.WifiConfiguration;
|
|
||||||
import android.net.wifi.WifiInfo;
|
import android.net.wifi.WifiInfo;
|
||||||
import android.net.wifi.WifiManager;
|
import android.net.wifi.WifiManager;
|
||||||
import android.view.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.Toast;
|
import android.widget.Toast;
|
||||||
|
|
||||||
/**
|
|
||||||
* Created by tim on 24.01.15.
|
|
||||||
*/
|
|
||||||
public class DerivBroadcastReceiver extends BroadcastReceiver {
|
public class DerivBroadcastReceiver extends BroadcastReceiver {
|
||||||
|
|
||||||
|
private final Context appContext;
|
||||||
|
private final String targetSSID;
|
||||||
|
|
||||||
|
|
||||||
|
public DerivBroadcastReceiver(Context appContext, String networkSSID) {
|
||||||
|
this.appContext = appContext;
|
||||||
|
this.targetSSID = networkSSID;
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onReceive(Context context, Intent intent) {
|
public void onReceive(Context context, Intent intent) {
|
||||||
final String action = intent.getAction();
|
// final String action = intent.getAction();
|
||||||
if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
|
|
||||||
|
WifiManager wifiManager = (WifiManager) appContext.getSystemService(Context.WIFI_SERVICE);
|
||||||
|
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
||||||
|
|
||||||
NetworkInfo nwInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
|
NetworkInfo nwInfo = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
|
||||||
if (nwInfo.getExtraInfo() != null && nwInfo.getExtraInfo().equals(MainActivity.networkSSID) && nwInfo.isConnected()) {
|
|
||||||
View rootView = ((Activity)context).getWindow().getDecorView().findViewById(android.R.id.content);
|
if (nwInfo != null && nwInfo.isConnectedOrConnecting()) {
|
||||||
Button button = (Button) rootView.findViewById(R.id.button2);
|
String ssid = wifiInfo.getSSID();
|
||||||
button.setEnabled(true);
|
|
||||||
button = (Button) rootView.findViewById(R.id.button3);
|
if (ssid.equals(targetSSID)){
|
||||||
button.setEnabled(true);
|
buttonActivator((Activity) context, true);
|
||||||
button = (Button) rootView.findViewById(R.id.button);
|
|
||||||
button.setEnabled(true);
|
|
||||||
Toast.makeText(context, context.getString(R.string.wlan_connected), Toast.LENGTH_SHORT).show();
|
Toast.makeText(context, context.getString(R.string.wlan_connected), Toast.LENGTH_SHORT).show();
|
||||||
} else {
|
} else {
|
||||||
View rootView = ((Activity)context).getWindow().getDecorView().findViewById(android.R.id.content);
|
buttonActivator((Activity) context, false);
|
||||||
Button button = (Button) rootView.findViewById(R.id.button2);
|
}
|
||||||
button.setEnabled(false);
|
|
||||||
button = (Button) rootView.findViewById(R.id.button3);
|
}
|
||||||
button.setEnabled(false);
|
}
|
||||||
|
|
||||||
|
private void buttonActivator(Activity context, boolean ssidTrue) {
|
||||||
|
View rootView = context.getWindow().getDecorView().findViewById(android.R.id.content);
|
||||||
|
Button button = (Button) rootView.findViewById(R.id.button_buzzer);
|
||||||
|
button.setEnabled(ssidTrue);
|
||||||
|
button = (Button) rootView.findViewById(R.id.button_door_open);
|
||||||
|
button.setEnabled(ssidTrue);
|
||||||
|
button = (Button) rootView.findViewById(R.id.button_door_unlock);
|
||||||
|
button.setEnabled(ssidTrue);
|
||||||
button = (Button) rootView.findViewById(R.id.button);
|
button = (Button) rootView.findViewById(R.id.button);
|
||||||
button.setEnabled(false);
|
button.setEnabled(ssidTrue);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -48,7 +48,7 @@ import java.util.List;
|
||||||
|
|
||||||
public class MainActivity extends ActionBarActivity {
|
public class MainActivity extends ActionBarActivity {
|
||||||
|
|
||||||
final static String networkSSID = "\"KrautSpace\"";
|
final static String networkSSID = "KrautSpace";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
|
@ -63,7 +63,7 @@ public class MainActivity extends ActionBarActivity {
|
||||||
IntentFilter intentFilter = new IntentFilter();
|
IntentFilter intentFilter = new IntentFilter();
|
||||||
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) ;
|
intentFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION) ;
|
||||||
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
intentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
|
||||||
registerReceiver(new DerivBroadcastReceiver(),intentFilter);
|
registerReceiver(new DerivBroadcastReceiver(this, networkSSID),intentFilter);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
Reference in a new issue