1.先确认操作WIFI所需要的权限,需要修改AndroidManifest.xml 在SDK文档中的docs\reference 搜索 Manifest.permission 里面查看关于wifi的权限 String ACCESS_WIFI_STATE Allows applications to access information about Wi-Fi networks String CHANGE_WIFI_MULTICAST_STATE Allows applications to enter Wi-Fi Multicast mode 添加如下 |
<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>
2.确认WIFIManager类的相关介绍和WIFI的状态值
在SDK文档中的docs\reference 搜索WifiManager,可以看到状态值
int WIFI_STATE_DISABLED Wi-Fi is disabled.
int WIFI_STATE_DISABLING Wi-Fi is currently being disabled.
int WIFI_STATE_ENABLED Wi-Fi is enabled.
int WIFI_STATE_ENABLING Wi-Fi is currently being enabled.
int WIFI_STATE_UNKNOWN Wi-Fi is in an unknown state.
3.WifiManager对象操作
WifiManager wifiManager =(WifiManager)Context.getSystemService(Service_WIFI_SERVICE);
打开WIFI网卡 wifiManager.setWifiEnable(true);
关闭WIFI网卡 wifiManager.setWifiEnable(false);
获取状态 wifiManager.getWifiState();
4.具体代码如下:
package x2p2.wifi;
import android.app.Activity;
import android.content.Context;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ActivityMain extends Activity {
/** Called when the activity is first created. */
private Button startButton=null;
private Button endButton=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);
endButton =(Button)findViewById(R.id.stopWifi);
checkButton=(Button)findViewById(R.id.checkwifi);
startButton.setOnClickListener(new StartWifiListener());
endButton.setOnClickListener(new EndWifiListener());
checkButton.setOnClickListener(new CheckWifiListener());
}
class StartWifiListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wifiManager=(WifiManager)ActivityMain.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(true);
System.out.println("wifi state-->"+wifiManager.getWifiState());
Toast.makeText(ActivityMain.this, "当前wifi网卡状态为"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
}
}
class EndWifiListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wifiManager=(WifiManager)ActivityMain.this.getSystemService(Context.WIFI_SERVICE);
wifiManager.setWifiEnabled(false);
System.out.println("wifi state-->"+wifiManager.getWifiState());
Toast.makeText(ActivityMain.this, "当前wifi网卡状态为"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
}
}
class CheckWifiListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
wifiManager=(WifiManager)ActivityMain.this.getSystemService(Context.WIFI_SERVICE);
//wifiManager.setWifiEnabled(false);
System.out.println("wifi state-->"+wifiManager.getWifiState());
Toast.makeText(ActivityMain.this, "当前wifi网卡状态为"+wifiManager.getWifiState(), Toast.LENGTH_SHORT).show();
}
}
}
5:WiFi一些常见操作
package com.yarin.android.Examples_08_08;
import java.util.List;
import android.content.Context;
import android.net.wifi.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
public class WifiAdmin
{
//定义WifiManager对象
private WifiManager mWifiManager;
//定义WifiInfo对象
private WifiInfo mWifiInfo;
//扫描出的网络连接列表
private List<ScanResult> mWifiList;
//网络连接列表
private List<WifiConfiguration> mWifiConfiguration;
//定义一个WifiLock
WifiLock mWifiLock;
//构造器
public WifiAdmin(Context context)
{
//取得WifiManager对象
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
//取得WifiInfo对象
mWifiInfo = mWifiManager.getConnectionInfo();
}
//打开WIFI
public void OpenWifi()
{
if (!mWifiManager.isWifiEnabled())
{
mWifiManager.setWifiEnabled(true);
}
}
//关闭WIFI
public void CloseWifi()
{
if (!mWifiManager.isWifiEnabled())
{
mWifiManager.setWifiEnabled(false);
}
}
//锁定WifiLock
public void AcquireWifiLock()
{
mWifiLock.acquire();
}
//解锁WifiLock
public void ReleaseWifiLock()
{
//判断时候锁定
if (mWifiLock.isHeld())
{
mWifiLock.acquire();
}
}
//创建一个WifiLock
public void CreatWifiLock()
{
mWifiLock = mWifiManager.createWifiLock("Test");
}
//得到配置好的网络
public List<WifiConfiguration> GetConfiguration()
{
return mWifiConfiguration;
}
//指定配置好的网络进行连接
public void ConnectConfiguration(int index)
{
//索引大于配置好的网络索引返回
if(index > mWifiConfiguration.size())
{
return;
}
//连接配置好的指定ID的网络
mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId, true);
}
public void StartScan()
{
mWifiManager.startScan();
//得到扫描结果
mWifiList = mWifiManager.getScanResults();
//得到配置好的网络连接
mWifiConfiguration = mWifiManager.getConfiguredNetworks();
}
//得到网络列表
public List<ScanResult> GetWifiList()
{
return mWifiList;
}
//查看扫描结果
public StringBuilder LookUpScan()
{
StringBuilder stringBuilder = new StringBuilder();
for (int i = 0; i < mWifiList.size(); i++)
{
stringBuilder.append("Index_"+new Integer(i + 1).toString() + ":");
//将ScanResult信息转换成一个字符串包
//其中把包括:BSSID、SSID、capabilities、frequency、level
stringBuilder.append((mWifiList.get(i)).toString());
stringBuilder.append("\n");
}
return stringBuilder;
}
//得到MAC地址
public String GetMacAddress()
{
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
}
//得到接入点的BSSID
public String GetBSSID()
{
return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
}
//得到IP地址
public int GetIPAddress()
{
return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
}
//得到连接的ID
public int GetNetworkId()
{
return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
}
//得到WifiInfo的所有信息包
public String GetWifiInfo()
{
return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
}
//添加一个网络并连接
public void AddNetwork(WifiConfiguration wcg)
{
int wcgID = mWifiManager.addNetwork(wcg);
mWifiManager.enableNetwork(wcgID, true);
}
//断开指定ID的网络
public void DisconnectWifi(int netId)
{
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}
}