├── app ├── .gitignore ├── src │ └── main │ │ ├── res │ │ └── values │ │ │ ├── dimens.xml │ │ │ ├── strings.xml │ │ │ ├── colors.xml │ │ │ └── styles.xml │ │ ├── java │ │ └── com │ │ │ └── iwdael │ │ │ └── wifimanager │ │ │ └── example │ │ │ └── WifiActivity.java │ │ └── AndroidManifest.xml ├── build.gradle └── proguard-rules.pro ├── wifimanager ├── .gitignore ├── src │ └── main │ │ ├── res │ │ └── values │ │ │ └── strings.xml │ │ ├── java │ │ └── com │ │ │ └── iwdael │ │ │ └── wifimanager │ │ │ ├── State.java │ │ │ ├── OnWifiConnectListener.java │ │ │ ├── OnWifiStateChangeListener.java │ │ │ ├── OnWifiChangeListener.java │ │ │ ├── IWifi.java │ │ │ ├── IWifiManager.java │ │ │ ├── WifiManager.java │ │ │ ├── Wifi.java │ │ │ ├── WifiHelper.java │ │ │ └── BaseWifiManager.java │ │ └── AndroidManifest.xml ├── build.gradle └── proguard-rules.pro ├── settings.gradle ├── screenshots.png ├── gradle └── wrapper │ ├── gradle-wrapper.jar │ └── gradle-wrapper.properties ├── .gitignore ├── config.gradle ├── gradle.properties ├── gradlew.bat ├── README.md └── gradlew /app/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /wifimanager/.gitignore: -------------------------------------------------------------------------------- 1 | /build 2 | -------------------------------------------------------------------------------- /settings.gradle: -------------------------------------------------------------------------------- 1 | include ':app' 2 | include ':wifimanager' 3 | -------------------------------------------------------------------------------- /screenshots.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iwdael/WifiManager/HEAD/screenshots.png -------------------------------------------------------------------------------- /app/src/main/res/values/dimens.xml: -------------------------------------------------------------------------------- 1 | 2 | 16dp 3 | 4 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.jar: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iwdael/WifiManager/HEAD/gradle/wrapper/gradle-wrapper.jar -------------------------------------------------------------------------------- /wifimanager/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | WifiManager 3 | 4 | -------------------------------------------------------------------------------- /app/src/main/res/values/strings.xml: -------------------------------------------------------------------------------- 1 | 2 | WifiManager 3 | Settings 4 | 5 | -------------------------------------------------------------------------------- /wifimanager/src/main/java/com/iwdael/wifimanager/State.java: -------------------------------------------------------------------------------- 1 | package com.iwdael.wifimanager; 2 | 3 | public enum State { 4 | ENABLING,ENABLED,DISABLING,DISABLED,UNKNOWN 5 | } 6 | -------------------------------------------------------------------------------- /wifimanager/src/main/java/com/iwdael/wifimanager/OnWifiConnectListener.java: -------------------------------------------------------------------------------- 1 | package com.iwdael.wifimanager; 2 | 3 | public interface OnWifiConnectListener { 4 | void onConnectChanged(boolean status); 5 | } 6 | -------------------------------------------------------------------------------- /wifimanager/src/main/java/com/iwdael/wifimanager/OnWifiStateChangeListener.java: -------------------------------------------------------------------------------- 1 | package com.iwdael.wifimanager; 2 | 3 | public interface OnWifiStateChangeListener { 4 | void onStateChanged(State state); 5 | } 6 | -------------------------------------------------------------------------------- /app/src/main/java/com/iwdael/wifimanager/example/WifiActivity.java: -------------------------------------------------------------------------------- 1 | package com.iwdael.wifimanager.example; 2 | 3 | import androidx.appcompat.app.AppCompatActivity; 4 | 5 | public class WifiActivity extends AppCompatActivity { 6 | } 7 | -------------------------------------------------------------------------------- /wifimanager/src/main/java/com/iwdael/wifimanager/OnWifiChangeListener.java: -------------------------------------------------------------------------------- 1 | package com.iwdael.wifimanager; 2 | 3 | import java.util.List; 4 | 5 | public interface OnWifiChangeListener { 6 | void onWifiChanged(List wifis); 7 | } 8 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | *.iml 2 | .gradle 3 | /local.properties 4 | /.idea/caches 5 | /.idea/libraries 6 | /.idea/modules.xml 7 | /.idea/workspace.xml 8 | /.idea/navEditor.xml 9 | /.idea/assetWizardSettings.xml 10 | .DS_Store 11 | /build 12 | /captures 13 | .externalNativeBuild 14 | -------------------------------------------------------------------------------- /gradle/wrapper/gradle-wrapper.properties: -------------------------------------------------------------------------------- 1 | #Tue Nov 23 11:33:34 CST 2021 2 | distributionBase=GRADLE_USER_HOME 3 | distributionPath=wrapper/dists 4 | zipStoreBase=GRADLE_USER_HOME 5 | zipStorePath=wrapper/dists 6 | distributionUrl=https\://services.gradle.org/distributions/gradle-6.1.1-all.zip 7 | -------------------------------------------------------------------------------- /app/src/main/res/values/colors.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | #008577 4 | #00574B 5 | #D81B60 6 | #00000000 7 | 8 | -------------------------------------------------------------------------------- /wifimanager/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 3 | 4 | 5 | 6 | 7 | 8 | -------------------------------------------------------------------------------- /wifimanager/build.gradle: -------------------------------------------------------------------------------- 1 | apply from: './../config.gradle' 2 | apply plugin: 'com.android.library' 3 | android { 4 | buildToolsVersion buidConfig.buildToolsVersion 5 | compileSdkVersion buidConfig.compileSdkVersion 6 | defaultConfig { 7 | minSdkVersion buidConfig.minSdkVersion 8 | targetSdkVersion buidConfig.targetSdkVersion 9 | versionCode buidConfig.versionCode 10 | versionName buidConfig.versionName 11 | } 12 | } 13 | dependencies { 14 | compileOnly dependent.appcompat 15 | } 16 | -------------------------------------------------------------------------------- /config.gradle: -------------------------------------------------------------------------------- 1 | ext { 2 | buidConfig = [ 3 | 'compileSdkVersion': 28, 4 | 'targetSdkVersion' : 28, 5 | 'minSdkVersion' : 19, 6 | 'versionCode' : 1, 7 | 'versionName' : '1.0.0', 8 | 'buildToolsVersion': '28.0.3' 9 | ] 10 | dependent = [ 11 | 'appcompat' : 'androidx.appcompat:appcompat:1.3.1', 12 | 'constraintlayout': 'androidx.constraintlayout:constraintlayout:2.1.1', 13 | 'androidxCore' : 'androidx.core:core-ktx:1.7.0', 14 | 'kotlinJdk7' : 'org.jetbrains.kotlin:kotlin-stdlib-jdk7:1.4.21', 15 | ] 16 | } -------------------------------------------------------------------------------- /wifimanager/src/main/java/com/iwdael/wifimanager/IWifi.java: -------------------------------------------------------------------------------- 1 | package com.iwdael.wifimanager; 2 | 3 | public interface IWifi { 4 | 5 | String name(); 6 | 7 | boolean isEncrypt(); 8 | 9 | boolean isSaved(); 10 | 11 | boolean isConnected(); 12 | 13 | String encryption(); 14 | 15 | int level(); 16 | 17 | String description(); 18 | 19 | String ip(); 20 | 21 | String description2(); 22 | 23 | void state(String state); 24 | 25 | @Deprecated 26 | String SSID(); 27 | 28 | @Deprecated 29 | String capabilities(); 30 | 31 | @Deprecated 32 | IWifi merge(IWifi merge); 33 | 34 | String state(); 35 | } 36 | -------------------------------------------------------------------------------- /app/build.gradle: -------------------------------------------------------------------------------- 1 | apply from: './../config.gradle' 2 | apply plugin: 'com.android.application' 3 | android { 4 | buildToolsVersion buidConfig.buildToolsVersion 5 | compileSdkVersion buidConfig.compileSdkVersion 6 | defaultConfig { 7 | applicationId "com.iwdael.wifimanager.example" 8 | minSdkVersion buidConfig.minSdkVersion 9 | targetSdkVersion buidConfig.targetSdkVersion 10 | versionCode buidConfig.versionCode 11 | versionName buidConfig.versionName 12 | } 13 | } 14 | dependencies { 15 | implementation dependent.appcompat 16 | implementation dependent.constraintlayout 17 | implementation project(':wifimanager') 18 | } 19 | -------------------------------------------------------------------------------- /app/src/main/AndroidManifest.xml: -------------------------------------------------------------------------------- 1 | 2 | 4 | 5 | 10 | 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | -------------------------------------------------------------------------------- /app/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /wifimanager/proguard-rules.pro: -------------------------------------------------------------------------------- 1 | # Add project specific ProGuard rules here. 2 | # You can control the set of applied configuration files using the 3 | # proguardFiles setting in build.gradle. 4 | # 5 | # For more details, see 6 | # http://developer.android.com/guide/developing/tools/proguard.html 7 | 8 | # If your project uses WebView with JS, uncomment the following 9 | # and specify the fully qualified class name to the JavaScript interface 10 | # class: 11 | #-keepclassmembers class fqcn.of.javascript.interface.for.webview { 12 | # public *; 13 | #} 14 | 15 | # Uncomment this to preserve the line number information for 16 | # debugging stack traces. 17 | #-keepattributes SourceFile,LineNumberTable 18 | 19 | # If you keep the line number information, uncomment this to 20 | # hide the original source file name. 21 | #-renamesourcefileattribute SourceFile 22 | -------------------------------------------------------------------------------- /wifimanager/src/main/java/com/iwdael/wifimanager/IWifiManager.java: -------------------------------------------------------------------------------- 1 | package com.iwdael.wifimanager; 2 | 3 | import java.util.List; 4 | 5 | public interface IWifiManager { 6 | 7 | boolean isOpened(); 8 | 9 | void openWifi(); 10 | 11 | void closeWifi(); 12 | 13 | void scanWifi(); 14 | 15 | boolean disConnectWifi(); 16 | 17 | boolean connectEncryptWifi(IWifi wifi, String password); 18 | 19 | boolean connectSavedWifi(IWifi wifi); 20 | 21 | boolean connectOpenWifi(IWifi wifi); 22 | 23 | boolean removeWifi(IWifi wifi); 24 | 25 | List getWifi(); 26 | 27 | void setOnWifiConnectListener(OnWifiConnectListener onWifiConnectListener); 28 | 29 | void setOnWifiStateChangeListener(OnWifiStateChangeListener onWifiStateChangeListener); 30 | 31 | void setOnWifiChangeListener(OnWifiChangeListener onWifiChangeListener); 32 | 33 | 34 | void destroy(); 35 | } 36 | -------------------------------------------------------------------------------- /gradle.properties: -------------------------------------------------------------------------------- 1 | # Project-wide Gradle settings. 2 | # IDE (e.g. Android Studio) users: 3 | # Gradle settings configured through the IDE *will override* 4 | # any settings specified in this file. 5 | # For more details on how to configure your build environment visit 6 | # http://www.gradle.org/docs/current/userguide/build_environment.html 7 | # Specifies the JVM arguments used for the daemon process. 8 | # The setting is particularly useful for tweaking memory settings. 9 | org.gradle.jvmargs=-Xmx2048m 10 | # When configured, Gradle will run in incubating parallel mode. 11 | # This option should only be used with decoupled projects. More details, visit 12 | # http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects 13 | # org.gradle.parallel=true 14 | # AndroidX package structure to make it clearer which packages are bundled with the 15 | # Android operating system, and which are packaged with your app"s APK 16 | # https://developer.android.com/topic/libraries/support-library/androidx-rn 17 | android.useAndroidX=true 18 | # Automatically convert third-party libraries to use AndroidX 19 | android.enableJetifier=true -------------------------------------------------------------------------------- /app/src/main/res/values/styles.xml: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 10 | 11 | 15 | 16 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /wifimanager/src/main/java/com/iwdael/wifimanager/WifiManager.java: -------------------------------------------------------------------------------- 1 | package com.iwdael.wifimanager; 2 | 3 | import android.content.Context; 4 | 5 | 6 | import java.util.List; 7 | 8 | public class WifiManager extends BaseWifiManager { 9 | 10 | private WifiManager(Context context) { 11 | super(context); 12 | } 13 | 14 | public static IWifiManager create(Context context) { 15 | return new WifiManager(context); 16 | } 17 | 18 | @Override 19 | public boolean isOpened() { 20 | return manager.isWifiEnabled(); 21 | } 22 | 23 | @Override 24 | public void openWifi() { 25 | if (!manager.isWifiEnabled()) 26 | manager.setWifiEnabled(true); 27 | } 28 | 29 | @Override 30 | public void closeWifi() { 31 | if (manager.isWifiEnabled()) 32 | manager.setWifiEnabled(false); 33 | } 34 | 35 | @Override 36 | public void scanWifi() { 37 | manager.startScan(); 38 | } 39 | 40 | @Override 41 | public boolean disConnectWifi() { 42 | return manager.disconnect(); 43 | } 44 | 45 | @Override 46 | public boolean connectEncryptWifi(IWifi wifi, String password) { 47 | if (manager.getConnectionInfo() != null && wifi.SSID().equals(manager.getConnectionInfo().getSSID())) 48 | return true; 49 | int networkId = WifiHelper.configOrCreateWifi(manager, wifi, password); 50 | boolean ret = manager.enableNetwork(networkId, true); 51 | modifyWifi(wifi.SSID(), "开始连接..."); 52 | return ret; 53 | } 54 | 55 | @Override 56 | public boolean connectSavedWifi(IWifi wifi) { 57 | int networkId = WifiHelper.configOrCreateWifi(manager, wifi, null); 58 | boolean ret = manager.enableNetwork(networkId, true); 59 | modifyWifi(wifi.SSID(), "开始连接..."); 60 | return ret; 61 | } 62 | 63 | @Override 64 | public boolean connectOpenWifi(IWifi wifi) { 65 | boolean ret = connectEncryptWifi(wifi, null); 66 | modifyWifi(wifi.SSID(), "开始连接..."); 67 | return ret; 68 | } 69 | 70 | @Override 71 | public boolean removeWifi(IWifi wifi) { 72 | boolean ret = WifiHelper.deleteWifiConfiguration(manager, wifi); 73 | modifyWifi(); 74 | return ret; 75 | } 76 | 77 | @Override 78 | public List getWifi() { 79 | return wifis; 80 | } 81 | 82 | 83 | } 84 | -------------------------------------------------------------------------------- /gradlew.bat: -------------------------------------------------------------------------------- 1 | @if "%DEBUG%" == "" @echo off 2 | @rem ########################################################################## 3 | @rem 4 | @rem Gradle startup script for Windows 5 | @rem 6 | @rem ########################################################################## 7 | 8 | @rem Set local scope for the variables with windows NT shell 9 | if "%OS%"=="Windows_NT" setlocal 10 | 11 | set DIRNAME=%~dp0 12 | if "%DIRNAME%" == "" set DIRNAME=. 13 | set APP_BASE_NAME=%~n0 14 | set APP_HOME=%DIRNAME% 15 | 16 | @rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 17 | set DEFAULT_JVM_OPTS= 18 | 19 | @rem Find java.exe 20 | if defined JAVA_HOME goto findJavaFromJavaHome 21 | 22 | set JAVA_EXE=java.exe 23 | %JAVA_EXE% -version >NUL 2>&1 24 | if "%ERRORLEVEL%" == "0" goto init 25 | 26 | echo. 27 | echo ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 28 | echo. 29 | echo Please set the JAVA_HOME variable in your environment to match the 30 | echo location of your Java installation. 31 | 32 | goto fail 33 | 34 | :findJavaFromJavaHome 35 | set JAVA_HOME=%JAVA_HOME:"=% 36 | set JAVA_EXE=%JAVA_HOME%/bin/java.exe 37 | 38 | if exist "%JAVA_EXE%" goto init 39 | 40 | echo. 41 | echo ERROR: JAVA_HOME is set to an invalid directory: %JAVA_HOME% 42 | echo. 43 | echo Please set the JAVA_HOME variable in your environment to match the 44 | echo location of your Java installation. 45 | 46 | goto fail 47 | 48 | :init 49 | @rem Get command-line arguments, handling Windows variants 50 | 51 | if not "%OS%" == "Windows_NT" goto win9xME_args 52 | 53 | :win9xME_args 54 | @rem Slurp the command line arguments. 55 | set CMD_LINE_ARGS= 56 | set _SKIP=2 57 | 58 | :win9xME_args_slurp 59 | if "x%~1" == "x" goto execute 60 | 61 | set CMD_LINE_ARGS=%* 62 | 63 | :execute 64 | @rem Setup the command line 65 | 66 | set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar 67 | 68 | @rem Execute Gradle 69 | "%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS% 70 | 71 | :end 72 | @rem End local scope for the variables with windows NT shell 73 | if "%ERRORLEVEL%"=="0" goto mainEnd 74 | 75 | :fail 76 | rem Set variable GRADLE_EXIT_CONSOLE if you need the _script_ return code instead of 77 | rem the _cmd.exe /c_ return code! 78 | if not "" == "%GRADLE_EXIT_CONSOLE%" exit 1 79 | exit /b 1 80 | 81 | :mainEnd 82 | if "%OS%"=="Windows_NT" endlocal 83 | 84 | :omega 85 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # WifiManager 2 | ![](https://img.shields.io/badge/platform-android-orange.svg) 3 | ![](https://img.shields.io/badge/language-java-yellow.svg) 4 | ![](https://jitpack.io/v/com.iwdael/wifimanager.svg) 5 | ![](https://img.shields.io/badge/build-passing-brightgreen.svg) 6 | ![](https://img.shields.io/badge/license-apache--2.0-green.svg) 7 | ![](https://img.shields.io/badge/api-19+-green.svg) 8 | 9 | 简化Android Wifi开发,已实现常用的功能,比如获取WiFi管理,WiFi列表,断开、清除或者连接WiFi。 10 | ## 特点 11 | * 根据信号强度排序 12 | * 连接或操作中的WIFI自动置顶 13 | * 自动刷新WIFI列表 14 | * 支持常见Wifi操作 15 |


