Android get wifi name

How to Get Name of Wifi-Network Out of Android Using Android API

How to get name of wifi-network out of android using android API?

WifiManager wifiMgr = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiMgr.getConnectionInfo();
String name = wifiInfo.getSSID();

How to get current wifi connection name in android pie(9) devices?

This is related to permissions. since API level 27 you need either ACCESS_FINE_LOCATION or ACCESS_COARSE_LOCATION permission. You may also need CHANGE_WIFI_STATE for Android 9 (that’s the case for wifi scan anyway as per google permisson model

 ConnectivityManager connManager = (ConnectivityManager) activity.getSystemService(Context.CONNECTIVITY_SERVICE); 
NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI);
if (networkInfo.isConnected()) WifiManager wifiManager = (WifiManager) activity.getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
wifiInfo.getSSID();
String name = networkInfo.getExtraInfo();
String ssid = "\"" + wifiInfo.getSSID() + "\"";
>

Get SSID when WIFI is connected

I listen for WifiManager.NETWORK_STATE_CHANGED_ACTION in a broadcast receiver

if (WifiManager.NETWORK_STATE_CHANGED_ACTION.equals(action)) NetworkInfo netInfo = intent.getParcelableExtra (WifiManager.EXTRA_NETWORK_INFO); 
if (ConnectivityManager.TYPE_WIFI == netInfo.getType ()) .
>
>

I check for netInfo.isConnected() . Then I am able to use

WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(Context.WIFI_SERVICE);
WifiInfo info = wifiManager.getConnectionInfo();
String ssid = info.getSSID();

From android 8.0 onwards we wont be getting SSID of the connected network unless location services are enabled and your app has the permission to access it.

How to get the name of the connected network in android?

android.net.wifi.WifiInfo.getSSID

How to get name of wifi-network out of android using android API?

How to get the name of currently connected WiFi network in Android with BroadcastReceiver

Get Wifi Interface name on Android

for(Enumeration list = NetworkInterface.getNetworkInterfaces(); list.hasMoreElements();) 
NetworkInterface i = list.nextElement();
Log.e("network_interfaces", "display name " + i.getDisplayName());
>

Источник

wifi network interface name

I am developing an android app in which phone connects to a wifi network or acts as a wifi access point. I need to get the correct interface name.When phone connects to a wifi network on a mac, interface name is ‘eth0’ and when it acts as an access point it is ‘wl0.1’. I find this using NetworkInterface.getNetworkInterfaces() which lists all interfaces. I want to get active wifi interface name only.Any help on this?

3 Answers 3

NetworkInterface has getHardwareAddress for returning the hardware (MAC) address of the interface. WifiInfo , representing the current state of Wi-Fi (obtained by WifiManager.getConnectionInfo ) has getMacAddress method. Just use the latter to find your Wi-Fi interace.

Читайте также:  Подключить алтел вай фай

Remember that when comparing the addresses, you should convert them to common format as getHardwareAddress returns array of bytes while getMacAddress returns string with colon-separated hex values.

As far is I know, there are two ways to obtain the interface name of the Wi-Fi adaptor on an Android device, using Java. Via either the Wi-Fi interface’s:

Once you have either piece of information, you can use NetworkInterface to figure out the rest.

Option #1, via MAC address, is likely the best option. MAC addresses are are always available, even when a device is not on a network. It might actually be desirable to some people to get the interface name when the device is not connected to a network. MAC addresses never change, and if so rarely. So doing it through the Wi-Fi adaptor’s MAC address promises to be more robust.

Here is an example on how to get the Wi-Fi interface name through its MAC address. It is an expansion on Xion‘s original answer.

protected String wifiInterfaceName(final Context context) < // Get WiFi interface's MAC address as a BigInteger. WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); String wifiMacString = wifiInfo.getMacAddress(); byte[] wifiMacBytes = macAddressToByteArray(wifiMacString); BigInteger wifiMac = new BigInteger(wifiMacBytes); String result = null; try < ListnetworkInterfaces = Collections.list(NetworkInterface.getNetworkInterfaces()); for (NetworkInterface currentInterface : networkInterfaces) < byte[] hardwareAddress = currentInterface.getHardwareAddress(); if (hardwareAddress != null) < BigInteger currentMac = new BigInteger(hardwareAddress); if (currentMac.equals(wifiMac)) < result = currentInterface.getName(); break; >> > > catch (SocketException ex) < Log.e("WifiGet", "Socket excpetion: " + ex.getMessage()); >return result; > protected byte[] macAddressToByteArray(String macString) < String[] mac = macString.split("[:\\s-]"); byte[] macAddress = new byte[6]; for (int i = 0; i < mac.length; i++) < macAddress[i] = Integer.decode("0x" + mac[i]).byteValue(); >return macAddress; > 

