Android Wifi Signal Strength - android

I want to get the list of available routers, their SSID's and signal strengths, by doing a wifi scan.
Can you please help me out with the code?
P.S.: I am new to android. A related Example will be very much appreciated.

Below code is used to get SSID, BSSID, IP Address & RSSI...
WifiManager wifi = = (WifiManager) uMobility.getContext().getSystemService(Context.WIFI_SERVICE);
String ssid = wifi.getConnectionInfo().getSSID(); // for geting SSID
String bssid = wifi.getConnectionInfo().getBSSID(); // for geting BSSID
int ipAddress = wifi.getConnectionInfo().getIpAddress(); // for geting IP Address
int rssi = wifi.getConnectionInfo().getRssi(); // for geting RSSI

Related

how to get BSSID of Hotspot

I need to get bssid of my hotspot using android.
WifiManager wifimanager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Method[] methods = wifimanager.getClass().getDeclaredMethods();
for (Method m : methods) {
if (m.getName().equals("getWifiApConfiguration")) {
WifiConfiguration config = null;
config = (WifiConfiguration) m.invoke(wifimanager);
String ssid = config.SSID;
String bssid = config.BSSID;
System.out.println(config);
}
}
i tryed using above code. but it give bssid null value
Wifi.Configuration.BSSID does not return the BSSID of the AP on your device but is used to further narrow down the network selection when connecting to a Wi-Fi in station mode.
If set, the device will only connect to a network with exactly that BSSID.
For details, see:
https://developer.android.com/reference/android/net/wifi/WifiConfiguration.html#BSSID

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.

Android: detect whether wifi access point still in range

I have a method to detect available wifi access ppoints. My method works well but when I am not in the range I still getiing the SSID of the last scan results displayed in my xml file though I am out of the range of this SSID.
private void check_wifi_available() {
WifiManager wifi = (WifiManager) getSystemService(Context.WIFI_SERVICE);
if (!wifi.isWifiEnabled()) {
Toast.makeText(this, "Please turn your Wi-Fi on",
Toast.LENGTH_SHORT).show();
}
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
final List<ScanResult> results = wifiManager.getScanResults();
if (results != null) {
// list of access points from the last scan
List<ScanResult> updatedResults = new ArrayList<ScanResult>();
// pick Wi-Fi access points which begins with these "SV-"
// characters.
for (int i = 0; i < results.size(); i++) {
String ssid = results.get(i).SSID;
// Pattern p = Pattern.compile("^KD-(4[0-9]{2}|500)$");
// Matcher m = p.matcher(ssid);
// if(m.matches()){}else{}
if (ssid.startsWith("KD")) {
updatedResults.add(results.get(i));
}
}
if (updatedResults.size() > 0) {
String a = deliverBestAccessPoint(updatedResults);
textWifi.setText(a.toString());
}
}
}
This is best handled by the operating system. The best you could do is set up a timer to periodically scan for WiFi devices and update the results.
Other than that, on rooted devices you may be able to manually send 802.11 requests to the access point/router and do a timeout check for replies.
To clarify: the operating system, when it is scanning for devices, sends out a broadcast message and reports what devices it hears back from. When devices are toward the edge of the 'range' they may report as being available even if connecting and maintaining a connection is problematic because the signal is not strong enough.
EDIT:
For what it's worth, ScanResult has a "level" member variable that reports the signal strength. You could do some more fine filtering for low-strength results. http://developer.android.com/reference/android/net/wifi/ScanResult.html

how to obtain the ip address of the connected wifi router in android programmatically?

I want to obtain the ip address of the the wifi router to which my android phone is connected? I know that we can get the mac/BSSId and SSID by using the android APIS but I don't find the way to find the way to find the ip address of it?
I found the code for obtaining the ip address of phone owns wifi router
WifiManager myWifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo myWifiInfo = myWifiManager.getConnectionInfo();
int ipAddress = myWifiInfo.getIpAddress();
System.out.println("WiFi address is " + android.text.format.Formatter.formatIpAddress(ipAddress))
but failed to get what I want
What you likely want is DhcpInfo:
final WifiManager manager = (WifiManager) super.getSystemService(WIFI_SERVICE);
final DhcpInfo dhcp = manager.getDhcpInfo();
final String address = Formatter.formatIpAddress(dhcp.gateway);
This will yield the (formatted) gateway IP address, which should be what you're looking for.
Since formatIpAddress is Deprecatted, here is the alternative :
public String getHotspotAdress(){
final WifiManager manager = (WifiManager)super.getSystemService(WIFI_SERVICE);
final DhcpInfo dhcp = manager.getDhcpInfo();
int ipAddress = dhcp.gateway;
ipAddress = (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) ?
Integer.reverseBytes(ipAddress) : ipAddress;
byte[] ipAddressByte = BigInteger.valueOf(ipAddress).toByteArray();
try {
InetAddress myAddr = InetAddress.getByAddress(ipAddressByte);
return myAddr.getHostAddress();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
Log.e("Wifi Class", "Error getting Hotspot IP address ", e);
}
return "null"
}

Calculating Internet Speed in android

I am working with an App which contains web service things.
In that I need to know the status when the Internet speed is low. How to find the internet speed level in Android?
For example, Consider if I am using 2Mbps connection in my cell phone and when it slows to 50Kbps I need to notice that situation by making a Toast or Alert.
Thanks.
If you are connected to WiFi you can find the speed of the connection using WifiManager :
WifiInfo wifiInfo = wifiManger.getConnectionInfo();
and then from the WifiInfo you can get the current speed :
int speedMbps = wifiInfo.getLinkSpeed();
If you are on 3G, I don't think there is a standard way of finding out, maybe you can assume automatically that 3G is slow.
This is specilally to detect internet connection speed by
facebook sdk
ConnectionQuality cq = ConnectionClassManager.getInstance().getCurrentBandwidthQuality();
This is the code for getting speed of your internet while connected to wifi.
WifiManager wifiManager = (WifiManager)
this.getSystemService(Context.WIFI_SERVICE);
List<ScanResult> wifiList = wifiManager.getScanResults();
for (ScanResult scanResult : wifiList) {
int level = WifiManager.calculateSignalLevel(scanResult.level, 5);
String net=String.valueOf(level);
// Toast.makeText(MainActivity.this,net,Toast.LENGTH_LONG).show();
}
// Level of current connection.here rssi is the value of internet speed whose value
// can be -50,-60 and some others,you can find the speed values easily on internet.
int rssi = wifiManager.getConnectionInfo().getRssi();
int level = WifiManager.calculateSignalLevel(rssi, 5);
String net=String.valueOf(rssi);
Toast.makeText(MainActivity.this,net,Toast.LENGTH_LONG).show();
// -100 is the minimum speed value of your internet.
if(rssi < -100) {
slowInternet=false;
}

Categories

Resources