Android development scans nearby wifi hotspots and displays the list, androidwifi

Source: Internet
Author: User
Tags bssid

Android development scans nearby wifi hotspots and displays the list, androidwifi

The Wi-Fi module has been used in recent projects. Today we will make a simple summary.

Reference: http://www.2cto.com/kf/201310/253617.html

1. To obtain a Wi-Fi object and perform operations on the wifi device, you must first obtain Context. getSystemService (Context. WIFI_SERVICE) to obtain the WifiManager object and use this object to manage the WIFI device. AddNetwork (WifiConfiguration config) adds a WIFI network described in config. By default, the WIFI network is DISABLE. CalculateSignalLevel (int Arg, int numLevels) Calculate the signal level compareSignalLevel (int rssiA, int rssiB) Compare the signal strength of Network A and Network B createWifiLock (int lockType, String tag) create a WIFI lock and lock the current WIFI connection disableNetwork (int netId) so that a network connection becomes invalid disconnect () disconnect the current WIFI connection enableNetwork (int netId, Boolean disableOthers) connect to the Wi-Fi network referred to by netId, and getConfiguredNetworks () is disabled for other networks to get the network connection status getConnectionInfo () to get the information of the current connection getDhcpInfo () to get the DHCP information getScanR Esulats () Get scan test result getWifiState () Get current WIFI device status isWifiEnabled () Determine whether the WIFI device has enabled pingSupplicant () ping operation, the ping operation is the same as that of the PC. ressociate () reconnects to the Wi-Fi network, even if the network is already connected to reconnect () and reconnects to a non-connected Wi-Fi network removeNetwork () remove a network saveConfiguration () and keep the configuration information setWifiEnabled () so that a connection valid startScan () starts to scan updateNetwork (WifiConfiguration config) to update a network connection 2. common wifi status WIFI_STATE_DISABLED WIFI Nic unavailable WIFI_STATE_DISABLING WIFI Nic is disabling WIFI_STAT E_ENABLED WIFI Nic available WIFI_STATE_ENABLING WIFI Nic is on WIFI_STATE_UNKNOWN WIFI Nic status unknown 3. the ScanResult object with connection signals in the list is used to indicate the properties of nearby Wi-Fi hotspots. You can use WifiManager. getScanResults () returns a ScanResult list. I will attach a demo to view nearby Wi-Fi hotspots. The important attributes of ScanResult include: the name of the SSID network address of the BSSID access point, the only difference is the name of the WIFI network Capabilities network access performance Frequency the current WIFI device nearby the hotspot Frequency (MHz) Level detected by the Wi-Fi network signal strength 4. connect to a Wi-Fi hotspot through WifiManager. the getConfiguredNetworks () method returns the list of WifiConfiguration objects. And then call WifiManager. enableNetwork (); to connect to the specified hotspot. 5. view the Wi-Fi information that has been connected WifiInfo is used to indicate the connected object. This object can be obtained through WifiManager. getConnectionInfo. WifiInfo contains information about the current connection. GetBSSID () Get BSSID property getDetailedStateOf () Get client connectivity getHiddenSSID () Get SSID is hidden getIpAddress () Get IP address getLinkSpeed () Get connection speed getMacAddress () obtain the Mac address getarg () to obtain the signal getSSID () of the 802.11n network to obtain the information of the SSIDgetSupplicanState () to obtain the status of a specific client. These are common classes and methods used in wifi operations, the following is a demo of displaying wifi hotspots in the list. 1. activity layout is very simple is a ListView, activity_wifi_list.xml content is as follows: [html] <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" xmlns: tools = "http://schemas.android.com/tools" android: layout_width = "match_parent" android: layout_height = "match_parent" android: paddingBottom = "@ dimen/activity_vertical_margin" android: paddingLeft = "@ dimen/plugin" android: paddingRight = "@ dimen/ Ctivity_horizontal_margin "android: paddingTop =" @ dimen/activity_vertical_margin "tools: context = ". wifiListActivity "> <ListView android: id =" @ + id/listView "android: layout_width =" match_parent "android: layout_height = "wrap_content"> </ListView> </RelativeLayout> 2. the item_wifi_list.xml layout file of ListView is as follows: [html] <? Xml version = "1.0" encoding = "UTF-8"?> <RelativeLayout xmlns: android = "http://schemas.android.com/apk/res/android" android: layout_width = "match_parent" android: layout_height = "match_parent"> <ImageView android: id = "@ + id/imageView" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignParentLeft = "true" android: layout_alignParentTop = "true" android: src = "@ drawable/ic_launcher"/> <TextView android: id = "@ + Id/textView "android: layout_width =" wrap_content "android: layout_height =" wrap_content "android: layout_alignBottom =" @ + id/imageView "android: layout_marginBottom =" 14dp "android: layout_toRightOf = "@ + id/imageView" android: text = "TextView"/> <TextView android: id = "@ + id/signal_strenth" android: layout_width = "wrap_content" android: layout_height = "wrap_content" android: layout_alignBaseline = "@ + id/textView" andro Id: layout_alignBottom = "@ + id/textView" android: layout_alignParentRight = "true" android: text = "TextView"/> </RelativeLayout> 3. the following is the code of the activity. You need to customize the list here. [Java] public class WifiListActivity extends Activity {private WifiManager wifiManager; List <ScanResult> list; @ Override protected void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. activity_wifi_list); init ();} private void init () {wifiManager = (WifiManager) getSystemService (Context. WIFI_SERVICE); openWifi (); list = wifiManager. getScanResults (); ListView listView = (ListView) findViewById (R. id. listView); if (list = null) {Toast. makeText (this, "wifi is not enabled! ", Toast. LENGTH_LONG ). show ();} else {listView. setAdapter (new MyAdapter (this, list);}/*** enable WIFI */private void openWifi () {if (! WifiManager. isWifiEnabled () {wifiManager. setWifiEnabled (true) ;}} public class MyAdapter extends BaseAdapter {LayoutInflater inflater; List <ScanResult> list; public MyAdapter (Context context, List <ScanResult> list) {// TODO Auto-generated constructor stub this. inflater = LayoutInflater. from (context); this. list = list ;}@ Override public int getCount () {// TODO Auto-generated method stub return list. size () ;}@ Override public Object getItem (int position) {// TODO Auto-generated method stub return position ;}@ Override public long getItemId (int position) {// TODO Auto-generated method stub return position;} @ Override public View getView (int position, View convertView, ViewGroup parent) {// TODO Auto-generated method stub View = null; view = inflater. inflate (R. layout. item_wifi_list, null); ScanResult scanResult = list. get (position); TextView textView = (TextView) view. findViewById (R. id. textView); textView. setText (scanResult. SSID); TextView signalStrenth = (TextView) view. findViewById (R. id. signal_strenth); signalStrenth. setText (String. valueOf (Math. abs (scanResult. level); ImageView imageView = (ImageView) view. findViewById (R. id. imageView); // determines the signal strength and displays the corresponding indicator icon if (Math. abs (scanResult. -level)> 100) {imageView. setImageDrawable (getResources (). getDrawable (R. drawable. stat_sys_wifi_signal_0);} else if (Math. abs (scanResult. level)> 80) {imageView. setImageDrawable (getResources (). getDrawable (R. drawable. stat_sys_wifi_signal_1);} else if (Math. abs (scanResult. level)> 70) {imageView. setImageDrawable (getResources (). getDrawable (R. drawable. stat_sys_wifi_signal_1);} else if (Math. abs (scanResult. level)> 60) {imageView. setImageDrawable (getResources (). getDrawable (R. drawable. stat_sys_wifi_signal_2);} else if (Math. abs (scanResult. level)> 50) {imageView. setImageDrawable (getResources (). getDrawable (R. drawable. stat_sys_wifi_signal_3);} else {imageView. setImageDrawable (getResources (). getDrawable (R. drawable. stat_sys_wifi_signal_4);} return view ;}} the program runs as follows:

Reference: http://www.open-open.com/lib/view/open1377702950526.html


In android development, how can I enable the Wi-Fi hotspot through code implementation?

1. Apply for permissions:
Android. permission. ACCESS_WIFI_STATE
Android. permission. CHANGE_WIFI_STATE
Android. permission. WAKE_LOCK
2. Get WifiManager
WifiManager = (WifiManager) this. getSystemService (Context. WIFI_SERVICE );
3. Enable and disable wifi
If (wifiManager. isWifiEnabled ()){
WifiManager. setWifiEnabled (false );
} Else {
WifiManager. setWifiEnabled (true );
}
4. Note
If you encounter force-close, select wait, because it takes several seconds to start wifi. If the UI hasn't been reflected in five minutes, the system will give you this force close exception.

PS: I used to design and read system hardware information before, but it has been useless for a long time. This comment is sent from the Internet, hoping to help you.

Wifi hotspot connection issues developed by android

The problems with the mobile phone system of some brands are not yours, but may also be related to the android version or the manufacturer's customization problems.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.