Option #2, via IP address, is pretty well explained in the question section of: Get Wifi Interface name on Android. There are a few things to be aware of when trying to get the Wi-Fi interface name by IP address:

  • It only works when the device is connected to a Wi-Fi network.
  • IP addresses change so there is a very small chance your operation will fail if you catch the device changing IP address.
  • This one I am still trying to figure out. But there might be issues with the endianness of various devices and the result from WifiInfo getIpAddress(). Again, I’m not sure. All I know for certain is that I have to reverse byte order in order for me to get the actual IP address.

The following example is based on the question part of Get Wifi Interface name on Android

protected String wifiInterfaceName2(final Context context) < WifiManager wifiManager = (WifiManager) context.getSystemService(WIFI_SERVICE); WifiInfo wifiInfo = wifiManager.getConnectionInfo(); int ipAddress = wifiInfo.getIpAddress(); // Convert little-endian to big-endianif needed if (ByteOrder.nativeOrder().equals(ByteOrder.LITTLE_ENDIAN)) < ipAddress = Integer.reverseBytes(ipAddress); >byte[] bytes = BigInteger.valueOf(ipAddress).toByteArray(); String result; try < InetAddress addr = InetAddress.getByAddress(bytes); NetworkInterface netInterface = NetworkInterface.getByInetAddress(addr); result = netInterface.getName(); >catch (UnknownHostException ex) < LOG.error("Unknown host.", ex); result = null; >catch (SocketException ex) < LOG.error("Socket exception.", ex); result = null; >return result; > 

Please keep in mind that both code segments have been provided as examples only. Make sure to clean up the code, add error handling, and test if you plan on using either in production.

Читайте также:  Проводной ip ahd домофон wi fi eplutus ep 6814lg

Oh, and don’t forget to add the proper permissions to your AndroidManifest.xml as well. I think they are something along the lines of:

Also, these examples will only work on an actual device. No Wi-Fi on the emulators.

Источник

Get current Wifi SSID Android 10

Since API 26 (Android 8 Oreo) you need to obtain the user location permission in order to get the wifi name (SSID), which is why also in Android 9 (API 28), Android 10 (API 29) or Android 11 (API 30) and newer you may get as the SSID returned or 02:00:00:00:00:00 .

For that, in AndroidManifest.xml:

I will provide some demo code written in Kotlin.

For declaring your permission request success code, we store it in a companion object (Kotlin way for storing constants) inside the class we are testing from (MainActivity in this case) or you may define a class explicitly for constants, which is actually a common practice.

class MainActivity : AppCompatActivity() < . companion object < const val PERMISSION_CODE_ACCEPTED = 1 const val PERMISSION_CODE_NOT_AVAILABLE = 0 >. > 
when(requestLocationPermission()) < MainActivity.PERMISSION_CODE_ACCEPTED ->getWifiSSID() > 

For checking an requesting the permission:

fun requestLocationPermission(): Int < if (ContextCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) < if (ActivityCompat.shouldShowRequestPermissionRationale(this, android.Manifest.permission.ACCESS_FINE_LOCATION)) < >else < // request permission ActivityCompat.requestPermissions(this, arrayOf(android.Manifest.permission.ACCESS_FINE_LOCATION), MainActivity.PERMISSION_CODE_ACCEPTED) >> else < // already granted return MainActivity.PERMISSION_CODE_ACCEPTED >// not available return MainActivity.PERMISSION_CODE_NOT_AVAILABLE > 

For actually getting the SSID (wifi name):

Tested on the emulator on API 29 (Android 10).

2020-10-04 15:35:28.625 13013-13013/com.example.myapplication D/wifi name: "AndroidWifi" 

Источник

