WiFi in Android - University of Delaware

WiFi in Android - University of Delaware

WiFi in Android 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 Include member variable WifiManager wifiManager; In onCreate, add wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE); 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 Scanning for access points 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 li = wifiManager.getScanResults(); for (int i=0; i

careful to not register twice Add member variable to WiFiFun boolean intentIsRegistered = false; // be sure to set this to true after registerReceiver(wifiEventReceiver, filter); in onCreate Add functions to WiFiFun @Override public void onResume() { super.onResume(); if (intentIsRegistered==false) { registerReceiver(wifiEventReceiver, filter); intentIsRegistered = true; } } @Override public void onPause() { super.onPause(); if (intentIsRegistered==true) { unregisterReceiver(wifiEventReceiver); intentIsRegistered = false; } } Connect to access point Add button

Connect to udel wifi In onCreate, add 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"); } else {

if (wifiManager.enableNetwork(newId,true)) { Log.e("DEBUG","enable connection succeded"); } else { Log.e("DEBUG","connect failed"); } } 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);

In the BroadcastReceiver, add 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))); } } Add function private String getSupplicantStateText(SupplicantState supplicantState) { if(SupplicantState.FOUR_WAY_HANDSHAKE.equals(supplicantState)) { return "FOUR WAY HANDSHAKE"; } else if(SupplicantState.ASSOCIATED.equals(supplicantState)) { return "ASSOCIATED"; } else if(SupplicantState.ASSOCIATING.equals(supplicantState)) { return "ASSOCIATING"; } else if(SupplicantState.COMPLETED.equals(supplicantState)) { return "COMPLETED"; } else if(SupplicantState.DISCONNECTED.equals(supplicantState)) {

return "DISCONNECTED"; } else if(SupplicantState.DORMANT.equals(supplicantState)) { return "DORMANT"; } else if(SupplicantState.GROUP_HANDSHAKE.equals(supplicantState)) { return "GROUP HANDSHAKE"; } else if(SupplicantState.INACTIVE.equals(supplicantState)) { return "INACTIVE"; } else if(SupplicantState.INVALID.equals(supplicantState)) { return "INVALID"; } else if(SupplicantState.SCANNING.equals(supplicantState)) { return "SCANNING"; } else if(SupplicantState.UNINITIALIZED.equals(supplicantState)) { return "UNINITIALIZED"; } else { return "supplicant state is bad"; } } 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"); }

} } // but this does not seem to work correctly 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"); } } Get current connection info Add button: Get Current In onCreate, add Button getbut = (Button)findViewById(R.id.ButtonGetCurrent); getbut.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) {

} }); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); Log.e("DEBUG","current ssid: "+wifiInfo.getSSID()); Log.e("DEBUG","current rssi: "+wifiInfo.getRssi()); Log.e("DEBUG","current mac: "+wifiInfo.getMacAddress()); Log.e("DEBUG","current net id: "+wifiInfo.getNetworkId()); Log.e("DEBUG","current bssid: "+wifiInfo.getBSSID()); Connect to ad hoc network Android does not support ad hoc networking Only rooted phones can connect Add BusyBox from market place We need a new wpa_supplicant.conf Wifi hardward <-> driver (.ko) <-> wpa_supplicant <-> (wpa_cli) <-> android system+api Make new app

Add button Connect to ad hoc Disconnect from ad hoc 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