16 | ![](https://github.com/iwdael/WifiManager/blob/master/screenshots.png) 17 |

18 | ## 概述 19 | WifiManager主要是由*IWifiManager*和*IWifi*组成,IWifiManager中可实现三个接口,完成Wifi列表数据更新 20 | ### Class 21 | #### IWifiManager 22 | 23 | |方法|功能| 24 | |:------:|:------:| 25 | |isOpened()|WIFI是否打开| 26 | |openWifi()|打开WIFI| 27 | |closeWifi()|关闭WIFI| 28 | |scanWifi()|扫描WIFI。此方法为异步操作,扫描结果会通过接口回掉| 29 | |disConnectWifi()|断开当前连接| 30 | |connectEncryptWifi(IWifi wifi, String password)|通过密码,连接WIFI| 31 | |connectSavedWifi(IWifi wifi)|连接已保存的WIFI| 32 | |connectOpenWifi(IWifi wifi)|连接开放的WIFI| 33 | |removeWifi(IWifi wifi)|清除已保存的WIFI| 34 | |getWifi()|获取WIFI列表。此方法不建议使用,若是WIFI列表有变化会通过接口回调| 35 | |setOnWifiConnectListener(OnWifiConnectListener listener)|wifi连接状态变化监听| 36 | |setOnWifiStateChangeListener(OnWifiStateChangeListener listener)|wifi状态变化监听| 37 | |setOnWifiChangeListener(OnWifiChangeListener listener)|wifi列表变化监听。包含WiFi数目变化,WiFi状态变化等,用户只需要展示即可| 38 | |destroy()|IWifiManager不需要使用时,需销毁| 39 | 40 | #### IWifi 41 | |方法|功能| 42 | |:------:|:------:| 43 | |name()|Wifi名称| 44 | |isEncrypt()|是否加密| 45 | |isSaved()|密码是否保存| 46 | |isConnected()|是否连接| 47 | |encryption()|加密类型描述,例如:WEP等| 48 | |level()|信号强度| 49 | |ip()|若当前WiFi连接,ip不为空| 50 | |description()|wifi描述| 51 | |description2()|wifi描述,含Ip| 52 | 53 | #### OnWifiChangeListener 54 | 55 | |方法|功能| 56 | |:------:|:------:| 57 | |onWifiChanged(List wifis)|wifi列表,wifi所有变化都会通过此方法,只需要展示此列表即可| 58 | 59 | #### OnWifiConnectListener 60 | 61 | |方法|功能| 62 | |:------:|:------:| 63 | |onConnectChanged(boolean status)|wifi是否连接| 64 | 65 | #### OnWifiStateChangeListener 66 | 67 | |方法|功能| 68 | |:------:|:------:| 69 | |onStateChanged(State state)|wifi状态,例如:开启中,已开启,关闭中,已关闭| 70 | 71 | ## 使用说明 72 | 1.获取*IWifiManager* 73 | ``` 74 | IWifiManager.create(Context context); 75 | ``` 76 | 2.销毁*IWifiManager* 77 | ``` 78 | iWifiManager.destroy(); 79 | ``` 80 | ## 快速引入项目 81 | 合并以下代码到需要使用的Module的dependencies中。 82 | ``` 83 | dependencies { 84 | ... 85 | implementation 'com.iwdael:wifimanager:$version' 86 | } 87 | ``` 88 |


89 | ## 感谢浏览 90 | 请不要吝啬你的小星星,如果你有任何疑问,请加入QQ群,我将竭诚为你解答。 91 |
92 | ![Image Text](https://github.com/iwdael/CarouselBanner/blob/master/qq_group.png) 93 | -------------------------------------------------------------------------------- /wifimanager/src/main/java/com/iwdael/wifimanager/Wifi.java: -------------------------------------------------------------------------------- 1 | package com.iwdael.wifimanager; 2 | 3 | import android.net.wifi.ScanResult; 4 | import android.net.wifi.WifiConfiguration; 5 | import android.text.TextUtils; 6 | 7 | import java.util.List; 8 | 9 | public class Wifi implements IWifi { 10 | protected String name; 11 | protected String SSID; 12 | protected boolean isEncrypt; 13 | protected boolean isSaved; 14 | protected boolean isConnected; 15 | protected String encryption; 16 | protected String description; 17 | protected String capabilities; 18 | protected String ip; 19 | protected String state; 20 | protected int level; 21 | 22 | 23 | public static IWifi create(ScanResult result, List configurations, String connectedSSID, int ipAddress) { 24 | if (TextUtils.isEmpty(result.SSID)) 25 | return null; 26 | Wifi wifi = new Wifi(); 27 | wifi.isConnected = false; 28 | wifi.isSaved = false; 29 | wifi.name = result.SSID; 30 | wifi.SSID = "\"" + result.SSID + "\""; 31 | // wifi.isConnected = wifi.SSID.equals(connectedSSID) && ipAddress > 0; 32 | wifi.isConnected = wifi.SSID.equals(connectedSSID); 33 | wifi.capabilities = result.capabilities; 34 | wifi.isEncrypt = true; 35 | wifi.encryption = ""; 36 | wifi.level = result.level; 37 | wifi.ip = wifi.isConnected ? String.format("%d.%d.%d.%d", (ipAddress & 0xff), (ipAddress >> 8 & 0xff), (ipAddress >> 16 & 0xff), (ipAddress >> 24 & 0xff)) : ""; 38 | if (wifi.capabilities.toUpperCase().contains("WPA2-PSK") && wifi.capabilities.toUpperCase().contains("WPA-PSK")) { 39 | wifi.encryption = "WPA/WPA2"; 40 | } else if (wifi.capabilities.toUpperCase().contains("WPA-PSK")) { 41 | wifi.encryption = "WPA"; 42 | } else if (wifi.capabilities.toUpperCase().contains("WPA2-PSK")) { 43 | wifi.encryption = "WPA2"; 44 | } else { 45 | wifi.isEncrypt = false; 46 | } 47 | wifi.description = wifi.encryption; 48 | for (WifiConfiguration configuration : configurations) { 49 | if (configuration.SSID.equals(wifi.SSID)) { 50 | wifi.isSaved = true; 51 | break; 52 | } 53 | } 54 | if (wifi.isSaved) { 55 | wifi.description = "已保存"; 56 | } 57 | if (wifi.isConnected) { 58 | wifi.description = "已连接"; 59 | } 60 | return wifi; 61 | } 62 | 63 | @Override 64 | public String name() { 65 | return name; 66 | } 67 | 68 | @Override 69 | public boolean isEncrypt() { 70 | return isEncrypt; 71 | } 72 | 73 | @Override 74 | public boolean isSaved() { 75 | return isSaved; 76 | } 77 | 78 | @Override 79 | public boolean isConnected() { 80 | return isConnected; 81 | } 82 | 83 | @Override 84 | public String encryption() { 85 | return encryption; 86 | } 87 | 88 | @Override 89 | public int level() { 90 | return level; 91 | } 92 | 93 | @Override 94 | public String description() { 95 | return state == null ? description : state; 96 | } 97 | 98 | @Override 99 | public String ip() { 100 | return ip; 101 | } 102 | 103 | @Override 104 | public String description2() { 105 | return isConnected ? String.format("%s(%s)", description(), ip) : description(); 106 | } 107 | 108 | @Override 109 | public void state(String state) { 110 | this.state = state; 111 | } 112 | 113 | @Override 114 | public String SSID() { 115 | return SSID; 116 | } 117 | 118 | @Override 119 | public String capabilities() { 120 | return capabilities; 121 | } 122 | 123 | @Override 124 | public IWifi merge(IWifi merge) { 125 | isSaved = merge.isSaved(); 126 | isConnected = merge.isConnected(); 127 | ip = merge.ip(); 128 | state = merge.state(); 129 | level = merge.level(); 130 | description = ((Wifi) merge).description; 131 | return this; 132 | } 133 | 134 | @Override 135 | public String state() { 136 | return state; 137 | } 138 | 139 | 140 | @Override 141 | public boolean equals(Object obj) { 142 | if (obj == null || (!(obj instanceof Wifi))) return false; 143 | return ((Wifi) obj).SSID.equals(this.SSID); 144 | } 145 | 146 | @Override 147 | public String toString() { 148 | return "{" + 149 | "\"name\":\'" + name + "\'" + 150 | ", \"SSID\":\'" + SSID + "\'" + 151 | ", \"isEncrypt\":" + isEncrypt + 152 | ", \"isSaved\":" + isSaved + 153 | ", \"isConnected\":" + isConnected + 154 | ", \"encryption\":\'" + encryption + "\'" + 155 | ", \"description\":\'" + description + "\'" + 156 | ", \"capabilities\":\'" + capabilities + "\'" + 157 | ", \"ip\":\'" + ip + "\'" + 158 | ", \"state\":\'" + state + "\'" + 159 | ", \"level\":" + level + 160 | '}'; 161 | } 162 | } 163 | -------------------------------------------------------------------------------- /gradlew: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env sh 2 | 3 | ############################################################################## 4 | ## 5 | ## Gradle start up script for UN*X 6 | ## 7 | ############################################################################## 8 | 9 | # Attempt to set APP_HOME 10 | # Resolve links: $0 may be a link 11 | PRG="$0" 12 | # Need this for relative symlinks. 13 | while [ -h "$PRG" ] ; do 14 | ls=`ls -ld "$PRG"` 15 | link=`expr "$ls" : '.*-> \(.*\)$'` 16 | if expr "$link" : '/.*' > /dev/null; then 17 | PRG="$link" 18 | else 19 | PRG=`dirname "$PRG"`"/$link" 20 | fi 21 | done 22 | SAVED="`pwd`" 23 | cd "`dirname \"$PRG\"`/" >/dev/null 24 | APP_HOME="`pwd -P`" 25 | cd "$SAVED" >/dev/null 26 | 27 | APP_NAME="Gradle" 28 | APP_BASE_NAME=`basename "$0"` 29 | 30 | # Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script. 31 | DEFAULT_JVM_OPTS="" 32 | 33 | # Use the maximum available, or set MAX_FD != -1 to use that value. 34 | MAX_FD="maximum" 35 | 36 | warn () { 37 | echo "$*" 38 | } 39 | 40 | die () { 41 | echo 42 | echo "$*" 43 | echo 44 | exit 1 45 | } 46 | 47 | # OS specific support (must be 'true' or 'false'). 48 | cygwin=false 49 | msys=false 50 | darwin=false 51 | nonstop=false 52 | case "`uname`" in 53 | CYGWIN* ) 54 | cygwin=true 55 | ;; 56 | Darwin* ) 57 | darwin=true 58 | ;; 59 | MINGW* ) 60 | msys=true 61 | ;; 62 | NONSTOP* ) 63 | nonstop=true 64 | ;; 65 | esac 66 | 67 | CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar 68 | 69 | # Determine the Java command to use to start the JVM. 70 | if [ -n "$JAVA_HOME" ] ; then 71 | if [ -x "$JAVA_HOME/jre/sh/java" ] ; then 72 | # IBM's JDK on AIX uses strange locations for the executables 73 | JAVACMD="$JAVA_HOME/jre/sh/java" 74 | else 75 | JAVACMD="$JAVA_HOME/bin/java" 76 | fi 77 | if [ ! -x "$JAVACMD" ] ; then 78 | die "ERROR: JAVA_HOME is set to an invalid directory: $JAVA_HOME 79 | 80 | Please set the JAVA_HOME variable in your environment to match the 81 | location of your Java installation." 82 | fi 83 | else 84 | JAVACMD="java" 85 | which java >/dev/null 2>&1 || die "ERROR: JAVA_HOME is not set and no 'java' command could be found in your PATH. 86 | 87 | Please set the JAVA_HOME variable in your environment to match the 88 | location of your Java installation." 89 | fi 90 | 91 | # Increase the maximum file descriptors if we can. 92 | if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then 93 | MAX_FD_LIMIT=`ulimit -H -n` 94 | if [ $? -eq 0 ] ; then 95 | if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then 96 | MAX_FD="$MAX_FD_LIMIT" 97 | fi 98 | ulimit -n $MAX_FD 99 | if [ $? -ne 0 ] ; then 100 | warn "Could not set maximum file descriptor limit: $MAX_FD" 101 | fi 102 | else 103 | warn "Could not query maximum file descriptor limit: $MAX_FD_LIMIT" 104 | fi 105 | fi 106 | 107 | # For Darwin, add options to specify how the application appears in the dock 108 | if $darwin; then 109 | GRADLE_OPTS="$GRADLE_OPTS \"-Xdock:name=$APP_NAME\" \"-Xdock:icon=$APP_HOME/media/gradle.icns\"" 110 | fi 111 | 112 | # For Cygwin, switch paths to Windows format before running java 113 | if $cygwin ; then 114 | APP_HOME=`cygpath --path --mixed "$APP_HOME"` 115 | CLASSPATH=`cygpath --path --mixed "$CLASSPATH"` 116 | JAVACMD=`cygpath --unix "$JAVACMD"` 117 | 118 | # We build the pattern for arguments to be converted via cygpath 119 | ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null` 120 | SEP="" 121 | for dir in $ROOTDIRSRAW ; do 122 | ROOTDIRS="$ROOTDIRS$SEP$dir" 123 | SEP="|" 124 | done 125 | OURCYGPATTERN="(^($ROOTDIRS))" 126 | # Add a user-defined pattern to the cygpath arguments 127 | if [ "$GRADLE_CYGPATTERN" != "" ] ; then 128 | OURCYGPATTERN="$OURCYGPATTERN|($GRADLE_CYGPATTERN)" 129 | fi 130 | # Now convert the arguments - kludge to limit ourselves to /bin/sh 131 | i=0 132 | for arg in "$@" ; do 133 | CHECK=`echo "$arg"|egrep -c "$OURCYGPATTERN" -` 134 | CHECK2=`echo "$arg"|egrep -c "^-"` ### Determine if an option 135 | 136 | if [ $CHECK -ne 0 ] && [ $CHECK2 -eq 0 ] ; then ### Added a condition 137 | eval `echo args$i`=`cygpath --path --ignore --mixed "$arg"` 138 | else 139 | eval `echo args$i`="\"$arg\"" 140 | fi 141 | i=$((i+1)) 142 | done 143 | case $i in 144 | (0) set -- ;; 145 | (1) set -- "$args0" ;; 146 | (2) set -- "$args0" "$args1" ;; 147 | (3) set -- "$args0" "$args1" "$args2" ;; 148 | (4) set -- "$args0" "$args1" "$args2" "$args3" ;; 149 | (5) set -- "$args0" "$args1" "$args2" "$args3" "$args4" ;; 150 | (6) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" ;; 151 | (7) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" ;; 152 | (8) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" ;; 153 | (9) set -- "$args0" "$args1" "$args2" "$args3" "$args4" "$args5" "$args6" "$args7" "$args8" ;; 154 | esac 155 | fi 156 | 157 | # Escape application args 158 | save () { 159 | for i do printf %s\\n "$i" | sed "s/'/'\\\\''/g;1s/^/'/;\$s/\$/' \\\\/" ; done 160 | echo " " 161 | } 162 | APP_ARGS=$(save "$@") 163 | 164 | # Collect all arguments for the java command, following the shell quoting and substitution rules 165 | eval set -- $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS "\"-Dorg.gradle.appname=$APP_BASE_NAME\"" -classpath "\"$CLASSPATH\"" org.gradle.wrapper.GradleWrapperMain "$APP_ARGS" 166 | 167 | # by default we should be in the correct project dir, but when run from Finder on Mac, the cwd is wrong 168 | if [ "$(uname)" = "Darwin" ] && [ "$HOME" = "$PWD" ]; then 169 | cd "$(dirname "$0")" 170 | fi 171 | 172 | exec "$JAVACMD" "$@" 173 | -------------------------------------------------------------------------------- /wifimanager/src/main/java/com/iwdael/wifimanager/WifiHelper.java: -------------------------------------------------------------------------------- 1 | package com.iwdael.wifimanager; 2 | 3 | import android.net.wifi.WifiConfiguration; 4 | import android.net.wifi.WifiManager; 5 | 6 | import java.util.ArrayList; 7 | import java.util.Collections; 8 | import java.util.Comparator; 9 | import java.util.List; 10 | 11 | public class WifiHelper { 12 | public static final String WEP = "WEP"; 13 | public static final String PSK = "PSK"; 14 | public static final String EAP = "EAP"; 15 | public static final String WPA = "WPA"; 16 | 17 | 18 | public static int configOrCreateWifi(WifiManager manager, IWifi wifi, String password) { 19 | List configurations = manager.getConfiguredNetworks(); 20 | for (WifiConfiguration configuration : configurations) { 21 | if (configuration.SSID.equals(wifi.SSID())) 22 | return configuration.networkId; 23 | } 24 | WifiConfiguration configuration = createWifiConfiguration(wifi, password); 25 | return saveWifiConfiguration(manager, configuration); 26 | } 27 | 28 | public static boolean deleteWifiConfiguration(WifiManager manager, IWifi wifi) { 29 | List configurations = manager.getConfiguredNetworks(); 30 | for (WifiConfiguration configuration : configurations) { 31 | if (configuration.SSID.equals(wifi.SSID())) { 32 | boolean ret = manager.removeNetwork(configuration.networkId); 33 | ret = ret & manager.saveConfiguration(); 34 | return ret; 35 | } 36 | } 37 | return false; 38 | } 39 | 40 | private static WifiConfiguration createWifiConfiguration(IWifi wifi, String password) { 41 | WifiConfiguration configuration = new WifiConfiguration(); 42 | if (password == null) { 43 | configuration.hiddenSSID = false; 44 | configuration.status = WifiConfiguration.Status.ENABLED; 45 | configuration.SSID = wifi.SSID(); 46 | if (wifi.capabilities().contains(WEP)) { 47 | configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 48 | configuration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 49 | configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); 50 | configuration.wepTxKeyIndex = 0; 51 | configuration.wepKeys[0] = ""; 52 | } else if (wifi.capabilities().contains(PSK)) { 53 | configuration.preSharedKey = ""; 54 | } else if (wifi.capabilities().contains(EAP)) { 55 | configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_EAP); 56 | configuration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 57 | configuration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 58 | configuration.allowedProtocols.set(WifiConfiguration.Protocol.WPA); 59 | configuration.preSharedKey = ""; 60 | } else { 61 | configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 62 | configuration.preSharedKey = null; 63 | } 64 | } else { 65 | configuration.allowedAuthAlgorithms.clear(); 66 | configuration.allowedGroupCiphers.clear(); 67 | configuration.allowedKeyManagement.clear(); 68 | configuration.allowedPairwiseCiphers.clear(); 69 | configuration.allowedProtocols.clear(); 70 | configuration.SSID = wifi.SSID(); 71 | if (wifi.capabilities().contains(WEP)) { 72 | configuration.preSharedKey = "\"" + password + "\""; 73 | configuration.hiddenSSID = true; 74 | configuration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.SHARED); 75 | configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 76 | configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 77 | configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP40); 78 | configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.WEP104); 79 | configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 80 | configuration.wepTxKeyIndex = 0; 81 | } else if (wifi.capabilities().contains(WPA)) { 82 | configuration.hiddenSSID = true; 83 | configuration.preSharedKey = "\"" + password + "\""; 84 | configuration.allowedAuthAlgorithms.set(WifiConfiguration.AuthAlgorithm.OPEN); 85 | configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.TKIP); 86 | configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.WPA_PSK); 87 | configuration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.TKIP); 88 | configuration.allowedGroupCiphers.set(WifiConfiguration.GroupCipher.CCMP); 89 | configuration.allowedPairwiseCiphers.set(WifiConfiguration.PairwiseCipher.CCMP); 90 | } else { 91 | configuration.wepKeys[0] = ""; 92 | configuration.allowedKeyManagement.set(WifiConfiguration.KeyMgmt.NONE); 93 | configuration.wepTxKeyIndex = 0; 94 | } 95 | } 96 | 97 | return configuration; 98 | } 99 | 100 | 101 | private static int saveWifiConfiguration(WifiManager manager, WifiConfiguration configuration) { 102 | int networkId = manager.addNetwork(configuration); 103 | manager.saveConfiguration(); 104 | return networkId; 105 | } 106 | 107 | public static List removeDuplicate(List list) { 108 | Collections.sort(list, new Comparator() { 109 | @Override 110 | public int compare(IWifi l, IWifi r) { 111 | return r.level() - l.level(); 112 | } 113 | }); 114 | List set = new ArrayList<>(); 115 | for (IWifi wifi : list) { 116 | if (!set.contains(wifi)) { 117 | if (wifi.isConnected()) 118 | set.add(0, wifi); 119 | else 120 | set.add(wifi); 121 | } 122 | } 123 | return set; 124 | } 125 | } 126 | -------------------------------------------------------------------------------- /wifimanager/src/main/java/com/iwdael/wifimanager/BaseWifiManager.java: -------------------------------------------------------------------------------- 1 | package com.iwdael.wifimanager; 2 | 3 | import android.content.BroadcastReceiver; 4 | import android.content.Context; 5 | import android.content.Intent; 6 | import android.content.IntentFilter; 7 | import android.net.NetworkInfo; 8 | import android.net.wifi.ScanResult; 9 | import android.net.wifi.WifiConfiguration; 10 | import android.net.wifi.WifiManager; 11 | import android.os.Handler; 12 | import android.os.Looper; 13 | import android.os.Message; 14 | import android.text.TextUtils; 15 | 16 | import java.util.ArrayList; 17 | import java.util.LinkedList; 18 | import java.util.List; 19 | 20 | public abstract class BaseWifiManager implements IWifiManager { 21 | 22 | static final int WIFI_STATE_DISABLED = 1; 23 | static final int WIFI_STATE_DISABLING = 2; 24 | static final int WIFI_STATE_ENABLING = 3; 25 | static final int WIFI_STATE_ENABLED = 4; 26 | static final int WIFI_STATE_UNKNOWN = 5; 27 | static final int WIFI_STATE_MODIFY = 6; 28 | static final int WIFI_STATE_CONNECTED = 7; 29 | static final int WIFI_STATE_UNCONNECTED = 8; 30 | 31 | 32 | WifiManager manager; 33 | List wifis; 34 | OnWifiChangeListener onWifiChangeListener; 35 | OnWifiConnectListener onWifiConnectListener; 36 | OnWifiStateChangeListener onWifiStateChangeListener; 37 | WifiReceiver wifiReceiver; 38 | Context context; 39 | Handler handler = new Handler(Looper.getMainLooper()) { 40 | @Override 41 | public void handleMessage(Message msg) { 42 | switch (msg.what) { 43 | case WIFI_STATE_DISABLED: 44 | if (onWifiStateChangeListener != null) 45 | onWifiStateChangeListener.onStateChanged(State.DISABLED); 46 | break; 47 | case WIFI_STATE_DISABLING: 48 | if (onWifiStateChangeListener != null) 49 | onWifiStateChangeListener.onStateChanged(State.DISABLING); 50 | break; 51 | case WIFI_STATE_ENABLING: 52 | if (onWifiStateChangeListener != null) 53 | onWifiStateChangeListener.onStateChanged(State.ENABLING); 54 | break; 55 | case WIFI_STATE_ENABLED: 56 | if (onWifiStateChangeListener != null) 57 | onWifiStateChangeListener.onStateChanged(State.ENABLED); 58 | break; 59 | case WIFI_STATE_UNKNOWN: 60 | if (onWifiStateChangeListener != null) 61 | onWifiStateChangeListener.onStateChanged(State.UNKNOWN); 62 | break; 63 | case WIFI_STATE_MODIFY: 64 | if (onWifiChangeListener != null) 65 | onWifiChangeListener.onWifiChanged(wifis); 66 | break; 67 | case WIFI_STATE_CONNECTED: 68 | if (onWifiConnectListener != null) 69 | onWifiConnectListener.onConnectChanged(true); 70 | break; 71 | case WIFI_STATE_UNCONNECTED: 72 | if (onWifiConnectListener != null) 73 | onWifiConnectListener.onConnectChanged(false); 74 | break; 75 | } 76 | } 77 | }; 78 | 79 | BaseWifiManager(Context context) { 80 | this.context = context; 81 | manager = (WifiManager) context.getApplicationContext().getSystemService(Context.WIFI_SERVICE); 82 | wifis = new ArrayList<>(); 83 | wifiReceiver = new WifiReceiver(); 84 | IntentFilter filter = new IntentFilter(); 85 | filter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); 86 | filter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION); 87 | filter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION); 88 | filter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION); 89 | context.registerReceiver(wifiReceiver, filter); 90 | } 91 | 92 | @Override 93 | public void destroy() { 94 | context.unregisterReceiver(wifiReceiver); 95 | handler.removeCallbacksAndMessages(null); 96 | manager = null; 97 | wifis = null; 98 | context = null; 99 | } 100 | 101 | @Override 102 | public void setOnWifiChangeListener(OnWifiChangeListener onWifiChangeListener) { 103 | this.onWifiChangeListener = onWifiChangeListener; 104 | } 105 | 106 | @Override 107 | public void setOnWifiConnectListener(OnWifiConnectListener onWifiConnectListener) { 108 | this.onWifiConnectListener = onWifiConnectListener; 109 | } 110 | 111 | @Override 112 | public void setOnWifiStateChangeListener(OnWifiStateChangeListener onWifiStateChangeListener) { 113 | this.onWifiStateChangeListener = onWifiStateChangeListener; 114 | } 115 | 116 | public class WifiReceiver extends BroadcastReceiver { 117 | @Override 118 | public void onReceive(Context context, Intent intent) { 119 | String action = intent.getAction(); 120 | if (TextUtils.isEmpty(action)) return; 121 | if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { 122 | int state = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE, WifiManager.WIFI_STATE_UNKNOWN); 123 | int what = 0; 124 | switch (state) { 125 | case WifiManager.WIFI_STATE_DISABLED: 126 | what = WIFI_STATE_DISABLED; 127 | break; 128 | case WifiManager.WIFI_STATE_DISABLING: 129 | what = WIFI_STATE_DISABLING; 130 | break; 131 | case WifiManager.WIFI_STATE_ENABLING: 132 | what = WIFI_STATE_ENABLING; 133 | break; 134 | case WifiManager.WIFI_STATE_ENABLED: 135 | scanWifi(); 136 | what = WIFI_STATE_ENABLED; 137 | break; 138 | case WifiManager.WIFI_STATE_UNKNOWN: 139 | what = WIFI_STATE_UNKNOWN; 140 | break; 141 | } 142 | handler.sendEmptyMessage(what); 143 | } else if (action.equals(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION)) { 144 | if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.M) { 145 | boolean isUpdated = intent.getBooleanExtra(WifiManager.EXTRA_RESULTS_UPDATED, false); 146 | if (isUpdated) 147 | modifyWifi(); 148 | } else { 149 | modifyWifi(); 150 | } 151 | } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) { 152 | NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); 153 | if (info == null) return; 154 | NetworkInfo.DetailedState state = info.getDetailedState(); 155 | if (state == null) return; 156 | String SSID = info.getExtraInfo(); 157 | if (TextUtils.isEmpty(SSID)) return; 158 | if (state == NetworkInfo.DetailedState.IDLE) { 159 | } else if (state == NetworkInfo.DetailedState.SCANNING) { 160 | } else if (state == NetworkInfo.DetailedState.AUTHENTICATING) { 161 | modifyWifi(SSID, "身份验证中..."); 162 | } else if (state == NetworkInfo.DetailedState.OBTAINING_IPADDR) { 163 | modifyWifi(SSID, "获取地址信息..."); 164 | } else if (state == NetworkInfo.DetailedState.CONNECTED) { 165 | // modifyWifi(SSID, "已连接"); 166 | modifyWifi(); 167 | handler.sendEmptyMessage(WIFI_STATE_CONNECTED); 168 | } else if (state == NetworkInfo.DetailedState.SUSPENDED) { 169 | modifyWifi(SSID, "连接中断"); 170 | } else if (state == NetworkInfo.DetailedState.DISCONNECTING) { 171 | modifyWifi(SSID, "断开中..."); 172 | } else if (state == NetworkInfo.DetailedState.DISCONNECTED) { 173 | // modifyWifi(SSID, "已断开"); 174 | modifyWifi(); 175 | handler.sendEmptyMessage(WIFI_STATE_UNCONNECTED); 176 | } else if (state == NetworkInfo.DetailedState.FAILED) { 177 | modifyWifi(SSID, "连接失败"); 178 | } else if (state == NetworkInfo.DetailedState.BLOCKED) { 179 | modifyWifi(SSID, "wifi无效"); 180 | } else if (state == NetworkInfo.DetailedState.VERIFYING_POOR_LINK) { 181 | modifyWifi(SSID, "信号差"); 182 | } else if (state == NetworkInfo.DetailedState.CAPTIVE_PORTAL_CHECK) { 183 | modifyWifi(SSID, "强制登陆门户"); 184 | } 185 | } else if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) { 186 | NetworkInfo info = intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO); 187 | if (info == null) return; 188 | NetworkInfo.DetailedState state = info.getDetailedState(); 189 | if (state == null) return; 190 | String SSID = info.getExtraInfo(); 191 | if (TextUtils.isEmpty(SSID)) return; 192 | int code = intent.getIntExtra(WifiManager.EXTRA_SUPPLICANT_ERROR, -1); 193 | if (code == WifiManager.ERROR_AUTHENTICATING) { 194 | modifyWifi(SSID, "密码错误"); 195 | } else { 196 | modifyWifi(SSID, "身份验证出现问题"); 197 | } 198 | } 199 | } 200 | } 201 | 202 | protected void modifyWifi() { 203 | synchronized (wifis) { 204 | List results = manager.getScanResults(); 205 | List wifiList = new LinkedList<>(); 206 | List mergeList = new ArrayList<>(); 207 | List configurations = manager.getConfiguredNetworks(); 208 | String connectedSSID = manager.getConnectionInfo().getSSID(); 209 | int ipAddress = manager.getConnectionInfo().getIpAddress(); 210 | for (ScanResult result : results) { 211 | IWifi mergeObj = Wifi.create(result, configurations, connectedSSID, ipAddress); 212 | if (mergeObj == null) continue; 213 | mergeList.add(mergeObj); 214 | } 215 | mergeList = WifiHelper.removeDuplicate(mergeList); 216 | for (IWifi merge : mergeList) { 217 | boolean isMerge = false; 218 | for (IWifi wifi : wifis) { 219 | if (wifi.equals(merge)) { 220 | wifiList.add(wifi.merge(merge)); 221 | isMerge = true; 222 | } 223 | } 224 | if (!isMerge) 225 | wifiList.add(merge); 226 | } 227 | wifis.clear(); 228 | wifis.addAll(wifiList); 229 | handler.sendEmptyMessage(WIFI_STATE_MODIFY); 230 | } 231 | } 232 | 233 | protected void modifyWifi(String SSID, String state) { 234 | synchronized (wifis) { 235 | List wifiList = new ArrayList<>(); 236 | for (IWifi wifi : wifis) { 237 | if (SSID.equals(wifi.SSID())) { 238 | wifi.state(state); 239 | wifiList.add(0, wifi); 240 | } else { 241 | wifi.state(null); 242 | wifiList.add(wifi); 243 | } 244 | } 245 | wifis.clear(); 246 | wifis.addAll(wifiList); 247 | handler.sendEmptyMessage(WIFI_STATE_MODIFY); 248 | } 249 | } 250 | } 251 | --------------------------------------------------------------------------------