package com.ethanco.wifitransmit;
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.ScanResult;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.util.Log;
import java.util.List;
import java.util.Timer;
import java.util.TimerTask;
/**
* Created by EthanCo on 2016/4/24.
*/
public abstract class WifiAdmin {
private static final String TAG = "WifiAdmin";
private WifiManager mWifiManager;
private WifiInfo mWifiInfo;
// 扫描出的网络连接列表
private List<ScanResult> mWifiList;
private List<WifiConfiguration> mWifiConfiguration;
private WifiManager.WifiLock mWifiLock;
private String mPasswd = "";
private String mSSID = "";
private Context mContext = null;
public WifiAdmin(Context context) {
mContext = context;
// 取得WifiManager对象
mWifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
// 取得WifiInfo对象
mWifiInfo = mWifiManager.getConnectionInfo();
Log.v(TAG, "getIpAddress = " + mWifiInfo.getIpAddress());
}
// 打开WIFI
public void openWifi() {
if (!mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(true);
}
}
// 关闭WIFI
public void closeWifi() {
if (mWifiManager.isWifiEnabled()) {
mWifiManager.setWifiEnabled(false);
}
}
public abstract Intent myRegisterReceiver(BroadcastReceiver receiver, IntentFilter filter);
public abstract void myUnregisterReceiver(BroadcastReceiver receiver);
public abstract void onNotifyWifiConnected();
public abstract void onNotifyWifiConnectFailed();
// 添加一个网络并连接
public void addNetwork(WifiConfiguration wcg) {
register();
WifiApAdmin.closeWifiAp(mContext);
int wcgID = mWifiManager.addNetwork(wcg);
boolean b = mWifiManager.enableNetwork(wcgID, true);
}
public static final int TYPE_NO_PASSWD = 0x11;
public static final int TYPE_WEP = 0x12;
public static final int TYPE_WPA = 0x13;
public void addNetwork(String ssid, String passwd, int type) {
if (ssid == null || passwd == null || ssid.equals("")) {
Log.e(TAG, "addNetwork() ## nullpointer error!");
return;
}
if (type != TYPE_NO_PASSWD && type != TYPE_WEP && type != TYPE_WPA) {
Log.e(TAG, "addNetwork() ## unknown type = " + type);
}
stopTimer();
unRegister();
addNetwork(createWifiInfo(ssid, passwd, type));
}
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
if (intent.getAction().equals(WifiManager.RSSI_CHANGED_ACTION)) {
Log.d(TAG, "RSSI changed");
//有可能是正在获取,或者已经获取了
Log.d(TAG, " intent is " + WifiManager.RSSI_CHANGED_ACTION);
if (isWifiContected(mContext) == WIFI_CONNECTED) {
stopTimer();
onNotifyWifiConnected();
unRegister();
} else if (isWifiContected(mContext) == WIFI_CONNECT_FAILED) {
stopTimer();
closeWifi();
onNotifyWifiConnectFailed();
unRegister();
} else if (isWifiContected(mContext) == WIFI_CONNECTING) {
}
}
}
};
private final int STATE_REGISTRING = 0x01;
private final int STATE_REGISTERED = 0x02;
private final int STATE_UNREGISTERING = 0x03;
private final int STATE_UNREGISTERED = 0x04;
private int mHaveRegister = STATE_UNREGISTERED;
private synchronized void register() {
Log.v(TAG, "register() ##mHaveRegister = " + mHaveRegister);
if (mHaveRegister == STATE_REGISTRING
|| mHaveRegister == STATE_REGISTERED) {
return ;
}
mHaveRegister = STATE_REGISTRING;
myRegisterReceiver(mBroadcastReceiver, new IntentFilter(WifiManager.RSSI_CHANGED_ACTION));
mHaveRegister = STATE_REGISTERED;
startTimer();
}
private synchronized void unRegister() {
Log.v(TAG, "unRegister() ##mHaveRegister = " + mHaveRegister);
if (mHaveRegister == STATE_UNREGISTERED
|| mHaveRegister == STATE_UNREGISTERING) {
return ;
}
mHaveRegister = STATE_UNREGISTERING;
myUnregisterReceiver(mBroadcastReceiver);
mHaveRegister = STATE_UNREGISTERED;
}
private Timer mTimer = null;
private void startTimer() {
if (mTimer != null) {
stopTimer();
}
mTimer = new Timer(true);
// mTimer.schedule(mTimerTask, 0, 20 * 1000);// 20s
mTimer.schedule(mTimerTask, 30 * 1000);
}
private TimerTask mTimerTask = new TimerTask() {
@Override
public void run() {
// TODO Auto-generated method stub
Log.e(TAG, "timer out!");
onNotifyWifiConnectFailed();
unRegister();
}
};
private void stopTimer() {
if (mTimer != null) {
mTimer.cancel();
mTimer = null;
}
}
@Override
protected void finalize() {
try {
super.finalize();
unRegister();
} catch (Throwable e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public WifiConfiguration createWifiInfo(String SSID, String password, int type) {
Log.v(TAG, "SSID = " + SSID + "## Password = " + password + "## Type = " + type);
WifiConfiguration config = new WifiConfiguration();
config.allowedAuthAlgorithms.clear();
config.allowedGroupCiphers.clear();
config.allowedKeyManagement.clear();
config.allowedPairwiseCiphers.clear();
config.allowedProtocols.clear();
config.SSID = "\"" + SSID + "\"";
WifiConfiguration tempConfig = this.IsExsits(SSID);
if (tempConfig != null) {
mWifiManager.removeNetwork(tempConfig.networkId);
}
// 分为三种情况:1没有密码2用wep加密3用wpa加密
if (type == TYPE_NO_PASSWD) {// WIFICIPHER_NOPASS
config.wepKeys[0] = "";
config.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE);
config.wepTxKeyIndex = 0;
} else if (type == TYPE_WEP) { // WIFICIPHER_WEP
config.hiddenSSID = true;
config.wepKeys[0] = "\"" + 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 if (type == TYPE_WPA) { // WIFICIPHER_WPA
config.preSharedKey = "\"" + password + "\"";
config.hiddenSSID = true;
config.allowedAuthAlgorithms
.
没有合适的资源?快使用搜索试试~ 我知道了~
资源推荐
资源详情
资源评论
收起资源包目录
Android Wifi开发Demo示例 (113个子文件)
gradlew.bat 2KB
gradlew.bat 2KB
fileSnapshots.bin 3.81MB
fileSnapshots.bin 1.24MB
fileHashes.bin 175KB
fileHashes.bin 136KB
taskArtifacts.bin 128KB
taskArtifacts.bin 94KB
outputFileStates.bin 20KB
outputFileStates.bin 20KB
.gitignore 105B
.gitignore 105B
.gitignore 8B
.gitignore 8B
build.gradle 813B
build.gradle 651B
build.gradle 572B
build.gradle 528B
settings.gradle 16B
settings.gradle 16B
gradlew 5KB
gradlew 5KB
app.iml 10KB
app.iml 9KB
WifiTransmit.iml 943B
WifiTest.iml 939B
gradle-wrapper.jar 52KB
gradle-wrapper.jar 52KB
WifiAdmin.java 13KB
QuickTransferActivity.java 12KB
WifiAdmin.java 8KB
MainActivity.java 7KB
WifiApAdmin.java 5KB
WifiAdapter.java 4KB
WifiUtil.java 3KB
MyTimerCheck.java 2KB
ApplicationTest.java 367B
ApplicationTest.java 363B
ExampleUnitTest.java 331B
ExampleUnitTest.java 327B
Constant.java 281B
cache.properties.lock 17B
cache.properties.lock 17B
.name 12B
.name 8B
ic_launcher.png 10KB
ic_launcher.png 10KB
ic_launcher.png 8KB
ic_launcher.png 8KB
ic_launcher.png 5KB
ic_launcher.png 5KB
ic_launcher.png 3KB
ic_launcher.png 3KB
ic_launcher.png 2KB
ic_launcher.png 2KB
proguard-rules.pro 654B
proguard-rules.pro 654B
gradle.properties 872B
gradle.properties 872B
local.properties 419B
local.properties 419B
gradle-wrapper.properties 231B
gradle-wrapper.properties 231B
cache.properties 31B
cache.properties 31B
workspace.xml 168KB
workspace.xml 159KB
misc.xml 5KB
misc.xml 5KB
AndroidManifest.xml 3KB
item_wifi_info.xml 2KB
main.xml 1KB
AndroidManifest.xml 1KB
gradle.xml 739B
gradle.xml 739B
support_v4_23_3_0.xml 721B
support_v4_23_3_0.xml 721B
compiler.xml 686B
compiler.xml 686B
library_1_1_5.xml 660B
core_0_8_5_8.xml 652B
animated_vector_drawable_23_3_0.xml 637B
animated_vector_drawable_23_3_0.xml 637B
support_vector_drawable_23_3_0.xml 632B
support_vector_drawable_23_3_0.xml 632B
recyclerview_v7_23_3_0.xml 592B
appcompat_v7_23_3_0.xml 577B
appcompat_v7_23_3_0.xml 577B
runConfigurations.xml 564B
runConfigurations.xml 564B
hamcrest_core_1_3.xml 526B
hamcrest_core_1_3.xml 526B
activity_main.xml 504B
support_annotations_23_3_0.xml 479B
support_annotations_23_3_0.xml 479B
junit_4_12.xml 477B
junit_4_12.xml 477B
styles.xml 394B
styles.xml 394B
strings.xml 379B
共 113 条
- 1
- 2
氦客
- 粉丝: 2w+
- 资源: 56
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
- 1
- 2
- 3
- 4
前往页