Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/disconnect improvement #66

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@
.DS_Store
/build
/captures
.externalNativeBuild
.externalNativeBuild
.project
38 changes: 37 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);`

Expand All @@ -159,7 +196,6 @@ Damn You are required to set a few permissions in order for this lib to work cor
```xml
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE"/>
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION"/> <!-- for Android 6 and above -->
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/> <!-- for Android 6 and above -->
```
### Add it to your project
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -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) {
Expand All @@ -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()
}
Expand All @@ -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()
}
})
}
}
43 changes: 37 additions & 6 deletions sample/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,26 +7,57 @@
android:layout_height="match_parent"
tools:context="com.thanosfisherman.wifiutils.sample.MainActivity">

<TextView
android:id="@+id/textview_ssid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/textview_password"
tools:text="SSID"
/>

<TextView
android:id="@+id/textview_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="@id/textview_ssid"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toTopOf="@id/button_connect"
tools:text="password"
/>

<Button
android:id="@+id/button_connect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="@string/connect"
app:layout_constraintBottom_toBottomOf="@id/button_disconnect"
app:layout_constraintBottom_toTopOf="@id/button_disconnect"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
app:layout_constraintTop_toTopOf="@id/textview_password" />

<Button
android:id="@+id/button_disconnect"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:text="@string/disconnect"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintBottom_toTopOf="@id/button_remove"
app:layout_constraintTop_toBottomOf="@+id/button_connect"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"/>
app:layout_constraintRight_toRightOf="parent" />

<Button
android:id="@+id/button_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/remove"
app:layout_constraintTop_toBottomOf="@id/button_disconnect"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintBottom_toBottomOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>
1 change: 1 addition & 0 deletions sample/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<string name="app_name">WifiUtils</string>
<string name="connect">Connect</string>
<string name="disconnect">Disconnect</string>
<string name="remove">Remove</string>
</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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();

Expand All @@ -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);

Expand Down
Loading