diff --git a/.gitignore b/.gitignore
index 4db771a..c380c39 100644
--- a/.gitignore
+++ b/.gitignore
@@ -5,4 +5,5 @@
.DS_Store
/build
/captures
-.externalNativeBuild
\ No newline at end of file
+.externalNativeBuild
+.project
\ No newline at end of file
diff --git a/README.md b/README.md
index 3518487..862a899 100644
--- a/README.md
+++ b/README.md
@@ -150,6 +150,43 @@ WifiUtils.withContext(getApplicationContext())
.start();
```
+### Disconnect
+
+You can disconnect from the currently connected network.
+
+```java
+WifiUtils.withContext(context)
+ .disconnect(new DisconnectionSuccessListener() {
+ @Override
+ public void success() {
+ Toast.makeText(MainActivity.this, "Disconnect success!", Toast.LENGTH_SHORT).show();
+ }
+
+ @Override
+ public void failed(@NonNull DisconnectionErrorCode errorCode) {
+ Toast.makeText(MainActivity.this, "Failed to disconnect: " + errorCode.toString(), Toast.LENGTH_SHORT).show();
+ }
+ });
+```
+
+### Disconnect and remove saved network configuration
+
+You can also remove the saved wifi network configuration. On Android 10, this will just simply disconnect (as wifi configuration's made by WifiUtils are no longer saved).
+Notice: WifiUtils can't remove network configurations created by the user or by another app.
+
+```kotlin
+WifiUtils.withContext(context)
+ .remove(SSID, object : RemoveSuccessListener {
+ override fun success() {
+ Toast.makeText(context, "Remove success!", Toast.LENGTH_SHORT).show()
+ }
+
+ override fun failed(errorCode: RemoveErrorCode) {
+ Toast.makeText(context, "Failed to disconnect and remove: $errorCode", Toast.LENGTH_SHORT).show()
+ }
+ })
+```
+
### Enable Logging
If you want to receive some extra logging info comming from WiFi Utils you can enable its logging capabilities with `WifiUtils.enableLog(true);`
@@ -159,7 +196,6 @@ Damn You are required to set a few permissions in order for this lib to work cor
```xml
-
```
### Add it to your project
diff --git a/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainActivity.java b/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainActivity.java
index 0c7a1e0..c7ad9ae 100644
--- a/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainActivity.java
+++ b/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainActivity.java
@@ -63,7 +63,7 @@ private void connectWithWpa() {
private void disconnect(final Context context) {
WifiUtils.withContext(context)
- .disconnectFrom(SSID, new DisconnectionSuccessListener() {
+ .disconnect(new DisconnectionSuccessListener() {
@Override
public void success() {
Toast.makeText(MainActivity.this, "Disconnect success!", Toast.LENGTH_SHORT).show();
diff --git a/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainKotlinActivity.kt b/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainKotlinActivity.kt
index 1eb6778..06d3561 100644
--- a/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainKotlinActivity.kt
+++ b/sample/src/main/java/com/thanosfisherman/wifiutils/sample/MainKotlinActivity.kt
@@ -11,6 +11,8 @@ import com.thanosfisherman.wifiutils.wifiConnect.ConnectionErrorCode
import com.thanosfisherman.wifiutils.wifiConnect.ConnectionSuccessListener
import com.thanosfisherman.wifiutils.wifiDisconnect.DisconnectionErrorCode
import com.thanosfisherman.wifiutils.wifiDisconnect.DisconnectionSuccessListener
+import com.thanosfisherman.wifiutils.wifiRemove.RemoveErrorCode
+import com.thanosfisherman.wifiutils.wifiRemove.RemoveSuccessListener
import kotlinx.android.synthetic.main.activity_main.*
class MainKotlinActivity : AppCompatActivity() {
@@ -23,8 +25,11 @@ class MainKotlinActivity : AppCompatActivity() {
setContentView(R.layout.activity_main)
ActivityCompat.requestPermissions(this, arrayOf(Manifest.permission.ACCESS_FINE_LOCATION), 555)
WifiUtils.enableLog(true)
+ textview_ssid.text = SSID
+ textview_password.text = PASSWORD
button_connect.setOnClickListener { connectWithWpa(applicationContext) }
button_disconnect.setOnClickListener { disconnect(applicationContext) }
+ button_remove.setOnClickListener{ remove(applicationContext) }
}
private fun connectWithWpa(context: Context) {
@@ -45,7 +50,7 @@ class MainKotlinActivity : AppCompatActivity() {
private fun disconnect(context: Context) {
WifiUtils.withContext(context)
- .disconnectFrom(SSID, object : DisconnectionSuccessListener {
+ .disconnect(object : DisconnectionSuccessListener {
override fun success() {
Toast.makeText(context, "Disconnect success!", Toast.LENGTH_SHORT).show()
}
@@ -55,4 +60,17 @@ class MainKotlinActivity : AppCompatActivity() {
}
})
}
+
+ private fun remove(context: Context) {
+ WifiUtils.withContext(context)
+ .remove(SSID, object : RemoveSuccessListener {
+ override fun success() {
+ Toast.makeText(context, "Remove success!", Toast.LENGTH_SHORT).show()
+ }
+
+ override fun failed(errorCode: RemoveErrorCode) {
+ Toast.makeText(context, "Failed to disconnect and remove: $errorCode", Toast.LENGTH_SHORT).show()
+ }
+ })
+ }
}
\ No newline at end of file
diff --git a/sample/src/main/res/layout/activity_main.xml b/sample/src/main/res/layout/activity_main.xml
index 79af269..d926e8e 100644
--- a/sample/src/main/res/layout/activity_main.xml
+++ b/sample/src/main/res/layout/activity_main.xml
@@ -7,26 +7,57 @@
android:layout_height="match_parent"
tools:context="com.thanosfisherman.wifiutils.sample.MainActivity">
+
+
+
+
+ app:layout_constraintTop_toTopOf="@id/textview_password" />
+ app:layout_constraintRight_toRightOf="parent" />
+
+
+
diff --git a/sample/src/main/res/values/strings.xml b/sample/src/main/res/values/strings.xml
index 79805da..d57fc1f 100644
--- a/sample/src/main/res/values/strings.xml
+++ b/sample/src/main/res/values/strings.xml
@@ -2,4 +2,5 @@
WifiUtils
Connect
Disconnect
+ Remove
diff --git a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/ConnectorUtils.java b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/ConnectorUtils.java
index e141044..67fbbe2 100644
--- a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/ConnectorUtils.java
+++ b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/ConnectorUtils.java
@@ -18,17 +18,17 @@
import android.os.Build;
import android.provider.Settings;
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
-import androidx.annotation.RequiresApi;
-import androidx.annotation.RequiresPermission;
-
import com.thanosfisherman.elvis.Objects;
import com.thanosfisherman.wifiutils.wifiWps.ConnectionWpsListener;
import java.util.Collections;
import java.util.List;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.annotation.RequiresApi;
+import androidx.annotation.RequiresPermission;
+
import static android.Manifest.permission.ACCESS_FINE_LOCATION;
import static android.Manifest.permission.ACCESS_WIFI_STATE;
import static com.thanosfisherman.wifiutils.WifiUtils.wifiLog;
@@ -170,21 +170,6 @@ static void unregisterReceiver(@NonNull final Context context, @Nullable final B
}
}
- @RequiresPermission(ACCESS_WIFI_STATE)
- static boolean disconnectFromWifi(@NonNull final Context context, @NonNull final ConnectivityManager connectivityManager, @NonNull final WifiManager wifiManager, @NonNull final String ssid) {
- if (isAndroidQOrLater()) {
- if (networkCallback != null) {
- connectivityManager.unregisterNetworkCallback(networkCallback);
- networkCallback = null;
- }
-
- return true;
- }
-
- final WifiConfiguration wifiConfiguration = ConfigSecurities.getWifiConfiguration(wifiManager, ssid);
- return cleanPreviousConfiguration(wifiManager, wifiConfiguration);
- }
-
@RequiresPermission(allOf = {ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE})
static boolean connectToWifi(@NonNull final Context context, @Nullable final WifiManager wifiManager, @NonNull final ScanResult scanResult, @NonNull final String password) {
if (wifiManager == null) {
@@ -319,6 +304,9 @@ public void onAvailable(@NonNull Network network) {
super.onAvailable(network);
wifiLog("AndroidQ+ connected to wifi ");
+
+ // bind so all api calls are performed over this new network
+ connectivityManager.bindProcessToNetwork(network);
}
@Override
@@ -480,6 +468,35 @@ public void onFailed(int reason) {
wifiManager.startWps(wpsInfo, wpsCallback);
}
+ @RequiresPermission(ACCESS_WIFI_STATE)
+ static boolean disconnectFromWifi(@NonNull final ConnectivityManager connectivityManager, @NonNull final WifiManager wifiManager) {
+ if (isAndroidQOrLater()) {
+ return disconnectAndroidQ(connectivityManager);
+ }
+
+ return wifiManager.disconnect();
+ }
+
+ @RequiresPermission(ACCESS_WIFI_STATE)
+ static boolean removeWifi(@NonNull final ConnectivityManager connectivityManager, @NonNull final WifiManager wifiManager, @NonNull final String ssid) {
+ if (isAndroidQOrLater()) {
+ return disconnectAndroidQ(connectivityManager);
+ }
+
+ final WifiConfiguration wifiConfiguration = ConfigSecurities.getWifiConfiguration(wifiManager, ssid);
+ return cleanPreviousConfiguration(wifiManager, wifiConfiguration);
+ }
+
+ @RequiresApi(api = Build.VERSION_CODES.Q)
+ private static boolean disconnectAndroidQ(@NonNull final ConnectivityManager connectivityManager) {
+ if (networkCallback != null) {
+ connectivityManager.unregisterNetworkCallback(networkCallback);
+ networkCallback = null;
+ }
+
+ return true;
+ }
+
@RequiresPermission(allOf = {ACCESS_FINE_LOCATION, ACCESS_WIFI_STATE})
static boolean cleanPreviousConfiguration(@Nullable final WifiManager wifiManager, @NonNull final ScanResult scanResult) {
if (wifiManager == null) {
diff --git a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiConnectorBuilder.java b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiConnectorBuilder.java
index 962f74d..910cd57 100644
--- a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiConnectorBuilder.java
+++ b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiConnectorBuilder.java
@@ -1,17 +1,19 @@
package com.thanosfisherman.wifiutils;
import android.os.Build;
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
-import androidx.annotation.RequiresApi;
import com.thanosfisherman.wifiutils.wifiConnect.ConnectionScanResultsListener;
import com.thanosfisherman.wifiutils.wifiConnect.ConnectionSuccessListener;
import com.thanosfisherman.wifiutils.wifiDisconnect.DisconnectionSuccessListener;
+import com.thanosfisherman.wifiutils.wifiRemove.RemoveSuccessListener;
import com.thanosfisherman.wifiutils.wifiScan.ScanResultsListener;
import com.thanosfisherman.wifiutils.wifiState.WifiStateListener;
import com.thanosfisherman.wifiutils.wifiWps.ConnectionWpsListener;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.annotation.RequiresApi;
+
public interface WifiConnectorBuilder {
void start();
@@ -31,8 +33,13 @@ interface WifiUtilsBuilder {
@NonNull
WifiSuccessListener connectWith(@NonNull String ssid, @NonNull String bssid, @NonNull String password);
+ @Deprecated
void disconnectFrom(@NonNull String ssid, @NonNull DisconnectionSuccessListener disconnectionSuccessListener);
+ void disconnect(@NonNull DisconnectionSuccessListener disconnectionSuccessListener);
+
+ void remove(@NonNull String ssid, @NonNull RemoveSuccessListener removeSuccessListener);
+
@NonNull
WifiSuccessListener connectWithScanResult(@NonNull String password, @Nullable ConnectionScanResultsListener connectionScanResultsListener);
diff --git a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiUtils.java b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiUtils.java
index 115dd85..704a699 100644
--- a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiUtils.java
+++ b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/WifiUtils.java
@@ -9,10 +9,6 @@
import android.os.Build;
import android.util.Log;
-import androidx.annotation.NonNull;
-import androidx.annotation.Nullable;
-import androidx.annotation.RequiresApi;
-
import com.thanosfisherman.wifiutils.wifiConnect.ConnectionErrorCode;
import com.thanosfisherman.wifiutils.wifiConnect.ConnectionScanResultsListener;
import com.thanosfisherman.wifiutils.wifiConnect.ConnectionSuccessListener;
@@ -20,6 +16,8 @@
import com.thanosfisherman.wifiutils.wifiConnect.WifiConnectionReceiver;
import com.thanosfisherman.wifiutils.wifiDisconnect.DisconnectionErrorCode;
import com.thanosfisherman.wifiutils.wifiDisconnect.DisconnectionSuccessListener;
+import com.thanosfisherman.wifiutils.wifiRemove.RemoveErrorCode;
+import com.thanosfisherman.wifiutils.wifiRemove.RemoveSuccessListener;
import com.thanosfisherman.wifiutils.wifiScan.ScanResultsListener;
import com.thanosfisherman.wifiutils.wifiScan.WifiScanCallback;
import com.thanosfisherman.wifiutils.wifiScan.WifiScanReceiver;
@@ -31,6 +29,10 @@
import java.util.ArrayList;
import java.util.List;
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+import androidx.annotation.RequiresApi;
+
import static com.thanosfisherman.elvis.Elvis.of;
import static com.thanosfisherman.wifiutils.ConnectorUtils.cleanPreviousConfiguration;
import static com.thanosfisherman.wifiutils.ConnectorUtils.connectToWifi;
@@ -41,6 +43,7 @@
import static com.thanosfisherman.wifiutils.ConnectorUtils.matchScanResultSsid;
import static com.thanosfisherman.wifiutils.ConnectorUtils.reenableAllHotspots;
import static com.thanosfisherman.wifiutils.ConnectorUtils.registerReceiver;
+import static com.thanosfisherman.wifiutils.ConnectorUtils.removeWifi;
import static com.thanosfisherman.wifiutils.ConnectorUtils.unregisterReceiver;
@SuppressLint("MissingPermission")
@@ -232,8 +235,14 @@ public WifiConnectorBuilder scanWifi(final ScanResultsListener scanResultsListen
return this;
}
+ @Deprecated
@Override
public void disconnectFrom(@NonNull final String ssid, @NonNull final DisconnectionSuccessListener disconnectionSuccessListener) {
+ this.disconnect(disconnectionSuccessListener);
+ }
+
+ @Override
+ public void disconnect(@NonNull DisconnectionSuccessListener disconnectionSuccessListener) {
if (mConnectivityManager == null) {
disconnectionSuccessListener.failed(DisconnectionErrorCode.COULD_NOT_GET_CONNECTIVITY_MANAGER);
return;
@@ -244,13 +253,34 @@ public void disconnectFrom(@NonNull final String ssid, @NonNull final Disconnect
return;
}
- if (disconnectFromWifi(mContext, mConnectivityManager, mWifiManager, ssid)) {
+ if (disconnectFromWifi(mConnectivityManager, mWifiManager)) {
disconnectionSuccessListener.success();
} else {
disconnectionSuccessListener.failed(DisconnectionErrorCode.COULD_NOT_DISCONNECT);
}
}
+
+
+ @Override
+ public void remove(@NonNull String ssid, @NonNull RemoveSuccessListener removeSuccessListener) {
+ if (mConnectivityManager == null) {
+ removeSuccessListener.failed(RemoveErrorCode.COULD_NOT_GET_CONNECTIVITY_MANAGER);
+ return;
+ }
+
+ if (mWifiManager == null) {
+ removeSuccessListener.failed(RemoveErrorCode.COULD_NOT_GET_WIFI_MANAGER);
+ return;
+ }
+
+ if(removeWifi(mConnectivityManager, mWifiManager, ssid)) {
+ removeSuccessListener.success();
+ } else {
+ removeSuccessListener.failed(RemoveErrorCode.COULD_NOT_REMOVE);
+ }
+ }
+
@NonNull
@Override
public WifiSuccessListener connectWith(@NonNull final String ssid, @NonNull final String password) {
diff --git a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/wifiRemove/RemoveErrorCode.java b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/wifiRemove/RemoveErrorCode.java
new file mode 100644
index 0000000..2ec0315
--- /dev/null
+++ b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/wifiRemove/RemoveErrorCode.java
@@ -0,0 +1,7 @@
+package com.thanosfisherman.wifiutils.wifiRemove;
+
+public enum RemoveErrorCode {
+ COULD_NOT_GET_WIFI_MANAGER,
+ COULD_NOT_GET_CONNECTIVITY_MANAGER,
+ COULD_NOT_REMOVE,
+}
diff --git a/wifiutils/src/main/java/com/thanosfisherman/wifiutils/wifiRemove/RemoveSuccessListener.java b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/wifiRemove/RemoveSuccessListener.java
new file mode 100644
index 0000000..a195dfd
--- /dev/null
+++ b/wifiutils/src/main/java/com/thanosfisherman/wifiutils/wifiRemove/RemoveSuccessListener.java
@@ -0,0 +1,9 @@
+package com.thanosfisherman.wifiutils.wifiRemove;
+
+import androidx.annotation.NonNull;
+
+public interface RemoveSuccessListener {
+ void success();
+
+ void failed(@NonNull RemoveErrorCode errorCode);
+}