How check if current network connected is WIFI 5G? - android

I have used this approach to check type of current connected network and works fine, except when is connected to WIFI 5G. How know if WIFI network connected is 5G? i only found how check if device supports WIFI 5G. Thanks in advance.

A possible solution is determine based in WIFI frequency, eg:
private static boolean is24GHzWifi(int frequency) {
return frequency > 2400 && frequency < 2500;
}
private static boolean is5GHzWifi(int frequency) {
return frequency > 4900 && frequency < 5900;
}
// ...
if (info.getType() == ConnectivityManager.TYPE_WIFI) {
WifiManager wifiMgr = (WifiManager) getContext().getSystemService(context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int frequency = wifiInfo.getFrequency();
if (is24GHzWifi(frequency))
return "WIFI 2.4G";
else if (is5GHzWifi(frequency))
return "WIFI 5G";
else
return "WIFI";
}
Reference: Android judge whether wifi is 2.4G or 5G

Related

how to check slow network connection in android

I have to show a page if network connection is slow
iam checking the network by using this code
ConnectivityManager cm = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo info = cm.getActiveNetworkInfo();
if (info.getType() == ConnectivityManager.TYPE_WIFI) {
Toast.makeText(MainActivity.this,"wifi",Toast.LENGTH_LONG).show();
// do something
} else if (info.getType() == ConnectivityManager.TYPE_MOBILE) {
// check NetworkInfo subtype
if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_GPRS) {
Toast.makeText(MainActivity.this,"mobile 100kbps",Toast.LENGTH_LONG).show();
// Bandwidth between 100 kbps and below
} else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EDGE) {
Toast.makeText(MainActivity.this,"mobile 50-100kbps",Toast.LENGTH_LONG).show();
// Bandwidth between 50-100 kbps
} else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EVDO_0) {
Toast.makeText(MainActivity.this,"mobile 400-1000kbps",Toast.LENGTH_LONG).show();
// Bandwidth between 400-1000 kbps
} else if (info.getSubtype() == TelephonyManager.NETWORK_TYPE_EVDO_A) {
Toast.makeText(MainActivity.this,"mobile 600-1400kbps",Toast.LENGTH_LONG).show();
// Bandwidth between 600-1400 kbps
}
it is showing the wifi network
But i need the code for slow wifi network.
Please help me is there any code to check slow wifi network.
Use below method to check the wifi level:
public int getWifiLevel()
{
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
int level = WifiManager.calculateSignalLevel(linkSpeed, 5);
return level;
}
Based on wifi level or link speed you can decide if it has the low connection or high connection internet.
You can use following code for checking wifi speed
WifiManager wifiManager = Context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
if (wifiInfo != null) {
Integer linkSpeed = wifiInfo.getLinkSpeed(); //measured using WifiInfo.LINK_SPEED_UNITS
}
I hope this may helps you.

How to log the data using WIFI connection in android log entries SDK

