找传奇、传世资源到传世资源站!

android wifi操作(打开/关闭/连接wifi 示例)

8.5玩家评分(1人评分)
下载后可评
介绍 评论 失效链接反馈

android 打开,关闭,扫描,连接,断开wifi,wifi连接状态查询
from clipboardpackage org.sunchao;//Download by http://www.codefans.netimport 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<ScanResult> listResult;private ScanResult mScanResult;// 定义WifiManager对象private WifiManager mWifiManager;// 定义WifiInfo对象private WifiInfo mWifiInfo;// 网络连接列表private List<WifiConfiguration> mWifiConfiguration;// 定义一个WifiLockWifiLock mWifiLock;/** * 构造方法 */public WifiAdmin(Context context) {mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);mWifiInfo = mWifiManager.getConnectionInfo();}/** * 打开Wifi网卡 */public void openNetCard() {if (!mWifiManager.isWifiEnabled()) {mWifiManager.setWifiEnabled(true);}}/** * 关闭Wifi网卡 */public void closeNetCard() {if (mWifiManager.isWifiEnabled()) {mWifiManager.setWifiEnabled(false);}}/** * 检查当前Wifi网卡状态 */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;}/** * 检查当前网络状态 * * @return String */public void checkNetWorkState() {if (mWifiInfo != null) {Log.i(TAG, "网络正常工作");} else {Log.i(TAG, "网络已断开");}}/** * 得到连接的ID */public int getNetworkId() {return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId();}/** * 得到IP地址 */public int getIPAddress() {return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress();}// 锁定WifiLockpublic void acquireWifiLock() {mWifiLock.acquire();}// 解锁WifiLockpublic void releaseWifiLock() {// 判断时候锁定if (mWifiLock.isHeld()) {mWifiLock.acquire();}}// 创建一个WifiLockpublic 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);}// 得到MAC地址public String getMacAddress() {return (mWifiInfo == null) ? "NULL" : mWifiInfo.getMacAddress();}// 得到接入点的BSSIDpublic String getBSSID() {return (mWifiInfo == null) ? "NULL" : mWifiInfo.getBSSID();}// 得到WifiInfo的所有信息包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;}}

评论

发表评论必须先登陆, 您可以 登陆 或者 注册新账号 !


在线咨询: 问题反馈
客服QQ:174666394

有问题请留言,看到后及时答复