Skip to content

Commit

Permalink
update
Browse files Browse the repository at this point in the history
  • Loading branch information
kongqw committed May 5, 2023
0 parents commit c303552
Show file tree
Hide file tree
Showing 42 changed files with 2,235 additions and 0 deletions.
8 changes: 8 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
*.iml
.gradle
/local.properties
/.idea/workspace.xml
/.idea/libraries
.DS_Store
/build
/captures
193 changes: 193 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,193 @@
# Android WIFI控制

[![](https://jitpack.io/v/kongqw/AndroidWiFiManager.svg)](https://jitpack.io/#kongqw/AndroidWiFiManager)

Step 1. Add the JitPack repository to your build file

Add it in your root build.gradle at the end of repositories:

``` gradle
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
```

Step 2. Add the dependency

``` gradle
dependencies {
compile 'com.github.kongqw:AndroidWiFiManager:1.1.1'
}
```

## 初始化

``` java
// WIFI管理器
mWiFiManager = new WiFiManager(getApplicationContext());
```

## 打开WIFI

``` java
mWiFiManager.openWiFi();
```

## 关闭WIFI

``` java
mWiFiManager.closeWiFi();
```

## 添加WIFI开关状态的监听

``` java
mWiFiManager.setOnWifiEnabledListener(this);
```

### 回调

``` java
/**
* WIFI开关状态的回调
*
* @param enabled true 打开 false 关闭
*/
@Override
public void onWifiEnabled(boolean enabled) {
// TODO
}
```

## 移除WIFI开关状态的监听

``` java
mWiFiManager.removeOnWifiEnabledListener();
```

## 获取WIFI列表

``` java
List<ScanResult> scanResults = mWiFiManager.getScanResults();
```

### 获取WIFI加密方式

``` java
mWiFiManager.getSecurityMode(scanResult)
```

> 注意:Android 6.0需要动态获取 Manifest.permission.ACCESS_FINE_LOCATION 或 Manifest.permission.ACCESS_COARSE_LOCATION 后,才能正常获取到WIFI列表。
## 添加获取WIFI列表的监听

``` java
mWiFiManager.setOnWifiScanResultsListener(this);
```

### 回调

``` java
/**
* WIFI列表刷新后的回调
*
* @param scanResults 扫描结果
*/
@Override
public void onScanResults(List<ScanResult> scanResults) {
// TODO
}
```

> mWiFiManager.getScanResults(); 是返回当前的WIFI列表,回调返回的是扫描更新以后新的WIFI列表。
## 移除获取WIFI列表的监听

``` java
mWiFiManager.removeOnWifiScanResultsListener();
```

## 连接到开放网络

``` java
mWiFiManager.connectOpenNetwork(scanResult.SSID);
```

## 连接到WPA/WPA2网络

``` java
mWiFiManager.connectWPA2Network(scanResult.SSID, password);
```

## 连接到WEP网络

``` java
mWiFiManager.connectWEPNetwork(scanResult.SSID, password);
```

## 添加连接WIFI的监听

``` java
mWiFiManager.setOnWifiConnectListener(this);
```

### 回调

``` java
/**
* WIFI连接的Log得回调
*
* @param log log
*/
@Override
public void onWiFiConnectLog(String log) {
Log.i(TAG, "onWiFiConnectLog: " + log);
// TODO
}

/**
* WIFI连接成功的回调
*
* @param SSID 热点名
*/
@Override
public void onWiFiConnectSuccess(String SSID) {
Log.i(TAG, "onWiFiConnectSuccess: [ " + SSID + " ] 连接成功");
// TODO
}

/**
* WIFI连接失败的回调
*
* @param SSID 热点名
*/
@Override
public void onWiFiConnectFailure(String SSID) {
Log.i(TAG, "onWiFiConnectFailure: [ " + SSID + " ] 连接失败");
// TODO
}
```

## 移除连接WIFI的监听

``` java
mWiFiManager.removeOnWifiConnectListener();
```

## 断开网络连接

``` java
mWiFiManager.disconnectWifi(connectionInfo.getNetworkId());
```

## 删除网络配置

> 只能删除自己创建的配置,其他应用生成的配置需要Root权限才可以删除。
``` java
mWiFiManager.deleteConfig(wifiConfiguration.networkId);
```

1 change: 1 addition & 0 deletions WiFiLibrary/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/build
31 changes: 31 additions & 0 deletions WiFiLibrary/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"

defaultConfig {
minSdkVersion 14
targetSdkVersion 25
versionCode 1
versionName "1.0"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
androidTestImplementation('com.android.support.test.espresso:espresso-core:2.2.2', {
exclude group: 'com.android.support', module: 'support-annotations'
})
implementation 'com.android.support:appcompat-v7:25.2.0'
testImplementation 'junit:junit:4.13.2'
}
17 changes: 17 additions & 0 deletions WiFiLibrary/proguard-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in /Users/kongqingwei/Library/Android/sdk/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the proguardFiles
# directive in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# Add any project specific keep options here:

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}
32 changes: 32 additions & 0 deletions WiFiLibrary/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kongqw.wifilibrary">

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/>

<application
android:allowBackup="true"
android:label="@string/app_name"
android:supportsRtl="true"
>

<!-- 监听网络状态的广播接收者 -->
<receiver android:name="com.kongqw.wifilibrary.WiFiManager$NetworkBroadcastReceiver">
<intent-filter>
<!-- AP扫描完成,客户端得到可用的结果集 -->
<action android:name="android.net.wifi.SCAN_RESULTS"/>
<action android:name="android.net.wifi.WIFI_STATE_CHANGED"/>
<action android:name="android.net.wifi.STATE_CHANGE"/>
<action android:name="android.net.wifi.supplicant.STATE_CHANGE"/>
<action android:name="android.net.wifi.supplicant.CONNECTION_CHANGE"/>
</intent-filter>
</receiver>

</application>

</manifest>
Loading

0 comments on commit c303552

Please sign in to comment.