I want to know is there any way to determine whatever I am logging should go through WIFI instead of device network,Is there any methods as part of Log entries SDK
Try this code for wifi connection or not.
private boolean checkConnectedToDesiredWifi() {
boolean connected = false;
WifiManager wifiManager =(WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInf = wifiManager.getConnectionInfo();
String desiredMacAddress = wifiInf.getMacAddress();
WifiInfo wifi = wifiManager.getConnectionInfo();
if (wifi != null) {
// get current router Mac address
String bssid = wifi.getBSSID();
connected = desiredMacAddress.equals(bssid);
}
return connected;
}

Android: Why WifiManager.getConnectionInfo().getSSID() return disconnected SSID

I am developing an Android App. The app will keep trying to connect to an dedicated WiFi AP when started. This is working. However when I tested to power off the AP, so the app must have been disconnected from the AP, my app still return the SSID of the AP when I use WifiManager.getConnectionInfo().getSSID() to check the SSID, why? And how can I update the current SSID when it is not connected, even it return "unknown ssid"?
Below is the method I check the SSID, the app will invoke this method continuously:
WifiManager wifiManager; // <- In MainAativity
// ....
int checkWiFiSSID() {
wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (!wifiManager.isWifiEnabled()) {
Log.d(TAG, "WiFi is disabled, enable it now");
wifiManager.setWifiEnabled(true);
} else {
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
String ssid = wifiInfo.getSSID();
// This line always show the AP SSID even the AP is powered down, why??
Log.d(TAG, "AP SSID: " + ssid);
}
}
Thank you.
UPDATE:
Having read the document carefully, I found that if the WifiManager is trying to connect to an AP, even it is not connected, the getSSID() will return its SSID. As my app is keep trying to connect to the AP once it is disconnected. I guess this is the reason, but I am not sure.

WifiManager returns BSSID 00:00:00:00:00:00

I use the following code to get BSSID:
public static String getBSSID(Context context) {
WifiManager wifiMgr = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
return wifiMgr.getConnectionInfo().getBSSID();
}
When i use this code when the device doesn't have sim card it works fine.
But when i have a sim card, even when i am using wifi - me returned value is 00:00:00:00:00:00.
Anybody knows why it happens?
had same problem myself. most of the chances is that you are not checking if you are currently connected to the WiFi before attempting to get the bssid:
public static boolean isConnectedToRouter(Context context) {
ConnectivityManager onnectivityManager = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo wifi = onnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (wifi.isConnected()) {
return true;
}
return false;
}
only if this method returns true, then you can obtain bssid. attemping to get BSSID when you are not connected to specific Wifi will return 00:00:00:00:00:00

Problem: Android's isConnected() used to get current state of WiFi often returns false even when the device is connected

My Android app can only function with WiFi connected to the Internet. Thus, I use the following code to check if the device is connected:
ConnectivityManager conMgr = (ConnectivityManager)getSystemService(Activity.CONNECTIVITY_SERVICE);
boolean wifi = conMgr.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnected();
However, very often when the application is launched and WiFi connected to the Internet, I get the notification that is only shown when wifi = false. Have I missed something, or the check is not that accurate?
My project also relies on Wifi (although I use a private network). The following is my code for setting up a Wifi connection on start up:
private void initWIFI (WifiManager wifiMgr, String SSID, String key)
{
WifiInfo curr;
if (null == (curr = wifiMgr.getConnectionInfo())) // Get current wifi state
{
joinNetwork (wifiMgr, SSID, key);
}
else switch (curr.getSupplicantState())
{
case DISCONNECTED:
case DORMANT:
case INACTIVE:
case SCANNING:
joinNetwork (wifiMgr, SSID, key);
break;
default:
if (!curr.getSSID().equals (SSID))
joinNetwork (wifiMgr, SSID, key);
}
while (wifiMgr.getConnectionInfo().getIpAddress() == 0)
{
try
{
Thread.sleep (1000);
}
catch (Exception e)
{ }
}
}
/**This method is used to join the proper WiFi network when necessary. Normally,
* the Android retains network configuration and it is not necessary to manually
* re-join the desired network on software startup. However, when it is determined
* that the Android is not currently attached to the proper network, this function
* is used to correct that situation. */
private void joinNetwork (WifiManager wifiMgr, String SSID, String key)
{
try
{
WifiConfiguration wc = new WifiConfiguration();
wc.allowedAuthAlgorithms.set (WifiConfiguration.AuthAlgorithm.OPEN);
wc.allowedAuthAlgorithms.set (WifiConfiguration.AuthAlgorithm.SHARED);
wc.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.WEP40);
wc.allowedGroupCiphers.set (WifiConfiguration.GroupCipher.WEP104);
wc.allowedKeyManagement.set (WifiConfiguration.KeyMgmt.NONE);
wc.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.TKIP);
wc.allowedPairwiseCiphers.set (WifiConfiguration.PairwiseCipher.CCMP);
wc.allowedProtocols.set (WifiConfiguration.Protocol.WPA);
wc.allowedProtocols.set (WifiConfiguration.Protocol.RSN);
wc.hiddenSSID = false;
wc.priority = 32;
wc.SSID = "\"" + SSID + "\"";
wc.status = WifiConfiguration.Status.ENABLED;
wc.wepKeys[0] = key;
wc.wepTxKeyIndex = 0;
int netID;
if (-1 == (netID = wifiMgr.addNetwork (wc)))
{
listener.lostConnection (true);
}
else
{
wifiMgr.enableNetwork (netID, true);
Thread.sleep (5000); // Delay to allow the DHCP process to work
}
}
catch (Exception e)
{
listener.lostConnection (true);
}
}
It should be pointed out that I always use the same wireless access point, and the code in joinNetwork() is specifically configured for it, so if your configuration needs to be more flexible, then your solution may be more complex. Sadly, I do not remember the web site where I found the starting point for this code, but it didn't take a ton of Googling to find it. Finally, I'm pretty sure your application needs to have the ACCESS_WIFI_STATE and CHANGE_WIFI_STATE permissions.
I use code like this:
public static String getCurrentSsid(Context context) {
final WifiInfo wifiInfo = getCurrentWifiInfo(context);
if (wifiInfo != null && !StringUtil.isBlank(wifiInfo.getSSID())) {
return wifiInfo.getSSID();
}
return null;
}
public static WifiInfo getCurrentWifiInfo(Context context) {
final ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
final NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo != null && networkInfo.isConnected()) {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
return wifiManager.getConnectionInfo();
}
return null;
}
At the same time be aware of this two issues 19078 and 3641.

Categories

Resources