Android学习笔记之WI-FI(1)

分享到:
<1>什么是Wi-Fi?

Wi-Fi是一种可以将个人电脑、手持设备(如PDA、手机)等终端以无线方式互相连接的技术。Wi-Fi是一个无线网路通信技术的品牌,由Wi-Fi联盟(Wi-FiAlliance)所持有。

Wi-Fi 原先是无线保真的缩写,Wi-Fi 的英文全称为wireless fidelity,在无线局域网的范畴是指“无线相容性认证”,实质上是一种商业认证,同时也是一种无线联网的技术,以前通过网线连接电脑,而现在则是通过无线电波来连网;常见的就是一个无线路由器,那么在这个无线路由器的电波覆盖的有效范围都可以采用WIFI连接方式进行联网,如果无线路由器连接了一条ADSL线路或者别的上网线路,则又被称为“热点”。

<2>获取WI-FI网卡的状态(常量)

public    static    final    int    WIFI_STATE_DISABLED

Wi-Fi is disabled.      WI-FI网卡不可用

Constant Value(常量值):         1         (0x00000001)                   

 

 public    static    final   int     WIFI_STATE_DISABLING   

Wi-Fi is currently being disabled.   WI-FI网卡正在关闭

The state will change to WIFI_STATE_DISABLED if it finishes successfully. 

 最终状态会变成WI-FI网卡不可用

Constant Value(常量值):          0          (0x00000000)                   

 

 public   static    final      int     WIFI_STATE_ENABLED   

Wi-Fi is enabled.  WI-FI网卡可用

Constant Value(常量值):        3         (0x00000003)                   

 

 public   static    final    int      WIFI_STATE_ENABLING   

Wi-Fi is currently being enabled.   WI-FI网卡正在打开

The state will change to WIFI_STATE_ENABLED if it finishes successfully.

最终状态会变成WI-FI网卡可用

Constant Value(常量值):          2         (0x00000002)                   

 

 public    static    final    int       WIFI_STATE_UNKNOWN   

Wi-Fi is in an unknown state.  未知 网卡状态

This state will occur when an error happens while enabling or disabling.

Constant Value(常量值):          4          (0x00000004)                   

 

<3>操作WI-FI的所需的权限

可以在帮助文档里找到详细的权限,这里只列出一部分。

Manifest.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>改变WI-FI网卡的状态

对WI-FI网卡进行操作需要通过WifiManager对象进行

WifiManager  wifiManager = (WifiManager) this.getSystemService(Context.WIFI_SERVICE);

打开WI-FI网卡

wifiManager.setWifiEnabled(true);   

关闭WI-FI网卡

wifiManager.setWifiEnabled(false);   

获取网卡当前状态

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, "当前Wifi网卡状态为" + 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已关闭");
break;
case WifiManager.WIFI_STATE_DISABLING:
System.out.println("wifi state --->" + "WIFI正在关闭中");
break;
case WifiManager.WIFI_STATE_ENABLED:
System.out.println("wifi state --->" + "WIFI已启用");
break;
case WifiManager.WIFI_STATE_ENABLING:
System.out.println("wifi state --->" + "WIFI正在启动中");
break;
case WifiManager.WIFI_STATE_UNKNOWN:
System.out.println("wifi state --->" + "未知WIFI状态");
break;
}
Toast.makeText(WifiActivity.this, "当前Wifi网卡状态为" + 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, "当前Wifi网卡状态为" + 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" />
	<!-- 以下是使用wifi访问网络所需要的权限 -->
	<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> 

昵    称:
验证码:

相关文档: