I'm trying to get the Wi-Fi signal strength of the AP connected. When I try to run the below code, nothing is returned/displays error.
public class Wifi extends uiscenario {
public void onReceive(WifiManager wifiManager) throws UiObjectNotFoundException, InterruptedException, RemoteException, IOException {
System.out.println("level");
int numberOfLevels=4;
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int level=WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
System.out.println("Bars =" +level);
}
}
How to get the signal strength of the wifi connected and also how to get the scan results of wifi and connect to the wifi which has strongest signal strength?
Updated code
public class Wifi extends UiScenario {
private static WifiManager wifiManager;
public void wifiscan() throws UiObjectNotFoundException,InterruptedException,RemoteException, IOException {
int numberOfLevels=5;
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int level=WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
System.out.println("Bars =" +level);
}
}
Getting error in this line(WifiInfo wifiInfo = wifiManager.getConnectionInfo();)
Hope it should work. Its working fine for me.
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
WifiManager wifiManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
System.out.println("Link Speed is======"+linkSpeed);
Check your manifest uses permissions for wifi and read phone state your code is perfect,
it should work else try this.
WifiManager wifiManager = (WifiManager)context.getSystemService(Context.WIFI_SERVICE);
int linkSpeed = wifiManager.getConnectionInfo().getRssi();
Related
Any one help me out to fetch wifi Signal strength ?
You can get the signal in levels by using:
WifiManager wifi = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
int levels = 5;
WifiInfo wifiInfo = wifi.getConnectionInfo();
int level = WifiManager.calculateSignalLevel(wifiInfo.getRssi(), numberOfLevels);
Refer to this answer for further help.
The problem:
When I connect to wifi, and then disconnect, the following code gives me SupplicantState.COMPLETED
SupplicantState supState;
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
supState = wifiInfo.getSupplicantState();
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;
}
I create an WifiNetwork class that holds information about Wifis networks.
I have the method:
public static WifiNetwork fromScanResult(ScanResult scanResult) {
String capabilities = scanResult.capabilities;
String ssid = scanResult.SSID;
String bssid = scanResult.BSSID;
int frequency = scanResult.frequency;
int level = scanResult.level;
long timestamp = 0L;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR2) {
timestamp = scanResult.timestamp;
}
return new WifiNetwork(capabilities, ssid, bssid, frequency, level, timestamp);
}
That converts an ScanResult into my object.
I need another method populate my object from a WifiInfo:
public static WifiNetwork fromWifiInfo (WifiInfo wifiInfo){
....
}
The WifiInfo object is obtained by:
WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
wifiManager.getConnectionInfo()
From ScanResult, the security mode (if the network is WEP, WAP, etc) is getted by
scanResult.capabilities();
But I cannot see an equivalent in WifiInfo.
Is there another way to get the security mode from the active Wifi Connection?
I need to determine if the Android device is connected to Wifi, and if so, obtain its Wifi IP address.
I know how to use ConnectivityManager to determine whether the active network is a Wifi network, and I know how to use java.net.NetworkInterface to iterate over the available network interfaces and get their IP addresses.
What I don't know how to do is determine which IP address belongs to the Wifi network, if there is more than one address found. Any advice?
Thanks.
public String getIpAddr() {
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipString = String.format(
"%d.%d.%d.%d",
(ip & 0xff),
(ip >> 8 & 0xff),
(ip >> 16 & 0xff),
(ip >> 24 & 0xff));
return ipString;
}
Please Note: You need to add android.permission.INTERNET and android.permission.ACCESS_WIFI_STATE in your AndroidManifest.xml as <user-permission/> to access the code.
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Please try this code.
ConnectivityManager connec = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
//To fetch the state of the Wi-Fi network in the device
Boolean isWifi = connec.getNetworkInfo(ConnectivityManager.TYPE_WIFI).isConnectedOrConnecting();
WifiManager wifiMgr = (WifiManager) getBaseContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
//To fetch the name of the Wi-Fi network to which the device is connected
String wifiName = wifiInfo.getSSID();
static final int IP_ADDRESS_LENGTH = 32;
public static Integer getSystemWifiIpAddress(Context context)
{
WifiManager wManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
WifiInfo wInfo = wManager.getConnectionInfo();
int ipAddress = wInfo.getIpAddress();
if (ipAddress == 0)
return null;
return ipAddress;
}