Android WiFi tutorial WiFi list display

Source: Internet
Author: User
Tags bssid

1. How to get the WiFi object and operate the WiFi device, you need to get Context.getsystemservice (Context.wifi_service) to get the Wifimanager object, and use this object to manage WiFi devices. Addnetwork (wificonfiguration config) Adds a config description of the WiFi network, by default, this WiFi network is disable state. Calculatesignallevel (int rssi, int numlevels) calculates the signal's rank comparesignallevel (int rssia, int rssib) Compare the signal strength of network A and Network B createwifilock (int lockType, String tag) to create a WiFi lock, lock the current WiFi connection disablenetwork (int netId) Make a network connection fail disconnect () disconnect the current WiFi connection enablenetwork (int netId, Boolean disableothers) connected to the WiFi network referred to by NetId, and other networks are disabled getconfigurednetworks () Gets the status of the network connection Getconnectioninfo () Gets the current connection information getdhcpinfo () Get DHCP information getscanresulats () Get the results of a scan test getwifistate () Gets the status of the current WiFi device iswifienabled () determine if the WiFi device is turned on pingsupplicant () Ping, and the ping operation of the PC is the same ressociate () Reconnect the WiFi network, even if the network is already connected to the reconnect () reconnect a non-connected WiFi network removenetwork () remove a network saveconfiguration () Leave a configuration Message setwifienabled () to have a connection active startscan () Start scanning updatenetwork (wificonfiguration config) to update a network connection 2. Common WiFi status Wifi_ State_disabled WiFi nic is not available  wifi_state_disabling WiFi nic is shutting down  wifI_state_enabled WiFi network card available  wifi_state_enabling WiFi card is open  wifi_state_unknown WiFi NIC Status not known 3. List view WiFi hotspot with connected signal Scanresult object is a property used to represent a nearby WiFi hotspot, you can return a scanresult list by Wifimanager.getscanresults (). I enclose the important properties of Demo,scanresult to view nearby WiFi hotspots There are a few: BSSID access point address SSID Network name, unique difference wifi network name capabilities Network access Performance frequency The WiFi network signal strength found in the frequency (MHz) level of the hotspot near the current WiFi device 4. Connect WiFi hotspot via Wifimanager.getconfigurednetworks () method returns a list of Wificonfiguration objects and then calls the Wifimanager.enablenetwork () method to connect to the specified hotspot. 5. View WiFi information that is already connected Wifiinfo is an object that is designed to represent a connection, and this object can be obtained through Wifimanager.getconnectioninfo (). The wifiinfo contains information about the current connection.  getbssid () Gets the Bssid property getdetailedstateof () Gets the connectivity of the client Gethiddenssid () gets whether the SSID is hidden getipaddress () gets the IP Address Getlinkspeed () Gets the speed of the connection getmacaddress () Gets the MAC address Getrssi () Gets the signal of the 802.11N network Getssid () get Ssidgetsupplicanstate () Get information about the status of specific clients the classes and methods that are commonly used in WiFi operations are as follows, and a list showing the demo of WiFi hotspots is given below. The layout of the 1.activity is simple and a listview,activity_wifi_list.xml content is as follows:
<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/activity_horizontal_margin"Android:paddingright="@dimen/activity_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>

The contents of the 2.ListView item layout file Item_wifi_list.xml as follows:

<?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"Android:layout_alignbottom="@+id/textview"Android:layout_alignparentright="true"Android:text="TextView"/> </RelativeLayout>

3. Below is the activity code, here you need to customize the list

 Public classWifilistactivity extends Activity {PrivateWifimanager Wifimanager; List<ScanResult>list; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);          Setcontentview (r.layout.activity_wifi_list);      Init (); }        Private voidinit () {Wifimanager=(Wifimanager) Getsystemservice (Context.wifi_service);          Openwifi (); List=wifimanager.getscanresults (); ListView ListView=(ListView) Findviewbyid (R.id.listview); if(List = =NULL) {Toast.maketext ( This,"WiFi not open! ", Toast.length_long). Show (); }Else{listview.setadapter (NewMyadapter ( This, list)); }                }            /** * Open WiFi*/      Private voidOpenwifi () {if(!wifimanager.iswifienabled ()) {wifimanager.setwifienabled (true); }              }         Public classMyadapter extends Baseadapter {layoutinflater inflater; List<ScanResult>list;  PublicMyadapter (context context, list<scanresult>list) {              //TODO auto-generated Constructor stub             This. Inflater = Layoutinflater. from(context);  This. List =list; } @Override Public intGetCount () {//TODO auto-generated Method Stub            returnlist.size (); } @Override PublicObject GetItem (intposition) {              //TODO auto-generated Method Stub            returnposition; } @Override Public LongGetitemid (intposition) {              //TODO auto-generated Method Stub            returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {              //TODO auto-generated Method StubView 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); //determine the signal strength and display the corresponding indicator icon            if(Math.Abs (Scanresult.level) > -) {imageview.setimagedrawable (Getresources (). getdrawable (R.DRAWABLE.STAT_SYS_WIFI_SIGNAL_0)); } Else if(Math.Abs (Scanresult.level) > the) {imageview.setimagedrawable (Getresources (). getdrawable (R.drawable.stat_sys_wifi_signal_1)); } Else if(Math.Abs (Scanresult.level) > -) {imageview.setimagedrawable (Getresources (). getdrawable (R.drawable.stat_sys_wifi_signal_1)); } Else if(Math.Abs (Scanresult.level) > -) {imageview.setimagedrawable (Getresources (). getdrawable (r.drawable.stat_sys_wifi_signal_2)); } Else if(Math.Abs (Scanresult.level) > -) {imageview.setimagedrawable (Getresources (). getdrawable (R.drawable.stat_sys_wifi_signal_3)); } Else{imageview.setimagedrawable (Getresources (). getdrawable (R.drawable.stat_sys_wifi_signal_4)); }              returnview; }        }    }  

The program runs as follows:

Android WiFi tutorial WiFi list display

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.