Android连接wifi,调用系统API【转】
本文转载自:http://blog.csdn.net/aaa1050070637/article/details/54136472
直接上代码,简单粗暴,一看就懂
- 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);
- }
- }
Android连接wifi,调用系统API【转】的更多相关文章
- Android上传图片之调用系统拍照和从相冊选择图片
Android上传图片之调用系统拍照和从相冊选择图片 本篇文章已授权微信公众号 guolin_blog (郭霖)独家公布 前言: 万丈高楼平底起,万事起于微末.不知不觉距离上篇博文已近四个月,2015 ...
- Android下载图片/调用系统相机拍照、显示并保存到本地
package com.example.testhttpget; import java.io.BufferedReader; import java.io.FileNotFoundException ...
- 【Android学习】调用系统相机
Android调用系统相机分三步走: 首先是要设置调用相机的权限. 其次是给按钮加打开相机的事件. 最后是拍照后进行图片的保存. 第一步,添加权限: <!-- 调用系统相机 --> < ...
- Android 连接Wifi和创建Wifi热点 demo
android的热点功能不可见,用了反射的技术搞定之外. Eclipse设置语言为utf-8才能查看中文注释 上代码: MainActivity.java package com.widget.hot ...
- android 连接wifi案例
1.xml布局文件: <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...
- 【Android学习】调用系统短信、电话
今天全心投入Android学习已经有一段时间了,从起初的啥也不懂,到现在半知半解状态,随笔记录些简单且常用的系统功能代码. 调用Android系统短信,其实调用短信非常简单,一个方法就可以搞定.我们可 ...
- 调用系统api修改系统时间
一:截图 二:代码 using System; using System.Collections.Generic; using System.ComponentModel; using System. ...
- android 7.0 调用系统相机崩溃的解决方案(非谷歌官方推荐)
解决方案: 1.(推荐)7.0之后你的app就算有权限,给出一个URI之后手机也认为你没有权限. 不用修改原有代码,在Application的oncreate方法中:(或者直接放在调用相机的activ ...
- C# 调用系统API 内核 简单样例
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.R ...
随机推荐
- Vue编写轮播组件引入better-scroll插件无法正常循环轮播
临近过年还是发个博客表示一下自己的存在感,这段时间公司突然说想搞小程序,想到这无比巨大的坑就只能掩面而泣,于是乎这段时间在学习小程序开发.关于标题所说的是有老铁问的,我也跟着网上的代码码了一遍然后发现 ...
- 2. TypeScript笔记
1. 安装node.js之后 需要测试npm命令 2.命令正常安装TypeScript 3.安装Egret egret 命令
- 大数据学习——下载集群根目录下的文件到E盘
代码如下: package cn.itcast.hdfs; import java.io.IOException; import org.apache.hadoop.conf.Configuratio ...
- 70.打印所有Spring boot载入的bean【从零开始学Spring Boot】
[从零开始学习Spirng Boot-常见异常汇总] 问题的提出: 我们在开发过程当中,我们可能会碰到这样的问题:No qualifying bean 就是我们定义的bean无法进行注入,那到底是什 ...
- Linux基础之重定向|grep
重定向 > :覆盖输出>> :追加输出 2> :重定向错误输出2>> : 追加方式 &> : 重定向标准输出或者错误输出 ...
- hdu3516 Tree Construction (四边形不等式)
题意:给定一些点(xi,yi)(xj,yj)满足:i<j,xi<xj,yi>yj.用下面的连起来,使得所有边的长度最小? 题解:直接给出吧 f[i][j]=min(f[i][k]+f ...
- IntelliJ IDEA平台下JNI编程—HelloWorld篇
转载请注明出处:[huachao1001的专栏:http://blog.csdn.net/huachao1001/article/details/53906237] JNI(Java Native I ...
- 【BZOJ1237】配对(贪心,DP)
题意:有n个a[i]和b[i],调整顺序使abs(a[i]-b[i])之和最小,但a[i]<>b[i].保证所有 Ai各不相同,Bi也各不相同. 30%的数据满足:n <= 104 ...
- androidstudio Cannot resolve symbol 'xxx'
Android Studio 无法识别同一个 package 里的其他类,将其显示为红色,但是 compile 没有问题.鼠标放上去后显示 “Cannot resolve symbol XXX”,重启 ...
- 搭建Spring+mybatis报错
java.lang.ClassCastException: com.sun.proxy.$Proxy12 cannot be cast to com.bdqn.service.impl.UserSer ...