之前有一篇文章已经简单的讲过Wifi的基本知识,请见Android之Wifi学习(1)。本文主要在此基础上,进一步完善程序功能,主要功能有打开无线网卡、关闭无线网卡、检查网卡状态、扫描网络、扫描结果、连接Wifi、断开Wifi、Wifi连接状态。
运行效果图如下:
其中连接Wifi是比较让我头疼的。需要在程序中进行连接,这会有两种情况。第一,Wifi没有密码,可以直接连接;第二,Wifi有密码,在程序中给出密码,然后连接。第一种我查阅相关资料好像确实是可以实现的,第二种我没找到,我自己也不知道该怎么去搞。最后想出一个办法,直接让程序跳到系统设置的Wifi的页面,然后让人手动去设置。观察了一下Android版的QQ,也确实是这么做的。不信你瞧:
点击设置网络后跳转到系统设置页面:
所以此处设置网络我也可以这么做,不过还是有点不爽,可能自己对Wifi这块还不是很熟悉,我会继续查阅相关API文档,到时候再跟大家分享,好了,下面贴一下源代码:
主页面:
package org.sunchao;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ScrollView;
import android.widget.TextView;
public class Main extends Activity implements OnClickListener {
// 右侧滚动条按钮
private ScrollView sView;
private Button openNetCard;
private Button closeNetCard;
private Button checkNetCardState;
private Button scan;
private Button getScanResult;
private Button connect;
private Button disconnect;
private Button checkNetWorkState;
private TextView scanResult;
private String mScanResult;
private WifiAdmin mWifiAdmin;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mWifiAdmin = new WifiAdmin(Main.this);
init();
}
/**
* 按钮等控件的初始化
*/
public void init() {
sView = (ScrollView) findViewById(R.id.mScrollView);
openNetCard = (Button) findViewById(R.id.openNetCard);
closeNetCard = (Button) findViewById(R.id.closeNetCard);
checkNetCardState = (Button) findViewById(R.id.checkNetCardState);
scan = (Button) findViewById(R.id.scan);
getScanResult = (Button) findViewById(R.id.getScanResult);
scanResult = (TextView) findViewById(R.id.scanResult);
connect = (Button) findViewById(R.id.connect);
disconnect = (Button) findViewById(R.id.disconnect);
checkNetWorkState = (Button) findViewById(R.id.checkNetWorkState);
openNetCard.setOnClickListener(Main.this);
closeNetCard.setOnClickListener(Main.this);
checkNetCardState.setOnClickListener(Main.this);
scan.setOnClickListener(Main.this);
getScanResult.setOnClickListener(Main.this);
connect.setOnClickListener(Main.this);
disconnect.setOnClickListener(Main.this);
checkNetWorkState.setOnClickListener(Main.this);
}
/**
* WIFI_STATE_DISABLING 0 WIFI_STATE_DISABLED 1 WIFI_STATE_ENABLING 2
* WIFI_STATE_ENABLED 3
*/
public void openNetCard() {
mWifiAdmin.openNetCard();
}
public void closeNetCard() {
mWifiAdmin.closeNetCard();
}
public void checkNetCardState() {
mWifiAdmin.checkNetCardState();
}
public void scan() {
mWifiAdmin.scan();
}
public void getScanResult() {
mScanResult = mWifiAdmin.getScanResult();
scanResult.setText(mScanResult);
}
public void connect() {
mWifiAdmin.connect();
// startActivityForResult(new Intent(
// android.provider.Settings.ACTION_WIFI_SETTINGS), 0);
startActivity(new Intent(android.provider.Settings.ACTION_WIFI_SETTINGS));
}
public void disconnect() {
mWifiAdmin.disconnectWifi();
}
public void checkNetWorkState() {
mWifiAdmin.checkNetWorkState();
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.openNetCard:
openNetCard();
break;
case R.id.closeNetCard:
closeNetCard();
break;
case R.id.checkNetCardState:
checkNetCardState();
break;
case R.id.scan:
scan();
break;
case R.id.getScanResult:
getScanResult();
break;
case R.id.connect:
connect();
break;
case R.id.disconnect:
disconnect();
break;
case R.id.checkNetWorkState:
checkNetWorkState();
break;
default:
break;
}
}
}
Wifi工具类:
- package org.sunchao;
-
- 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;
- import android.util.Log;
-
- public class WifiAdmin {
- private final static String TAG = "WifiAdmin";
- private StringBuffer mStringBuffer = new StringBuffer();
- private List listResult;
- private ScanResult mScanResult;
-
- private WifiManager mWifiManager;
-
- private WifiInfo mWifiInfo;
-
- private List mWifiConfiguration;
-
- WifiLock mWifiLock;
-
-
-
-
- public WifiAdmin(Context context) {
- mWifiManager = (WifiManager) context
- .getSystemService(Context.WIFI_SERVICE);
- mWifiInfo = mWifiManager.getConnectionInfo();
- }
-
-
-
-
- public void openNetCard() {
- if (!mWifiManager.isWifiEnabled()) {
- mWifiManager.setWifiEnabled(true);
- }
- }
-
-
-
-
- public void closeNetCard() {
- if (mWifiManager.isWifiEnabled()) {
- mWifiManager.setWifiEnabled(false);
- }
- }
-
-
-
-
- public void checkNetCardState() {
- if (mWifiManager.getWifiState() == 0) {
- Log.i(TAG, "网卡正在关闭");
- } else if (mWifiManager.getWifiState() == 1) {
- Log.i(TAG, "网卡已经关闭");
- } else if (mWifiManager.getWifiState() == 2) {
- Log.i(TAG, "网卡正在打开");
- } else if (mWifiManager.getWifiState() == 3) {
- Log.i(TAG, "网卡已经打开");
- } else {
- Log.i(TAG, "---_---晕......没有获取到状态---_---");
- }
- }
-
-
-
-
- public void scan() {
- mWifiManager.startScan();
- listResult = mWifiManager.getScanResults();
- if (listResult != null) {
- Log.i(TAG, "当前区域存在无线网络,请查看扫描结果");
- } else {
- Log.i(TAG, "当前区域没有无线网络");
- }
- }
-
-
-
-
- public String getScanResult() {
-
- if (mStringBuffer != null) {
- mStringBuffer = new StringBuffer();
- }
-
- scan();
- listResult = mWifiManager.getScanResults();
- if (listResult != null) {
- for (int i = 0; i < listResult.size(); i++) {
- mScanResult = listResult.get(i);
- mStringBuffer = mStringBuffer.append("NO.").append(i + 1)
- .append(" :").append(mScanResult.SSID).append("->")
- .append(mScanResult.BSSID).append("->")
- .append(mScanResult.capabilities).append("->")
- .append(mScanResult.frequency).append("->")
- .append(mScanResult.level).append("->")
- .append(mScanResult.describeContents()).append("\n\n");
- }
- }
- Log.i(TAG, mStringBuffer.toString());
- return mStringBuffer.toString();
- }
-
-
-
-
- public void connect() {
- mWifiInfo = mWifiManager.getConnectionInfo();
-
- }
-
-
-
-
- public void disconnectWifi() {
- int netId = getNetworkId();
- mWifiManager.disableNetwork(netId);
- mWifiManager.disconnect();
- mWifiInfo = null;
- }
-
-
-
-
-
-
- public void checkNetWorkState() {
- if (mWifiInfo != null) {
- Log.i(TAG, "网络正常工作");
- } else {
- Log.i(TAG, "网络已断开");
- }
- }
-
-
-
-
- public int getNetworkId() {
- return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();
- }
-
-
-
-
- public int getIPAddress() {
- return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();
- }
-
-
- public void acquireWifiLock() {
- mWifiLock.acquire();
- }
-
-
- public void releaseWifiLock() {
-
- if (mWifiLock.isHeld()) {
- mWifiLock.acquire();
- }
- }
-
-
- public void creatWifiLock() {
- mWifiLock = mWifiManager.createWifiLock("Test");
- }
-
-
- public List getConfiguration() {
- return mWifiConfiguration;
- }
-
-
- public void connectConfiguration(int index) {
-
- if (index >= mWifiConfiguration.size()) {
- return;
- }
-
- mWifiManager.enableNetwork(mWifiConfiguration.get(index).networkId,
- true);
- }
-
-
- public String getMacAddress() {
- return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();
- }
-
-
- public String getBSSID() {
- return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();
- }
-
-
- public String getWifiInfo() {
- return (mWifiInfo == null) ? "NULL" : mWifiInfo.toString();
- }
-
-
- public int addNetwork(WifiConfiguration wcg) {
- int wcgID = mWifiManager.addNetwork(mWifiConfiguration.get(3));
- mWifiManager.enableNetwork(wcgID, true);
- return wcgID;
- }
- }
AndroidManifest.xml:(注意需要添加的权限)
- xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android=""
- package="org.sunchao" android:versionCode="1" android:versionName="1.0">
- <uses-sdk android:minSdkVersion="8" />
-
- <application android:icon="@drawable/icon" android:label="@string/app_name">
- <activity android:name=".Main" 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-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>
布局文件就不贴了,我想看界面你都可以自己写出来,如果有需要,这里可以下载源代码:
阅读(22377) | 评论(0) | 转发(1) |