From 6e51c36fdb0c68094c57edb6c3f5547be5a8c74c Mon Sep 17 00:00:00 2001 From: Elias Lecomte Date: Mon, 18 May 2020 21:36:19 +0200 Subject: [PATCH] Feature/disconnect improvement (#66) * Support both disconnecting or removing network config for pre android 10. * After all, this is required to be able to use internet from wifi network. * Only fine location is required. * Add .project files to gitignore. * Split removing and disconnecting in WifiUtils. Update docs & kotlin activity. * Pr review: remove unnecessary variables and make private. --- .gitignore | 3 +- README.md | 38 ++++++++++++- .../wifiutils/sample/MainActivity.java | 2 +- .../wifiutils/sample/MainKotlinActivity.kt | 20 ++++++- sample/src/main/res/layout/activity_main.xml | 43 ++++++++++++-- sample/src/main/res/values/strings.xml | 1 + .../wifiutils/ConnectorUtils.java | 57 ++++++++++++------- .../wifiutils/WifiConnectorBuilder.java | 13 ++++- .../thanosfisherman/wifiutils/WifiUtils.java | 40 +++++++++++-- .../wifiutils/wifiRemove/RemoveErrorCode.java | 7 +++ .../wifiRemove/RemoveSuccessListener.java | 9 +++ 11 files changed, 195 insertions(+), 38 deletions(-) create mode 100644 wifiutils/src/main/java/com/thanosfisherman/wifiutils/wifiRemove/RemoveErrorCode.java create mode 100644 wifiutils/src/main/java/com/thanosfisherman/wifiutils/wifiRemove/RemoveSuccessListener.java 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"> + + + +