Make ip from mac private String makeIPAddressFromMac(String mac) { Log.e("dEBUG","convert: "+mac); String delim = "[:]"; String[] tokens = mac.split(delim); int i1 = Integer.parseInt(tokens[4].trim(), 16); int i2 = Integer.parseInt(tokens[5].trim(), 16); Log.e("dEBUG","2nd "+tokens[4].trim()+" into "+i1); Log.e("dEBUG","2nd "+tokens[5].trim()+" into "+i2); //String address = "192.168."+i1+"."+i2; String address = "192.168.4."+i2; return address; } It would be better to embed the whole mac in an ipv6 address New wpa_supplicant.conf ##### wpa_supplicant configuration file template ##### update_config=1 ctrl_interface=eth0

eapol_version=1 ap_scan=2 fast_reauth=1 network={ ssid="MyAdHoc" scan_ssid=1 key_mgmt=NONE mode=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 Set static ip address Ad hoc mode needs a static ip address, since dhcp is most likely not available Here we get the mac address and build an IP address in subnet 192.168/16 from the MAC Dns is open-dns public void setUpStaticIP() { WifiInfo wifiInfo = wifiManager.getConnectionInfo(); Log.e("DEBUG","current mac: "+wifiInfo.getMacAddress()); String ip = makeIPAddressFromMac(wifiInfo.getMacAddress()); Settings.System.putString(getContentResolver(), Settings.System.WIFI_STATIC_IP, ip);

Settings.System.putString(getContentResolver(), Settings.System.WIFI_STATIC_NETMASK, "255.255.0.0"); Settings.System.putString(getContentResolver(), Settings.System.WIFI_STATIC_DNS1, "208.67.222.222"); Settings.System.putString(getContentResolver(), Settings.System.WIFI_STATIC_DNS2, "208.67.220.220"); Settings.System.putString(getContentResolver(), Settings.System.WIFI_STATIC_GATEWAY, "192.168.1.1"); Settings.System.putString(getContentResolver(), Settings.System.WIFI_USE_STATIC_IP, "1"); } public void clearStaticIP() { Settings.System.putString(getContentResolver(), Settings.System.WIFI_USE_STATIC_IP, 0"); } 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 ip 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; When getting out of ad hoc mode After turning off wifi, adHocState = 3; After turning on wifi, adHocState = 0; Check for wifi state changes 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 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; } } . Make button: Connect from ad hoc Button adHocOn = (Button)findViewById(R.id.AdHocOn); adHocOn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { final Runtime runtime = Runtime.getRuntime(); try { Process p =runtime.exec(new String[]{"/system/bin/su", "-c", "cp /mnt/sdcard/wpa_supplicant.conf_adHoc /data/misc/wifi/wpa_supplicant.conf"});

try { p.waitFor(); Process q = runtime.exec(new String[]{"/system/bin/su", "-c", "/system/xbin/chmod 777 /data/misc/wifi/wpa_supplicant.conf"}); q.waitFor(); if(wifiManager.isWifiEnabled()==true){ Log.e("DEBUG",into ad hoc, turning wifi off"); wifiManager.setWifiEnabled(false); adHocState = 1; } else { setUpStaticIP(); wifiManager.setWifiEnabled(true); Log.e("DEBUG",into ad hoc, turning wifi on"); adHocState = 2; } Process qq = runtime.exec(new String[]{"/system/bin/su", "-c", "chown system.wifi /data/misc/wifi/wpa_supplicant.conf"}); qq.waitFor(); } catch (InterruptedException e) {

Log.e("DEBUG","could not wait for copying wpa_supplicant.conf"); Log.e("DEBUG",e.getMessage()); e.printStackTrace(); } } catch (IOException e) { Log.e("DEBUG","exec failed"); e.printStackTrace(); Log.e("ERROR",e.getMessage()); } } }); Make button: disconnect from ad hoc Button adHocOff = (Button)findViewById(R.id.AdHocOff); adHocOff.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { final Runtime runtime = Runtime.getRuntime(); try { Process p =runtime.exec(new String[]{"/system/bin/su", "-c", "cp /mnt/sdcard/wpa_supplicant.conf_orig /data/misc/wifi/wpa_supplicant.conf"}); try {

p.waitFor(); Process q = runtime.exec(new String[]{"/system/bin/su", "-c", "/system/xbin/chmod 777 /data/misc/wifi/wpa_supplicant.conf"}); q.waitFor(); if(wifiManager.isWifiEnabled()==true){ Log.e("DEBUG",out of ad hoc, turning wifi off"); wifiManager.setWifiEnabled(false); adHocState = 3; } else { setUpStaticIP(); wifiManager.setWifiEnabled(true); Log.e("DEBUG",out of ad hoc, turning wifi on"); adHocState = 0; } Process qq = runtime.exec(new String[]{"/system/bin/su", "-c", "chown system.wifi /data/misc/wifi/wpa_supplicant.conf"}); qq.waitFor(); } catch (InterruptedException e) { Log.e("DEBUG","could not wait for copying wpa_supplicant.conf"); Log.e("DEBUG",e.getMessage()); e.printStackTrace(); }

} catch (IOException e) { Log.e("DEBUG","exec failed"); e.printStackTrace(); Log.e("ERROR",e.getMessage()); } } }); Adb shell Ping c 3 192.168.4.2

