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

  • Microbial Metabolism Lecture PowerPoint

    Microbial Metabolism Lecture PowerPoint

    Virtual Microbiology Classroom. on . ScienceProfOnline.com. Homework Assignment; At the end of some lectures, I will give you some type of homework to evaluate your understanding of that day's material. This homework will always be open-book.
  • Chapter

    Chapter

    C H A P T E R 5 Graded Exercise Testing and Exercise Prescription Keteyian Chapter 05 * * * * * * * * * * * * * * ECG Responses During Exercise Rate Amplitude of waves Normal:...
  • Advent 2019 Children&#x27;s Liturgy Resource

    Advent 2019 Children's Liturgy Resource

    Help us to be people of hope. We await your coming with gratitude. Amen. Everyone gathers around the wreath and someone lights the appropriate candle(s). Introduce the wreath to the children. Light the first candle, the candle of hope. ......
  • Chapter 6

    Chapter 6

    Although many individuals were associated with the military cadre during its 1803-1804 initial stages of travel from Pittsburgh, Pennsylvania to Fort Mandan, North Dakota, only those 33 members who journeyed from Fort Mandan to Fort Clatsop, Oregon, and returned comprised...
  • Land Planning Concepts

    Land Planning Concepts

    Steps of HIA Assessment, which is a two step process that first describes the baseline health status and then assesses potential impacts Scoping, to establish the scope of health effects that will be included in the HIA, the populations affected,...
  • M25 Consortium of Academic Libraries Directors - m25lib.ac.uk

    M25 Consortium of Academic Libraries Directors - m25lib.ac.uk

    KEF - two pillars. Knowledge Exchange Policy. Concordat. KEF Metrics. Good practice portal. Commitment to improve processes. ... SCIENCE EUROPE I 20-9-2019. Agenda. Plan S and summary of changes to the Implementation Plan. Working with key stakeholders.
  • Mitochondrial Disorders

    Mitochondrial Disorders

    קצב המוטציות הקורות בגנום המיטוכונדרי גדול פי 10-100 Ovum-zygote תורשה אימהית המחלה תעבר מהאם לכל צאצאיה רק אימהות מעבירות את המחלה לדורות הבאים Maternal Mitochondrial Inheritance of a Mitochondrial Disease הטרופלסמיות: כל תא ...
  • Metric System Measurement - Mr. Jensen&#x27;s Mahopac Voyagers

    Metric System Measurement - Mr. Jensen's Mahopac Voyagers

    When Do We Use The Metric System? Examples of Metric Basic Units of Measurement Metric Prefixes Numerically speaking… The metric ladder To climb the metric ladder: PowerPoint Presentation Let's try some examples K H D M d c m K...