让手机连接到指定的WIFI网络,适用于之前已经连过的网络
这个例子是从网上找的,我给重新优化了下。这里有个问题是只能连接到之前已经连接过的wifi,目前还没找到连接到陌生wifi(有/无密码)的方法。总之慢慢来吧~
说下思路:
1.通过wifiManager来查找想要链接的wifi是否是之前已经连过的,如果是就进行下一步
2.查找想要链接的wifi名字是否在可搜索的范围内,也就是当前是否可以搜到,如果能搜到就可以连接了
3.通过WifiManager.enableNetwork(getWifiConfigurated(wifi_SSID), true);来连接到指定网络
PS:这里动态注册了一个广播来监听wifi的变化,能比较直观的现实wifi的变化
MainActivity.java
package com.example.testforwifilinkdemo; import android.app.Activity; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.wifi.WifiInfo; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class LinkWifiActivity extends Activity { private WifiAdmin wifiAdmin; //这里写想要链接的wifi名字 private final static String TAG_SSID = "CMCC-EDU"; //判断是否连接上制定的wifi private boolean isConnected = false; private BroadcastReceiver wifiConnectReceiver; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_link_wifi); wifiAdmin = new WifiAdmin(LinkWifiActivity.this); wifiConnectReceiver = new WifiConnectReceiver(); IntentFilter filter = new IntentFilter(); filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION); LinkWifiActivity.this.registerReceiver(wifiConnectReceiver, filter);//注册广播 Button linkBtn = (Button) findViewById(R.id.linkWifiBtn); linkBtn.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { connectToTagWifi(TAG_SSID); } }); } class WifiConnectReceiver extends BroadcastReceiver { @Override public void onReceive(Context context, Intent intent) { String action = intent.getAction(); if (action.equals(ConnectivityManager.CONNECTIVITY_ACTION)) { ConnectivityManager manager = (ConnectivityManager) getSystemService(CONNECTIVITY_SERVICE); NetworkInfo notewokInfo = manager.getActiveNetworkInfo(); if (notewokInfo != null) { System.out.println("notewokInfo.getExtraInfo()------->"+ notewokInfo.getExtraInfo()); WifiInfo currentWifiInfo = wifiAdmin.getCurrentWifiInfo(); System.out.println("currentWifiInfo.getSSID()----->"+ currentWifiInfo.getSSID()); if (currentWifiInfo.getSSID().equals("\"" + TAG_SSID + "\"")) { isConnected = true; } } else { System.out.println("notewokInfo is null"); } } } } @Override protected void onDestroy() { super.onDestroy(); LinkWifiActivity.this.unregisterReceiver(wifiConnectReceiver);//取消广播 } /** * @param wifi_SSID * 关键代码 */ public void connectToTagWifi(String wifi_SSID) { System.out.println("正在努力连接到指定的wifi"); wifiAdmin.connectConfiguratedWifi(TAG_SSID); } }
工具类:WifiAdmin.java
package com.example.testforwifilinkdemo; import java.util.ArrayList; 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"; public static final int IS_OPENING = 1,IS_CLOSING = 2,IS_OPENED = 3,IS_CLOSED = 4; private StringBuffer mStringBuffer = new StringBuffer(); private List<ScanResult> scanResultList; private ScanResult mScanResult; // 定义WifiManager对象 private WifiManager mWifiManager; // 定义WifiInfo对象 private WifiInfo mWifiInfo; // 网络连接列表 private List<WifiConfiguration> mWifiConfigList; // 定义一个WifiLock WifiLock mWifiLock; /** * 构造方法 */ public WifiAdmin(Context context) { mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); mWifiInfo = mWifiManager.getConnectionInfo(); mWifiConfigList = mWifiManager.getConfiguredNetworks(); } /** * 打开Wifi网卡,能打开就返回true,无法打开返回false */ public boolean openNetCard() { if (!mWifiManager.isWifiEnabled()) { return mWifiManager.setWifiEnabled(true); } else{ return false; } } /** * 关闭Wifi网卡,能关闭返回true,不能关就返回false */ public boolean closeNetCard() { if (mWifiManager.isWifiEnabled()) { return mWifiManager.setWifiEnabled(false); } else{ return false; } } /** * 检查当前Wifi网卡状态,返回四种状态,如果出错返回-1 */ public int getWifitate() { int result = -1; switch (mWifiManager.getWifiState()) { case 0: Log.i(TAG, "网卡正在关闭"); result = IS_CLOSING; break; case 1: Log.i(TAG, "网卡已经关闭"); result = IS_CLOSED; break; case 2: Log.i(TAG, "网卡正在打开"); result = IS_OPENING; break; case 3: Log.i(TAG, "网卡已经打开"); result = IS_OPENED; break; default: Log.i(TAG, "---_---晕......没有获取到状态---_---"); result = -1; break; } return result; } /** * 扫描周边网络,判断周边是否有wifi,有就返回true,没有就返回false */ public boolean scan() { if (getWifitate() == IS_OPENED) { //开始扫描 mWifiManager.startScan(); //将扫描结果存入数据列中 scanResultList = mWifiManager.getScanResults(); if (scanResultList != null) { return true; } } return false; } /** * 得到附近wifi的扫描结果,是ScanResult对象 * 得到的是附近网络的结果集,没有就返回null */ public ArrayList<ScanResult> getScanResult() { // 每次点击扫描之前清空上一次的扫描结果 if (mStringBuffer != null) { mStringBuffer = new StringBuffer(); } scan();// 开始扫描网络 ArrayList<ScanResult> scanResultsList = new ArrayList<ScanResult>(); if (scanResultList != null) { for (int i = 0; i < scanResultList.size(); i++) { mScanResult = scanResultList.get(i); scanResultsList.add(mScanResult); /*mStringBuffer = mStringBuffer.append("NO.").append(i) .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 scanResultsList; } else { return null; } } /** * 判断指定的网络是否能被扫描到 * @param wifi_SSID * @return 如果能够在周边发现指定的网络就返回true,否则返回false */ public boolean canScannable(String wifi_SSID) { boolean canScannable = false; scan();//开始扫描周边网络 //得到扫描到的wifi列表 if (scanResultList != null) { for (int i = 0; i < scanResultList.size(); i++) { System.out.println("scanResultList " + i + "----->" + scanResultList.get(i).SSID); if (scanResultList.get(i).SSID.equals(wifi_SSID)) { canScannable = true;//如果想要链接的wifi能够扫描到,那么就说明能够链接 break; } } } return canScannable; } /** * 得到指定网络的index(从0开始计数),找不到就返回-1 */ public int getTagWifiId(String netName){ // 开始扫描网络 scan(); scanResultList = mWifiManager.getScanResults(); if (scanResultList != null) { for (int i = 0; i < scanResultList.size(); i++) { mScanResult = scanResultList.get(i); if(mScanResult.SSID.equals(netName)){ return i; } String show = "No = "+i + " SSID = "+mScanResult.SSID + " capabilities = "+mScanResult.capabilities + " level = "+mScanResult.level; Log.i(TAG, show); } } return -1; } /** * 断开当前连接的网络 */ public void disconnectWifi() { int netId = getCurrentNetworkId(); mWifiManager.disableNetwork(netId); mWifiManager.disconnect(); mWifiInfo = null; } /** * 检查当前网络状态 * 如果有wifi链接,返回true,如果没有就返回false */ public boolean getWifiConnectState() { return mWifiInfo != null ? true : false; } /** * @return 当前网络的名字,如果没有就返回null,否则返回string */ public String getCurrentSSID(){ return (mWifiInfo == null) ? null :mWifiInfo.getSSID(); } /** * 得到连接的ID,如果没有就返回0,否则返回正确的id */ public int getCurrentNetworkId() { return (mWifiInfo == null) ? 0 : mWifiInfo.getNetworkId(); } /** * 得到IP地址,出错时返回0 */ public int getCurrentIPAddress() { return (mWifiInfo == null) ? 0 : mWifiInfo.getIpAddress(); } /** * 得到MAC地址 * @return 出錯了返回null */ public String getCurrentMacAddress() { return (mWifiInfo == null) ? null : mWifiInfo.getMacAddress(); } /** * 得到接入点的BSSID * @return 出錯返回null */ public String getCurrentBSSID() { return (mWifiInfo == null) ? null : mWifiInfo.getBSSID(); } /** * 得到WifiInfo的所有信息包 * @return 出错了返回null */ public WifiInfo getCurrentWifiInfo() { return (mWifiInfo == null) ? null : mWifiInfo; } // 锁定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 mWifiConfigList; } /** * @param index * @return 没有连接到返回false,正在连接则返回true */ public boolean connectConfiguratedWifi(String wifi_SSID) { //如果当前网络不是想要链接的网络,要连接的网络是配置过的,并且要连接的网络能够被扫描到 if (getCurrentSSID().indexOf(wifi_SSID) == -1 ) { if (getWifiConfigurated(wifi_SSID) != -1 && canScannable(wifi_SSID)) { mWifiManager.enableNetwork(getWifiConfigurated(wifi_SSID), true); } else { return false; } } return true; } /** * 判断要连接的wifi名是否已经配置过了 * @return 返回要连接的wifi的ID,如果找不到就返回-1 */ public int getWifiConfigurated(String wifi_SSID) { int id = -1; if (mWifiConfigList != null) { for (int j = 0; j < mWifiConfigList.size(); j++) { if (mWifiConfigList.get(j).SSID.equals("\"" + wifi_SSID + "\"")) { //如果要连接的wifi在已经配置好的列表中,那就设置允许链接,并且得到id id = mWifiConfigList.get(j).networkId; break; } } } return id; } }
源码下载:http://download.csdn.net/detail/shark0017/7962497
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。