Study Notes for Android WI-FI (1)

Source: Internet
Author: User

<1> what is wi-fi?
Wi-Fi is a technology that can connect PCs, handheld devices (such as PDAs and mobile phones) to each other wirelessly. Wi-Fi is a brand of wireless network communication technology, held by the Wi-Fi Alliance.
Wi-Fi was originally short for wireless fidelity. Wi-Fi is called wireless fidelity in English. In the wireless LAN field, it refers to "wireless compatibility authentication", which is essentially a commercial authentication, it is also a wireless networking technology. It used to connect to a computer through a network cable, but now it is connected through a radio wave. A common is a wireless router, the valid range of the radio wave coverage of this wireless router can be connected through WIFI connection. If the wireless router is connected to an ADSL line or another Internet connection line, it is also called a "hot spot ".
<2> obtain the status of the WI-FI NIC (constant)
Public static final int WIFI_STATE_DISABLED
Wi-Fi is disabled. WI-FI Nic unavailable
Constant Value (Constant Value): 1 (0x00000001)
 
Public static final int WIFI_STATE_DISABLING
Wi-Fi is currently being disabled. WI-FI Nic is disabled
The state will change to WIFI_STATE_DISABLED if it finishes successfully.
The final state will change to WI-FI Nic unavailable
Constant Value (Constant Value): 0 (0x00000000)
 
Public static final int WIFI_STATE_ENABLED
Wi-Fi is enabled. WI-FI Nic available
Constant Value (Constant Value): 3 (0x00000003)
 
Public static final int WIFI_STATE_ENABLING
Wi-Fi is currently being enabled. WI-FI Nic is on
The state will change to WIFI_STATE_ENABLED if it finishes successfully.
The final state will change to WI-FI Nic available
Constant Value (Constant Value): 2 (0x00000002)
 
Public static final int WIFI_STATE_UNKNOWN
Wi-Fi is in an unknown state. unknown Nic status
This state will occur when an error happens while enabling or disabling.
Constant Value (Constant Value): 4 (0x00000004)
 
<3> the permissions required to operate the WI-FI
You can find detailed permissions in the help document. Only a part of the permissions is listed here.
Manifest. permission (permission)
Public static final String CHANGE_WIFI_MULTICAST_STATE
Allows applications to enter Wi-Fi Multicast mode
Constant Value: "android. permission. CHANGE_WIFI_MULTICAST_STATE"
 
Public static final String CHANGE_WIFI_STATE
Allows applications to change Wi-Fi connectivity state
Constant Value: "android. permission. CHANGE_WIFI_STATE"
 
Public static final String CHANGE_NETWORK_STATE
Allows applications to change network connectivity state
Constant Value: "android. permission. CHANGE_NETWORK_STATE
 
<4> changing the WI-FI Nic status
Operations on WI-FI NICs are performed through the WifiManager object
WifiManager wifiManager = (WifiManager) this. getSystemService (Context. WIFI_SERVICE );
Turn on WI-FI Nic
WifiManager. setWifiEnabled (true );
Turn Off WI-FI Nic
WifiManager. setWifiEnabled (false );
Obtain the current Nic status
WifiManager .. getWifiState ()
 
Package mars. wifi;
 
Import android. app. Activity;
Import android. content. Context;
Import android.net. wifi. WifiManager;
Import android. OS. Bundle;
Import android. view. View;
Import android. view. View. OnClickListener;
Import android. widget. Button;
Import android. widget. Toast;
 
Public class WifiActivity extends Activity {
/** Called when the activity is first created .*/
Private Button startButton = null;
Private Button stopButton = null;
Private Button checkButton = null;
Private WifiManager wifiManager = null;
@ Override
Public void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. main );
StartButton = (Button) findViewById (R. id. startWifi );
StopButton = (Button) findViewById (R. id. stopWifi );
CheckButton = (Button) findViewById (R. id. checkWifi );
StartButton. setOnClickListener (new StartWifiListener ());
StopButton. setOnClickListener (new StopWifiListener ());
CheckButton. setOnClickListener (new CheckWifiListener ());
}
Class StartWifiListener implements OnClickListener {
 
Public void onClick (View v ){
WifiManager = (WifiManager) WifiActivity. this. getSystemService (Context. WIFI_SERVICE );
WifiManager. setWifiEnabled (true );
System. out. println ("wifi state --->" + wifiManager. getWifiState ());
Toast. makeText (WifiActivity. this, "the current Wifi Nic status is" + wifiManager. getWifiState (), Toast. LENGTH_SHORT). show ();
}
}
Class StopWifiListener implements OnClickListener {
 
Public void onClick (View arg0 ){
// TODO Auto-generated method stub
WifiManager = (WifiManager) WifiActivity. this. getSystemService (Context. WIFI_SERVICE );
WifiManager. setWifiEnabled (false );
Switch (wifiManager. getWifiState ())
{
Case WifiManager. WIFI_STATE_DISABLED:
System. out. println ("wifi state --->" + "WIFI disabled ");
Break;
 
Case WifiManager. WIFI_STATE_DISABLING:
System. out. println ("wifi state --->" + "WIFI is being disabled ");
Break;
Case WifiManager. WIFI_STATE_ENABLED:
System. out. println ("wifi state --->" + "WIFI enabled ");
Break;
Case WifiManager. WIFI_STATE_ENABLING:
System. out. println ("wifi state --->" + "WIFI starting ");
Break;
Case WifiManager. WIFI_STATE_UNKNOWN:
System. out. println ("wifi state --->" + "unknown WIFI state ");
Break;
}
Toast. makeText (WifiActivity. this, "the current Wifi Nic status is" + wifiManager. getWifiState (), Toast. LENGTH_SHORT). show ();
}

}

Class CheckWifiListener implements OnClickListener {
 
Public void onClick (View v ){
WifiManager = (WifiManager) WifiActivity. this. getSystemService (Context. WIFI_SERVICE );
System. out. println ("wifi state --->" + wifiManager. getWifiState ());
Toast. makeText (WifiActivity. this, "the current Wifi Nic status is" + wifiManager. getWifiState (), Toast. LENGTH_SHORT). show ();
}

}
}
 
 
 
<? Xml version = "1.0" encoding = "UTF-8"?>
<Manifest xmlns: android = "http://schemas.android.com/apk/res/android"
Package = "mars. wifi" android: versionCode = "1" android: versionName = "1.0">
<Application android: icon = "@ drawable/icon" android: label = "@ string/app_name">
<Activity android: name = ". WifiActivity" android: label = "@ string/app_name">
<Intent-filter>
<Action android: name = "android. intent. action. MAIN"/>
<Category android: name = "android. intent. category. LAUNCHER"/>
</Intent-filter>
</Activity>
 
</Application>
<Uses-sdk android: minSdkVersion = "4"/>
<! -- The following are the permissions required to access the network through wifi: www.2cto.com -->
<Uses-permission android: name = "android. permission. CHANGE_NETWORK_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. CHANGE_WIFI_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_NETWORK_STATE"> </uses-permission>
<Uses-permission android: name = "android. permission. ACCESS_WIFI_STATE"> </uses-permission>
</Manifest>

Related Article

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.