Recently Viewed Presentations

  • Structure of Carboxylic Acids - King Saud University

    Structure of Carboxylic Acids - King Saud University

    Structure of Carboxylic Acids. Depending on whether an R or an Ar. residue is attached to the carboxyl group; Carboxylic acids are classified as aliphatic or aromatic.. Aliphatic Carboxylic Acids. Aromatic Carboxylic Acids. Fatty acids. Long straight-chain carboxylic acids with...
  • Accounts Receivable and Billing - University of Michigan

    Accounts Receivable and Billing - University of Michigan

    Arial Garamond Times New Roman Wingdings Edge Microsoft Excel Worksheet Accounts Receivable and Billing (ARB) Agenda ARB Benefits ARB Key Concepts & Terms ARB Reports ARB Reports Statement of Activity (SOA) Standalone Project/Grant Example AR/Billing Detail Report Standalone Project/Grant Example...
  • Polityka Open Access w Europie i w Stanach

    Polityka Open Access w Europie i w Stanach

    As Peter Suber wrote in the SPARC Open Access Newsletter, issue #141, January 2, 2010: "The European Science Foundation (ESF) and the EuroHORCs plan to issue a joint OA mandate. The ESF represents 80 member organizations in 30 European countries.
  • Unit 12 9 Honors - kingphilip.org

    Unit 12 9 Honors - kingphilip.org

    Synonym: betrayal. Even though Tommy and Terry eventually made up, Tommy's perfidy impacted the friendship so strongly that the two boys were never as close again. Propensity. N . A natural inclination or tendency . Synonym: predisposition .
  • Who are you - Yale University

    Who are you - Yale University

    Every 14 seconds a child is orphaned by HIV LysenkoChem Traxoline ® LysenkoChem Traxoline® mode of action Current level of HIV resistance in the human population is low Traxoline® strengthens this resistance level HIV-specific immune enhancer LysenkoChem Protocol Each generation...
  • Overview - University of Missouri-St. Louis

    Overview - University of Missouri-St. Louis

    - Caroline Smith, Director of Business Intelligence - Travelocity A robust data warehouse Obtain the "single view of the customer" Six Key Success Factors Flexible and scalable CRM tools Define event and wait for it to occur Event triggers a...
  • 2011-2012 Special Education Paraprofessional After-School Training Series Adaptations

    2011-2012 Special Education Paraprofessional After-School Training Series Adaptations

    * Answer: Response Jacob answers questions orally and paraprofessional scribes his response. Jacob is permitted to use a computer to type his responses. Jacob's quiz provides a larger space for him to write his responses. ... Pennsylvania System of School...
  • Las Funciones Del Lenguaje

    Las Funciones Del Lenguaje

    Los verbos transitivos siempre pasan su acción sobre un OD. Beber se escribe con b. Los accidentes gramaticales del sustantivo son género y número. Función Metalingüística Se centra en el código, porque es la que reflexiona sobre el lenguaje mismo....