I want to get my own device Wi-Fi SSID and BSSID name.
How can I get this?
I tried this
WifiManager wifiManager = (WifiManager)getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiConfiguration wifiConfiguration = new WifiConfiguration();
System.out.println("BSSID"+wifiConfiguration.BSSID);
But this code gives the BSSID of the device to which i currently connected but i wants to get my own device BSSID ssid through code??
Please help Me.
If you want to get the device's hotspot SSID or BSSID, use something like this:
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
Method[] methods = wifimanager.getClass().getDeclaredMethods();
for (Method m: methods) {
if (m.getName().equals("getWifiApConfiguration")) {
WifiConfiguration config = (WifiConfiguration)m.invoke(wifimanager);
String ssid = config.SSID;
String bssid = config.BSSID;
}
}
You can use WifiManager and WifiInfo for the Wifi info the device connected to, like this:
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid = info.getSSID();
String bssid = info.getBSSID();
You would need the following permissions:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
Note: since Android 8.0, you would also need location permissions (ACCESS_COARSE_LOCATION) to access the SSID or BSSID because of this, also, I think you need to have the device's location settings turned on for this to work even if you have the location permissions.
Add these permissions
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
and then use this method
public static String getBSSID(Context mContext) {
ConnectivityManager mConnectivityManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo mNetworkInfo = mConnectivityManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if(mNetworkInfo.isConnected()) {
final WifiManager mWifiManager = (WifiManager) mContext.getSystemService(Context.WIFI_SERVICE);
final WifiInfo mWifiInfo = mWifiManager.getConnectionInfo();
if(mWifiInfo != null) {
return mWifiInfo.getBSSID();
}
}
return null;
}
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'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();
How can I get the ip address of my phone when it is connected under wifi?
I found a method here but it returns something like 24.182.239.255 even if I'm under wifi and I expect something like 192.168.1.10.
I'd like something like:
if (you are under wifi)
String ip4 = getWifiIP()
else
String ip4 = getIPAddress with the method linked before
Many thanks!
So something to consider is that Formatter.formatIpAddress(int) is being deprecated:
This method was deprecated in API level 12.
Use getHostAddress(), which supports both IPv4 and IPv6 addresses. This method does not support IPv6 addresses.
So using formatIpAddress(int) is likely not a good long term solution, although it will work.
Here is a potential solution if you are looking to absolutely on get the IP address for the WiFi interface:
protected String wifiIpAddress(Context context) {
WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE);
int ipAddress = wifiManager.getConnectionInfo().getIpAddress();
// Convert little-endian to big-endianif needed
if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) {
ipAddress = Integer.reverseBytes(ipAddress);
}
byte[] ipByteArray = BigInteger.valueOf(ipAddress).toByteArray();
String ipAddressString;
try {
ipAddressString = InetAddress.getByAddress(ipByteArray).getHostAddress();
} catch (UnknownHostException ex) {
Log.e("WIFIIP", "Unable to get host address.");
ipAddressString = null;
}
return ipAddressString;
}
As stated in previous responses, you need to set the following in your AndroidManifest.xml:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Note that this is only an example solution. You should take time to check for null values and so on to make sure that the UX is smooth.
The irony is that on one hand Google is deprecating formatIpAddress(int), but still has getIpAddress() still returns an integer value. The IP address being an int also rules it out for being IPv6 compliant.
Next is the fact that endianness may or may not be an issue. I have only tested three devices and they have all been little-endian. It seems like endianness can vary depending on the hardware, even though we are running in VMs this can still be an issue. So to be on the safe side I added an endian check in the code.
getByAddress(byte[]) appears to want the integer value to be big endian. From researching this it appears that network byte order is big-endian. Makes sense since an address like 192.168.12.22 is a big-endian number.
Check out HammerNet GitHub project. It implements the code above along with a bunch of sanity checks, ability to handle defaults for AVDs, unit tests, and other things. I had to implement this for an app of mine and decided to open source the library.
If you would like to get the private IP address of your device when connected to Wi-Fi, you can try this.
WifiManager wifiMgr = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipAddress = Formatter.formatIpAddress(ip);
Be sure to add the permission
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
to your manifest.
This will get you the WiFi IPv4, IPv6 or both.
public static Enumeration<InetAddress> getWifiInetAddresses(final Context context) {
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final WifiInfo wifiInfo = wifiManager.getConnectionInfo();
final String macAddress = wifiInfo.getMacAddress();
final String[] macParts = macAddress.split(":");
final byte[] macBytes = new byte[macParts.length];
for (int i = 0; i< macParts.length; i++) {
macBytes[i] = (byte)Integer.parseInt(macParts[i], 16);
}
try {
final Enumeration<NetworkInterface> e = NetworkInterface.getNetworkInterfaces();
while (e.hasMoreElements()) {
final NetworkInterface networkInterface = e.nextElement();
if (Arrays.equals(networkInterface.getHardwareAddress(), macBytes)) {
return networkInterface.getInetAddresses();
}
}
} catch (SocketException e) {
Log.wtf("WIFIIP", "Unable to NetworkInterface.getNetworkInterfaces()");
}
return null;
}
#SuppressWarnings("unchecked")
public static<T extends InetAddress> T getWifiInetAddress(final Context context, final Class<T> inetClass) {
final Enumeration<InetAddress> e = getWifiInetAddresses(context);
while (e.hasMoreElements()) {
final InetAddress inetAddress = e.nextElement();
if (inetAddress.getClass() == inetClass) {
return (T)inetAddress;
}
}
return null;
}
Usage:
final Inet4Address inet4Address = getWifiInetAddress(context, Inet4Address.class);
final Inet6Address inet6Address = getWifiInetAddress(context, Inet6Address.class);
And don't forget:
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
Found this nice answer, https://gist.github.com/stickupkid/1250733
WifiManager wifiManager = (WifiManager) getSystemService(WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
int ipAddress = wifiInfo.getIpAddress();
String ipString = String.format(ā%d.%d.%d.%dā, (ip & 0xff), (ip >> 8 & 0xff), (ip >> 16 & 0xff), (ip >> 24 & 0xff));
Based on my crash logs, it appears not every device returns the WiFi mac address.
Here is a cleaner version of the most popular reply.
final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE);
final ByteBuffer byteBuffer = ByteBuffer.allocate(4);
byteBuffer.order(ByteOrder.LITTLE_ENDIAN);
byteBuffer.putInt(wifiInfo.getIpAddress());
try {
final InetAddress inetAddress = InetAddress.getByAddress(null, byteBuffer.array());
} catch (UnknownHostException e) {
//TODO: Return null?
}
If adb is installed in the terminal then do:
Runtime.getRuntime.exec("adb", "shell", "getprop", "dhcp.wlan0.ipaddress");
Add Following Permission.
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
WifiManager initialize in onCreate.
WifiManager wifiMgr = (WifiManager) getContext().getSystemService(context.WIFI_SERVICE);
Use following function.
public void WI-FI_IP() {
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
int ip = wifiInfo.getIpAddress();
String ipAddress = Formatter.formatIpAddress(ip);
}
The following code is from AOSP Settings. It get the active link's ip, not matter wifi or mobile. It's the most common way.
http://androidxref.com/8.0.0_r4/xref/packages/apps/Settings/src/com/android/settings/deviceinfo/Status.java#251
/**
* Returns the default link's IP addresses, if any, taking into account IPv4 and IPv6 style
* addresses.
* #param context the application context
* #return the formatted and newline-separated IP addresses, or null if none.
*/
public static String getDefaultIpAddresses(ConnectivityManager cm) {
LinkProperties prop = cm.getActiveLinkProperties();
return formatIpAddresses(prop);
}
private static String formatIpAddresses(LinkProperties prop) {
if (prop == null) return null;
Iterator<InetAddress> iter = prop.getAllAddresses().iterator();
// If there are no entries, return null
if (!iter.hasNext()) return null;
// Concatenate all available addresses, comma separated
String addresses = "";
while (iter.hasNext()) {
addresses += iter.next().getHostAddress();
if (iter.hasNext()) addresses += "\n";
}
return addresses;
}
Formatter.formatIpAddress(int) is deprecated:
WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ipAddress = BigInteger.valueOf(wm.getDhcpInfo().netmask).toString();
I want to know Mobile number and Wi-Fi-Address .Howis this possible.Can anyone Help me
try this for Mobile Number
TelephonyManager tm = (TelephonyManager) getSystemService(Context.TELEPHONY_SERVICE);
// get IMEI
String imei = tm.getDeviceId();
String phone = tm.getLine1Number();
but its not always reliable on for example non phone device. You will also need to add permision "android.permission.READ_PHONE_STATE".
for MAC address
WifiManager wfManager;
WifiInfo wifiinfo;
wfManager = (WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiinfo = wfManager.getConnectionInfo();
String MAC = wifiinfo.getMacAddress();
public String getMAC() {
wifimanager= (WifiManager)getSystemService(Context.WIFI_SERVICE);
wifiinfo = wifimanager.getConnectionInfo();
MAC=wifiinfo.getMacAddress();
System.out.println("MAC address info---- "+MAC);
Toast.makeText(getApplicationContext(), "MAC address:"+MAC , Toast.LENGTH_LONG).show();
if(MAC==null){
MAC="1A:DC:5C:8E:15:7B";
}
return MAC;
}