Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Step 2. Add the dependency
dependencies {
compile 'com.github.kongqw:AndroidWiFiManager:1.1.1'
}
// WIFI管理器
mWiFiManager = new WiFiManager(getApplicationContext());
mWiFiManager.openWiFi();
mWiFiManager.closeWiFi();
mWiFiManager.setOnWifiEnabledListener(this);
/**
* WIFI开关状态的回调
*
* @param enabled true 打开 false 关闭
*/
@Override
public void onWifiEnabled(boolean enabled) {
// TODO
}
mWiFiManager.removeOnWifiEnabledListener();
List<ScanResult> scanResults = mWiFiManager.getScanResults();
mWiFiManager.getSecurityMode(scanResult)
注意:Android 6.0需要动态获取 Manifest.permission.ACCESS_FINE_LOCATION 或 Manifest.permission.ACCESS_COARSE_LOCATION 后,才能正常获取到WIFI列表。
mWiFiManager.setOnWifiScanResultsListener(this);
/**
* WIFI列表刷新后的回调
*
* @param scanResults 扫描结果
*/
@Override
public void onScanResults(List<ScanResult> scanResults) {
// TODO
}
mWiFiManager.getScanResults(); 是返回当前的WIFI列表,回调返回的是扫描更新以后新的WIFI列表。
mWiFiManager.removeOnWifiScanResultsListener();
mWiFiManager.connectOpenNetwork(scanResult.SSID);
mWiFiManager.connectWPA2Network(scanResult.SSID, password);
mWiFiManager.connectWEPNetwork(scanResult.SSID, password);
mWiFiManager.setOnWifiConnectListener(this);
/**
* 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
}
mWiFiManager.removeOnWifiConnectListener();
mWiFiManager.disconnectWifi(connectionInfo.getNetworkId());
只能删除自己创建的配置,其他应用生成的配置需要Root权限才可以删除。
mWiFiManager.deleteConfig(wifiConfiguration.networkId);