How to fetch the connected Wifi name? [duplicate]

I have used the below code for fetching the Wifi name in android. Below is the code that i have used:

WifiManager wifiManager = (WifiManager) mContext.getApplicationContext().getSystemService(Context.WIFI_SERVICE); WifiInfo info; if (wifiManager != null && isWebViewLoaded)

The code works on other devices (below version 8), however I tried on Nexus 5X (Oreo v-8.1), it gives «Unknown SSID» against info.getSSID().

I don’t believe this is really a duplicate nor do those other solutions linked above and answered below address this. It appears to be an Android 8 or 8.1 issue. My Pixel 2 running 8.1 is giving me . However, this only happens when I target API 26, API 25 or lower seemed to work fine.

Читайте также:  Кабель от вай фай до телевизора

Alright, I figured this one out. It appears that in Android 8.1 (maybe 8.0 as well?) if you’re targeting API 26+ getSSID() will always return UNLESS your app has the following permission: ACCESS_COARSE_LOCATION. There is a bug logged for this behavior: issuetracker.google.com/issues/70795529 . I can’t add an answer to this question because it was erroneously marked as a duplicate. It clearly is not.

Thanks. I don’t think it’s a bug though. They do the same for bluetooth scanning. It’s a horrible way to do it and confuses the user, but it’s meant as additional security

As @Glaucus said, from Android 8 you have to explicitly request those location permissions via ActivityCompat.requestPermissions

Источник

How to get name of wifi-network out of android using android API?

I thought that I should use NetworkInterface::getDisplayName. I got some name, but this name is different that this name which I can see, when I choosing to which network I want to connect.

4 Answers 4

WifiManager wifiMgr = (WifiManager) getActivity().getSystemService(Context.WIFI_SERVICE); WifiInfo wifiInfo = wifiMgr.getConnectionInfo(); String name = wifiInfo.getSSID(); 
public String getWifiName(Context context) < WifiManager manager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); if (manager.isWifiEnabled()) < WifiInfo wifiInfo = manager.getConnectionInfo(); if (wifiInfo != null) < DetailedState state = WifiInfo.getDetailedStateOf(wifiInfo.getSupplicantState()); if (state == DetailedState.CONNECTED || state == DetailedState.OBTAINING_IPADDR) < return wifiInfo.getSSID(); >> > return null; > 

This (mix and match of various answers from Marakana and others) will simultaneously get everything you want to extract from:

  1. all wifi routers in range
  2. connected wifi router
  3. all stored wifi networks (on your device)
public String getCurrentSsid(Context context) < String ssid = null; ConnectivityManager connManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE); NetworkInfo networkInfo = connManager.getNetworkInfo(ConnectivityManager.TYPE_WIFI); if (networkInfo.isConnected()) < final WifiManager wifiManager = (WifiManager) context.getSystemService(Context.WIFI_SERVICE); final WifiInfo connectionInfo = wifiManager.getConnectionInfo(); if (connectionInfo != null && !(connectionInfo.getSSID().equals(""))) < //if (connectionInfo != null && !StringUtil.isBlank(connectionInfo.getSSID())) < ssid = connectionInfo.getSSID(); >// Get WiFi status MARAKANA WifiInfo info = wifiManager.getConnectionInfo(); String textStatus = ""; textStatus += "\n\nWiFi Status: " + info.toString(); String BSSID = info.getBSSID(); String MAC = info.getMacAddress(); List results = wifiManager.getScanResults(); ScanResult bestSignal = null; int count = 1; String etWifiList = ""; for (ScanResult result : results) < etWifiList += count++ + ". " + result.SSID + " : " + result.level + "\n" + result.BSSID + "\n" + result.capabilities +"\n" + "\n=======================\n"; >Log.v(TAG, "from SO: \n"+etWifiList); // List stored networks List configs = wifiManager.getConfiguredNetworks(); for (WifiConfiguration config : configs) < textStatus+= "\n\n" + config.toString(); >Log.v(TAG,"from marakana: \n"+textStatus); > return ssid; > 

DISCLAIMER: while this is working code, not pseudo code, its only purpose is to illustrate the methods for data extraction from wifi connections and it should be adapted (and cleaned) before use.

Источник

Оцените статью
Adblock
detector