别裁伪体亲风雅,转益多师是汝师。这篇文章主要讲述Android连接wifi,调用系统API相关的知识,希望能为你提供帮助。
本文转载自:http://blog.csdn.net/aaa1050070637/article/details/54136472
直接上代码,简单粗暴,一看就懂
【Android连接wifi,调用系统API】
[java]
view plain
copy
- import android.content.Context;
- import android.net.ConnectivityManager;
- import android.net.NetworkInfo;
- import android.net.wifi.ScanResult;
- import android.net.wifi.WifiConfiguration;
- import android.net.wifi.WifiInfo;
- import android.net.wifi.WifiManager;
- import android.util.Log;
- import java.util.List;
- /**
- * create 2016.12.29
- *
- * @author jack大漠
- *
- */
- public class WifiConnect {
- private static final String SSID = "Sunlight";
- private static final String PASSWORD = "12345678";
- public static final String WIFI_IS_NOT_OPEN = "wifi is closed.";
- private static final String TAG = "vivi";
- public WifiManager mWifiManager = null;
- private Context mContext = null;
- private int mNetworkID = -1;
- public WifiConnect(Context context) {
- this.mContext = context;
- mWifiManager = (WifiManager) context
- .getSystemService(Context.WIFI_SERVICE);
- }
- /**
- * 网络是否连接
- */
- public boolean isWifiConnected() {
- if (mContext != null) {
- ConnectivityManager mConnectivityManager = (ConnectivityManager) mContext
- .getSystemService(Context.CONNECTIVITY_SERVICE);
- NetworkInfo mNetworkInfo = mConnectivityManager
- .getActiveNetworkInfo();
- if (mNetworkInfo != null
- & & mNetworkInfo.getType() == ConnectivityManager.TYPE_WIFI) {
- return mNetworkInfo.isConnected();
- }
- }
- return false;
- }
- /**
- * Show wifi list
- */
- public String getWifiList() {
- if (WifiManager.WIFI_STATE_ENABLED != mWifiManager.getWifiState()) {
- return WIFI_IS_NOT_OPEN;
- }
- String text = "";
- List< ScanResult> results = mWifiManager.getScanResults();
- for (ScanResult result : results) {
- String SSID = result.SSID;
- if (SSID.startsWith("Sun")) {
- int db = result.level;
- text += SSID + ":" + db;
- return text;
- }
- }
- return "";
- }
- /**
- * 打开wifi开关
- */
- public void openWifi() {
- mWifiManager.setWifiEnabled(true);
- }
- public void closeWifi() {
- mWifiManager.setWifiEnabled(false);
- }
- /**
- * 是否打开开关
- */
- public boolean isWifiOpened() {
- return WifiManager.WIFI_STATE_ENABLED == mWifiManager.getWifiState();
- }
- public boolean isWifiEnable() {
- return mWifiManager.isWifiEnabled();
- }
- /**
- * 关闭开关
- */
- public void shutdownWiFi() {
- mWifiManager.setWifiEnabled(false);
- mNetworkID = -1;
- }
- /**
- * 连接指定Wifi
- */
- public boolean connectWifi(final String ssid, final String password,
- final SecurityMode mode) {
- WifiInfo info = getWifiInfo();
- Log.d(TAG, "connectWifi----> " + info.toString() + " ");
- if (isWifiConnected() & & info != null
- & & info.getSSID().equals("\"" + ssid + "\"")) {
- return true;
- }
- Log.d(TAG, "connectWifi----> " + isWifiConnected() + " ");
- List< WifiConfiguration> existingConfigs = mWifiManager
- .getConfiguredNetworks();
- if (existingConfigs == null) {
- return false;
- }
- for (WifiConfiguration existingConfig : existingConfigs) {
- if (existingConfig.SSID.equals("\"" + ssid + "\"")) {
- mNetworkID = existingConfig.networkId;
- break;
- }
- }
- if (mNetworkID < = -1) {
- WifiConfiguration config = new WifiConfiguration();
- config.SSID = "\"" + ssid + "\"";
- if (mode == SecurityMode.OPEN) {
- config.wepKeys[0] = "";
- config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
- config.wepTxKeyIndex = 0;
- } else if (password != null & & !"".equals(password)) {
- if (mode == SecurityMode.WEP) {
- config.preSharedKey = "\"" + password + "\"";
- config.allowedAuthAlgorithms
- .set(WifiConfiguration.AuthAlgorithm.SHARED);
- config.allowedGroupCiphers
- .set(WifiConfiguration.GroupCipher.CCMP);
- config.allowedGroupCiphers
- .set(WifiConfiguration.GroupCipher.TKIP);
- config.allowedGroupCiphers
- .set(WifiConfiguration.GroupCipher.WEP40);
- config.allowedGroupCiphers
- .set(WifiConfiguration.GroupCipher.WEP104);
- config.allowedKeyManagement
- .set(WifiConfiguration.KeyMgmt.NONE);
- config.wepTxKeyIndex = 0;
- } else {
- config.preSharedKey = "\"" + password + "\"";
- config.allowedAuthAlgorithms
- .set(WifiConfiguration.AuthAlgorithm.OPEN);
- config.allowedGroupCiphers
- .set(WifiConfiguration.GroupCipher.TKIP);
- config.allowedKeyManagement
- .set(WifiConfiguration.KeyMgmt.WPA_PSK);
- config.allowedPairwiseCiphers
- .set(WifiConfiguration.PairwiseCipher.TKIP);
- config.allowedGroupCiphers
- .set(WifiConfiguration.GroupCipher.CCMP);
- config.allowedPairwiseCiphers
- .set(WifiConfiguration.PairwiseCipher.CCMP);
- config.status = WifiConfiguration.Status.ENABLED;
- }
- }
- mNetworkID = mWifiManager.addNetwork(config);
- }
- // 连接该网络
- return mWifiManager.enableNetwork(mNetworkID, true);
- }
- public WifiInfo getWifiInfo() {
- WifiInfo info = null;
- try {
- if (isWifiConnected()) {
- info = mWifiManager.getConnectionInfo();
- }
- } catch (Exception e) {
- Log.e(TAG, "getWifiInfo", e);
- }
- return info;
- }
- /**
- * 网络加密模式
- */
- public enum SecurityMode {
- OPEN, WEP, WPA2
- }
- /**
- * 初始化WiFi连接
- */
- public boolean initWifiConnect() {
- Log.d("vivi", "初始化Wifi连接----> " + " 开始连接");
- return connectWifi(SSID, PASSWORD, SecurityMode.WPA2);
- }
- }
推荐阅读
- MappedByteBuffer文件句柄释放问题
- Xamarin安卓开发(去掉Activity的头部标题栏及全屏显示)
- Mybatis框架中Mapper动态代理方式
- 计算机毕业设计源码分享-801双鱼林安卓Android美食评论爱食app
- Unable to load script from assets 'index.android.bundle' 出错()
- win xp系统下如何运用截图工具的箭头
- win xp系统下如何下载搜狐视频|电脑下载搜狐视频的办法
- win xp系统下搜狐通行证密码失去的处理办法
- xp系统下怎样搜狐浏览器窗口全屏显示|搜狐浏览器让窗口全屏的办法