Android Wi Fi
Android Wi Fi
Android Wi Fi
outline
Enabling wifi Scanning Adding a network Enabling and connecting to a network
Start WiFi
New App called WiFiFun
Include permissions: access_wifi_state, change_wifi_state, access_network_state, change_network_state, write_settings, write_secure_settings, change_wifi_multicast WifiManager wifiManager; wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
In onCreate, add Start wifi, add if(wifiManager.isWifiEnabled()==false){ Log.e("DEBUG","turning on wifi"); wifiManager.setWifiEnabled(true); } else { Log.e("DEBUG","wifi is on"); } switch (wifiManager.getWifiState()) { case WifiManager.WIFI_STATE_DISABLED: Log.e("DEBUG","wifi state is disabled"); break; case WifiManager.WIFI_STATE_DISABLING: Log.e("DEBUG","wifi state is WIFI_STATE_DISABLING"); break; case WifiManager.WIFI_STATE_ENABLED: Log.e("DEBUG","wifi state is WIFI_STATE_ENABLED"); break; case WifiManager.WIFI_STATE_ENABLING: Log.e("DEBUG","wifi state is WIFI_STATE_ENABLING"); break; case WifiManager.WIFI_STATE_UNKNOWN: Log.e("DEBUG","wifi state is WIFI_STATE_UNKNOWN"); break; }
Run
Approach: start wifi scanning, get results in broadcast receiver At the end of onCreate, addd if (wifiManager. startScan () == false) { Log.e("Error","Scanning could not start"); } else { Log.e("DEBUG","Scanning is started"); } Register to receive broadcast about scanning results Add member variable to WiFiFun IntentFilter filter; At the end of onCreate, add filter = new IntentFilter(); filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); registerReceiver(wifiEventReceiver, filter); Make broadcast receiver. Somewhere in WifiFun, add member variable private BroadcastReceiver wifiEventReceiver = new BroadcastReceiver() {}; // let eclipse add unimplemented methods In public void onReceive(Context arg0, Intent intent) {, add if(intent.getAction().equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { Log.e("DEBUG","SCAN_RESULTS_AVAILABLE_ACTION"); List<ScanResult> li = wifiManager.getScanResults(); for (int i=0; i<li.size(); i++) { Log.e("DEBUG","ssid: "+li.get(i).SSID+" bssid: "+li.get(i).BSSID+" cap: "+li.get(i).capabilities+" level: "+li.get(i).level+ "chan: "+li.get(i).frequency); } } Run Try adding wifiManager. startScan () to end of BroadcastReceiver .onReceive Notice that signal strength varies.
Connect to udel wifi Button wifiConnect = (Button)findViewById(R.id.WifiConnect); wifiConnect.setOnClickListener(new View.OnClickListener() {}); // let eclipse add onClick
In onClick add
Add new network to current list of networks
WifiConfiguration myWifCon = new WifiConfiguration(); myWifCon.SSID = "\ udel\""; myWifCon.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); myWifCon.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); myWifCon.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); myWifCon.status=WifiConfiguration.Status.ENABLED; int newId = wifiManager.addNetwork (myWifCon); if (newId<0) {
Log.e("debug","could not add wifi config"); if (wifiManager.enableNetwork(newId,true)) { Log.e("DEBUG","enable connection succeded"); } else { Log.e("DEBUG","connect failed"); }
} else {
Two things
This might not connect, e.g., maybe udel is out of range Perhaps we should not add udel to the list of networks
Add BroadcastReceiver
In onCreate, when the intentFilter is being made, add
filter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION); filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); filter.addAction(WifiManager.NETWORK_IDS_CHANGED_ACTION); filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); if(intent.getAction().equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) { Log.e("DEBUG","SUPPLICANT_STATE_CHANGED_ACTION"); if (intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_ERROR)) { Log.e("DEBUG","supplicant error"); } else { Log.e("DEBUG","supplicant state: "+getSupplicantStateText((SupplicantState) intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE))); } }
if(intent.getAction().equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) { Log.e("DEBUG","SUPPLICANT_CONNECTION_CHANGE_ACTION"); if (intent.hasExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED)) { if (intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false)==true) { Log.e("DEBUG","wifi is now connected"); } else { Log.e("DEBUG","wifi is now disconnected"); }
}
NETWORK_STATE_CHANGED_ACTION
if(intent.getAction().equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)){
Log.e("DEBUG",".NETWORK_STATE_CHANGED_ACTION"); NetworkInfo mNetworkInfo = (NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); if (mNetworkInfo.getState()==NetworkInfo.State.CONNECTED) {
Log.e("DEBUG","connected");
} if (mNetworkInfo.getState()==NetworkInfo.State.CONNECTING) {
Log.e("DEBUG","CONNECTING ");
} if (mNetworkInfo.getState()==NetworkInfo.State.DISCONNECTED) {
Log.e("DEBUG","DISCONNECTED");
} if (mNetworkInfo.getState()==NetworkInfo.State.DISCONNECTING) {
Log.e("DEBUG","DISCONNECTING");
} if (mNetworkInfo.getState()==NetworkInfo.State.SUSPENDED) {
Log.e("DEBUG","SUSPENDED");
} if (mNetworkInfo.getState()==NetworkInfo.State.SUSPENDED) {
Log.e("DEBUG","UNKNOWN");
});
steps
Move wpa_supplicant to correct directory Change permissions of wpa_supplicant Disable dhcp Set ip address etc Turn off wifi
(Perhap we could only restart wpa_supplicant)
Set static ip Turn on wifi Check status Set routing table Try ping
New wpa_supplicant.conf
##### wpa_supplicant configuration file template ##### update_config=1 ctrl_interface=eth0 eapol_version=1 ap_scan=2 fast_reauth=1
Key things: ap_scan=2 (original has ap_scan=1), this allows connection to ad hoc and only MyAdHoc in list copy this to /mnt/sdcard/wpa_supplicant.conf_adHoc copy original to /mnt/sdcard/wpa_supplicant.conf_orig
steps
Move wpa_supplicant to correct directory Change permissions of wpa_supplicant Disable dhcp Set ip address etc Turn off wifi
(Perhap we could only restart wpa_supplicant) Trick: we want to set ip after wifi is off, which occurs well after we issue the command to turn off wifi Approach:
Add member variable int adHocState = 0; When getting into ad hoc mode
After turning off wifi, adHocState = 1; After turning on wifi, adHocState = 2; After turning off wifi, adHocState = 3; After turning on wifi, adHocState = 0; If state change and adHocState==1, then set ip, adHocState =2, turn on wifi If state change and adHocState==3, then clear static ip, adHocState =0, turn on wifi
Set ip
Turn on wifi Check status Try ping multi-hop Ad hoc networking - next week
Add member variable int adHocState = 0; private BroadcastReceiver wifiEventReceiver = new BroadcastReceiver() {
@Override public void onReceive(Context arg0, Intent intent) { if (adHocState==1) { setUpStaticIP(); wifiManager.setWifiEnabled(true); Log.e("DEBUG", into ad hoc, turning wifi on"); adHocState = 2; } if (adHocState==3) { clearStaticIP(); wifiManager.setWifiEnabled(true); Log.e("DEBUG", out of ad hoc, turning wifi on"); adHocState = 0; }
} .
} catch (InterruptedException e) {
} } catch (IOException e) {
Log.e("DEBUG","exec failed"); e.printStackTrace(); Log.e("ERROR",e.getMessage());
});
} catch (InterruptedException e) {
} } catch (IOException e) {
Log.e("DEBUG","exec failed"); e.printStackTrace(); Log.e("ERROR",e.getMessage());
});