Skip to content

Commit

Permalink
Added overloaded method for isWifiConnected
Browse files Browse the repository at this point in the history
  • Loading branch information
ThanosFisherman committed Jul 20, 2020
1 parent 250c638 commit 4ab7c6f
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import com.thanosfisherman.wifiutils.Logger;
import com.thanosfisherman.wifiutils.WifiUtils;
import com.thanosfisherman.wifiutils.wifiConnect.ConnectionErrorCode;
import com.thanosfisherman.wifiutils.wifiConnect.ConnectionSuccessListener;
Expand Down Expand Up @@ -52,7 +51,7 @@ protected void onCreate(Bundle savedInstanceState) {
final Button buttonCheck = findViewById(R.id.button_check);
buttonCheck.setOnClickListener(v -> checkWifi());

WifiUtils.forwardLog((priority, tag, message) -> {
WifiUtils.forwardLog((priority, tag, message) -> {
String customTag = tag + "_" + MainActivity.class.getSimpleName();
Log.i(customTag, message);
});
Expand Down Expand Up @@ -93,7 +92,7 @@ private void enableWifi() {
}

private void checkWifi() {
boolean result = WifiUtils.withContext(getApplicationContext()).isWifiConnected(null);
boolean result = WifiUtils.withContext(getApplicationContext()).isWifiConnected(SSID);
Toast.makeText(MainActivity.this, "Wifi Connect State: " + result, Toast.LENGTH_SHORT).show();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class MainKotlinActivity : AppCompatActivity() {
}

private fun check(context: Context) {
val result = WifiUtils.withContext(context).isWifiConnected(null)
val result = WifiUtils.withContext(context).isWifiConnected(SSID)
Toast.makeText(context, "Wifi Connect State: $result", Toast.LENGTH_SHORT).show()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,8 @@ interface WifiUtilsBuilder {

void cancelAutoConnect();

boolean isWifiConnected(@Nullable String ssid);
boolean isWifiConnected(@NonNull String ssid);
boolean isWifiConnected();
}

interface WifiSuccessListener {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -342,25 +342,31 @@ public void cancelAutoConnect() {
}

@Override
public boolean isWifiConnected(@Nullable String ssid) {
public boolean isWifiConnected(@NonNull String ssid) {
ConnectivityManager connManager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mWifi = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
final NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
boolean result = false;

if (mWifi.getState() == NetworkInfo.State.CONNECTED) {
if (ssid == null) {
result = true;
} else {
final String quotedSsid = SSIDUtils.convertToQuotedString(ssid);
final WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
String tempSSID = wifiInfo.getSSID();
result = tempSSID != null && tempSSID.equals(quotedSsid);
}
if (netInfo.getState() == NetworkInfo.State.CONNECTED) {

final String quotedSsid = SSIDUtils.convertToQuotedString(ssid);
final WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
String tempSSID = wifiInfo.getSSID();
result = tempSSID != null && tempSSID.equals(quotedSsid);
}
return result;
}

@Override
public boolean isWifiConnected() {
ConnectivityManager connManager = (ConnectivityManager) mContext
.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo netInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);

return netInfo.getState() == NetworkInfo.State.CONNECTED;
}

@NonNull
@Override
public WifiSuccessListener setTimeout(final long timeOutMillis) {
Expand Down

0 comments on commit 4ab7c6f

Please sign in to comment.