在Android中对Wifi操作,android本身提供了一些有用的包,在android.net.wifi包下面。主要包括以下几个类和接口:
1.ScanResult
主要用来描述已经检测出的接入点,包括接入点的地址,接入点的名称,身份认证,频率,信号强度等信息。
2.WifiConfiguration
Wifi网络的配置,包括安全设置等。
3.WifiInfo
wifi无线连接的描述,包括接入点,网络连接状态,隐藏的接入点,IP地址,连接速度,MAC地址,网络ID,信号强度等信息。这里简单介绍一下这里的方法:
getBSSID() 获取BSSID
getDetailedStateOf() 获取客户端的连通性
getHiddenSSID() 获得SSID 是否被隐藏
getIpAddress() 获取IP 地址
getLinkSpeed() 获得连接的速度
getMacAddress() 获得Mac 地址
getRssi() 获得802.11n 网络的信号
getSSID() 获得SSID
getSupplicanState() 返回具体客户端状态的信息
4.WifiManager
这个不用说,就是用来管理我们的wifi 连接,这里已经定义好了一些类,可以供我们使用。
WIFI网卡的状态是由一系列的整形常量来表示的。
1.WIFI_STATE_DISABLED : WIFI网卡不可用(1)
2.WIFI_STATE_DISABLING : WIFI网卡正在关闭(0)
3.WIFI_STATE_ENABLED : WIFI网卡可用(3)
4.WIFI_STATE_ENABLING : WIFI网正在打开(2) (WIFI启动需要一段时间)
5.WIFI_STATE_UNKNOWN : 未知网卡状态
下面先写一个wifi的实例:
首先看以下main.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <LinearLayout
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button
- android:id="@+id/scan"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="扫描网络"
- />
- <Button
- android:id="@+id/start"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="打开Wifi"
- />
- <Button
- android:id="@+id/stop"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="关闭Wifi"
- />
- <Button
- android:id="@+id/check"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Wifi状态"
- />
- <TextView
- android:id="@+id/allNetWork"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="当前没有扫描到Wifi网络"
- />
- </LinearLayout>
- </ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:id="@+id/scan"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="扫描网络"
/>
<Button
android:id="@+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打开Wifi"
/>
<Button
android:id="@+id/stop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="关闭Wifi"
/>
<Button
android:id="@+id/check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Wifi状态"
/>
<TextView
android:id="@+id/allNetWork"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="当前没有扫描到Wifi网络"
/>
</LinearLayout>
</ScrollView>
接下来我们吧wifi的相关操作都封装在了一个WifiAdmin类中,以后开启或关闭等相关操作可以直接调用这个类的相关方法:
- 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> mWifiConfigurations;
- 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);
- }
- }
- // 检查当前wifi状态
- public int checkState() {
- return mWifiManager.getWifiState();
- }
- //锁定wifiLock
- public void acquireWifiLock(){
- mWifiLock.acquire();
- }
- //解锁wifiLock
- public void releaseWifiLock(){
- //判断是否锁定
- if(mWifiLock.isHeld()){
- mWifiLock.acquire();
- }
- }
- //创建一个wifiLock
- public void createWifiLock(){
- mWifiLock=mWifiManager.createWifiLock("test");
- }
- //得到配置好的网络
- public List<WifiConfiguration> getConfiguration(){
- return mWifiConfigurations;
- }
- //指定配置好的网络进行连接
- public void connetionConfiguration(int index){
- if(index>mWifiConfigurations.size()){
- return ;
- }
- //连接配置好指定ID的网络
- mWifiManager.enableNetwork(mWifiConfigurations.get(index).networkId, true);
- }
- public void startScan(){
- mWifiManager.startScan();
- //得到扫描结果
- mWifiList=mWifiManager.getScanResults();
- //得到配置好的网络连接
- mWifiConfigurations=mWifiManager.getConfiguredNetworks();
- }
- //得到网络列表
- public List<ScanResult> getWifiList(){
- return mWifiList;
- }
- //查看扫描结果
- public StringBuffer lookUpScan(){
- StringBuffer sb=new StringBuffer();
- for(int i=0;i<mWifiList.size();i++){
- sb.append("Index_" + new Integer(i + 1).toString() + ":");
- // 将ScanResult信息转换成一个字符串包
- // 其中把包括:BSSID、SSID、capabilities、frequency、level
- sb.append((mWifiList.get(i)).toString()).append("\n");
- }
- return sb;
- }
- public String getMacAddress(){
- return (mWifiInfo==null)?"NULL":mWifiInfo.getMacAddress();
- }
- public String getBSSID(){
- return (mWifiInfo==null)?"NULL":mWifiInfo.getBSSID();
- }
- public int getIpAddress(){
- return (mWifiInfo==null)?0:mWifiInfo.getIpAddress();
- }
- //得到连接的ID
- public int getNetWordId(){
- return (mWifiInfo==null)?0:mWifiInfo.getNetworkId();
- }
- //得到wifiInfo的所有信息
- public String getWifiInfo(){
- return (mWifiInfo==null)?"NULL":mWifiInfo.toString();
- }
- //添加一个网络并连接
- public void addNetWork(WifiConfiguration configuration){
- int wcgId=mWifiManager.addNetwork(configuration);
- mWifiManager.enableNetwork(wcgId, true);
- }
- //断开指定ID的网络
- public void disConnectionWifi(int netId){
- mWifiManager.disableNetwork(netId);
- mWifiManager.disconnect();
- }
- }
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> mWifiConfigurations;
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);
}
}
// 检查当前wifi状态
public int checkState() {
return mWifiManager.getWifiState();
}
//锁定wifiLock
public void acquireWifiLock(){
mWifiLock.acquire();
}
//解锁wifiLock
public void releaseWifiLock(){
//判断是否锁定
if(mWifiLock.isHeld()){
mWifiLock.acquire();
}
}
//创建一个wifiLock
public void createWifiLock(){
mWifiLock=mWifiManager.createWifiLock("test");
}
//得到配置好的网络
public List<WifiConfiguration> getConfiguration(){
return mWifiConfigurations;
}
//指定配置好的网络进行连接
public void connetionConfiguration(int index){
if(index>mWifiConfigurations.size()){
return ;
}
//连接配置好指定ID的网络
mWifiManager.enableNetwork(mWifiConfigurations.get(index).networkId, true);
}
public void startScan(){
mWifiManager.startScan();
//得到扫描结果
mWifiList=mWifiManager.getScanResults();
//得到配置好的网络连接
mWifiConfigurations=mWifiManager.getConfiguredNetworks();
}
//得到网络列表
public List<ScanResult> getWifiList(){
return mWifiList;
}
//查看扫描结果
public StringBuffer lookUpScan(){
StringBuffer sb=new StringBuffer();
for(int i=0;i<mWifiList.size();i++){
sb.append("Index_" + new Integer(i + 1).toString() + ":");
// 将ScanResult信息转换成一个字符串包
// 其中把包括:BSSID、SSID、capabilities、frequency、level
sb.append((mWifiList.get(i)).toString()).append("\n");
}
return sb;
}
public String getMacAddress(){
return (mWifiInfo==null)?"NULL":mWifiInfo.getMacAddress();
}
public String getBSSID(){
return (mWifiInfo==null)?"NULL":mWifiInfo.getBSSID();
}
public int getIpAddress(){
return (mWifiInfo==null)?0:mWifiInfo.getIpAddress();
}
//得到连接的ID
public int getNetWordId(){
return (mWifiInfo==null)?0:mWifiInfo.getNetworkId();
}
//得到wifiInfo的所有信息
public String getWifiInfo(){
return (mWifiInfo==null)?"NULL":mWifiInfo.toString();
}
//添加一个网络并连接
public void addNetWork(WifiConfiguration configuration){
int wcgId=mWifiManager.addNetwork(configuration);
mWifiManager.enableNetwork(wcgId, true);
}
//断开指定ID的网络
public void disConnectionWifi(int netId){
mWifiManager.disableNetwork(netId);
mWifiManager.disconnect();
}
}
最后看以下WifiActivity类:
- import java.util.List;
- import android.app.Activity;
- import android.net.wifi.ScanResult;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- import android.widget.Toast;
- public class WifiActivity extends Activity {
- /** Called when the activity is first created. */
- private TextView allNetWork;
- private Button scan;
- private Button start;
- private Button stop;
- private Button check;
- private WifiAdmin mWifiAdmin;
- // 扫描结果列表
- private List<ScanResult> list;
- private ScanResult mScanResult;
- private StringBuffer sb=new StringBuffer();
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- mWifiAdmin = new WifiAdmin(WifiActivity.this);
- init();
- }
- public void init(){
- allNetWork = (TextView) findViewById(R.id.allNetWork);
- scan = (Button) findViewById(R.id.scan);
- start = (Button) findViewById(R.id.start);
- stop = (Button) findViewById(R.id.stop);
- check = (Button) findViewById(R.id.check);
- scan.setOnClickListener(new MyListener());
- start.setOnClickListener(new MyListener());
- stop.setOnClickListener(new MyListener());
- check.setOnClickListener(new MyListener());
- }
- private class MyListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- switch (v.getId()) {
- case R.id.scan://扫描网络
- getAllNetWorkList();
- break;
- case R.id.start://打开Wifi
- mWifiAdmin.openWifi();
- Toast.makeText(WifiActivity.this, "当前wifi状态为:"+mWifiAdmin.checkState(), 1).show();
- break;
- case R.id.stop://关闭Wifi
- mWifiAdmin.closeWifi();
- Toast.makeText(WifiActivity.this, "当前wifi状态为:"+mWifiAdmin.checkState(), 1).show();
- break;
- case R.id.check://Wifi状态
- Toast.makeText(WifiActivity.this, "当前wifi状态为:"+mWifiAdmin.checkState(), 1).show();
- break;
- default:
- break;
- }
- }
- }
- public void getAllNetWorkList(){
- // 每次点击扫描之前清空上一次的扫描结果
- if(sb!=null){
- sb=new StringBuffer();
- }
- //开始扫描网络
- mWifiAdmin.startScan();
- list=mWifiAdmin.getWifiList();
- if(list!=null){
- for(int i=0;i<list.size();i++){
- //得到扫描结果
- mScanResult=list.get(i);
- sb=sb.append(mScanResult.BSSID+" ").append(mScanResult.SSID+" ")
- .append(mScanResult.capabilities+" ").append(mScanResult.frequency+" ")
- .append(mScanResult.level+"\n\n");
- }
- allNetWork.setText("扫描到的wifi网络:\n"+sb.toString());
- }
- }
- }
import java.util.List;
import android.app.Activity;
import android.net.wifi.ScanResult;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class WifiActivity extends Activity {
/** Called when the activity is first created. */
private TextView allNetWork;
private Button scan;
private Button start;
private Button stop;
private Button check;
private WifiAdmin mWifiAdmin;
// 扫描结果列表
private List<ScanResult> list;
private ScanResult mScanResult;
private StringBuffer sb=new StringBuffer();
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWifiAdmin = new WifiAdmin(WifiActivity.this);
init();
}
public void init(){
allNetWork = (TextView) findViewById(R.id.allNetWork);
scan = (Button) findViewById(R.id.scan);
start = (Button) findViewById(R.id.start);
stop = (Button) findViewById(R.id.stop);
check = (Button) findViewById(R.id.check);
scan.setOnClickListener(new MyListener());
start.setOnClickListener(new MyListener());
stop.setOnClickListener(new MyListener());
check.setOnClickListener(new MyListener());
}
private class MyListener implements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.scan://扫描网络
getAllNetWorkList();
break;
case R.id.start://打开Wifi
mWifiAdmin.openWifi();
Toast.makeText(WifiActivity.this, "当前wifi状态为:"+mWifiAdmin.checkState(), 1).show();
break;
case R.id.stop://关闭Wifi
mWifiAdmin.closeWifi();
Toast.makeText(WifiActivity.this, "当前wifi状态为:"+mWifiAdmin.checkState(), 1).show();
break;
case R.id.check://Wifi状态
Toast.makeText(WifiActivity.this, "当前wifi状态为:"+mWifiAdmin.checkState(), 1).show();
break;
default:
break;
}
}
}
public void getAllNetWorkList(){
// 每次点击扫描之前清空上一次的扫描结果
if(sb!=null){
sb=new StringBuffer();
}
//开始扫描网络
mWifiAdmin.startScan();
list=mWifiAdmin.getWifiList();
if(list!=null){
for(int i=0;i<list.size();i++){
//得到扫描结果
mScanResult=list.get(i);
sb=sb.append(mScanResult.BSSID+" ").append(mScanResult.SSID+" ")
.append(mScanResult.capabilities+" ").append(mScanResult.frequency+" ")
.append(mScanResult.level+"\n\n");
}
allNetWork.setText("扫描到的wifi网络:\n"+sb.toString());
}
}
}
不要忘了加入权限:
- <!-- 以下是使用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>
<!-- 以下是使用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>
最后看以下演示效果:
点击打开wifi:
状态为3说明WIFI网卡可用(3)。点击扫描网络:
这些就是扫描到的wifi,具体参数参见上面介绍。