12

When an Android device connects to a wifi AP, it identifies itself with a name like:

android_cc1dec12345e6054

How can that string be obtained from within an Android app? Not for the purpose of changing it, just for readout.

EDIT:
This is a screenshot of my router's web interface, showing a list of all connected devices. Note the two Android devices on the list -- How can that string be read from Java code running on the device?

Router's connection list

1
  • Not trying to get the router's SSID -- Trying to get the android device's hostname string, which it somehow provides to the router. Commented Feb 20, 2014 at 5:03

7 Answers 7

14

Building off of @Merlevede's answer, here's a quick and dirty way to get the property. It's a private API, so it's subject to change, but this code hasn't been modified since at least Android 1.5 so it's probably safe to use.

import android.os.Build;
import java.lang.reflect.Method;

/**
 * Retrieves the net.hostname system property
 * @param defValue the value to be returned if the hostname could
 * not be resolved
 */
public static String getHostName(String defValue) {
    try {
        Method getString = Build.class.getDeclaredMethod("getString", String.class);
        getString.setAccessible(true);
        return getString.invoke(null, "net.hostname").toString();
    } catch (Exception ex) {
        return defValue;
    }
}
Sign up to request clarification or add additional context in comments.

8 Comments

You weren't kidding about that "Private API"... java.lang.IllegalAccessException: access to method denied
Interesting. What OS version and device are you running? This works just fine for me.
Strange. I'm on a Nexus 5 running 4.4.2, so it shouldn't be any different. Can you try with the edited change? (setAccessible(true))
Just for other Android newbie's - the class Method is java.lang.reflect.Method and the class Build is android.os.Build.
This will not work in Android O - Querying the net.hostname system property produces a null result. - Ref: developer.android.com/preview/behavior-changes.html
|
6

I don't know if this helps but here I go.

From a unix shell (you can download any Terminal app in Google Play), you can get the hostname by typing

getprop net.hostname

Of course this is not what you want... but... on the other hand, here is information on how to execute a unix command from java. Maybe by combining these two you get what you're looking for.

2 Comments

Thanks Merlevede, that helped. Further investigation shows that "net.hostname" is a deep Java SystemProperty, not exposed in the Android SDK. It can be made available through reflection, though: stackoverflow.com/questions/2641111/…
See my answer for the code
6

Use the NetworkInterface object to enumerate the interfaces and get the canonical host name from the interfaces' InetAddress. Since you want the wifi name, as a shortcut you can query for wlan0 directly and if that fails you can enumerate them all like this:

import android.test.InstrumentationTestCase;
import android.util.Log;

import java.net.InetAddress;
import java.net.NetworkInterface;
import java.util.Enumeration;

public class NetworkInterfaceTest extends InstrumentationTestCase {
    private static final String TAG = NetworkInterfaceTest.class.getSimpleName();
    public void testNetworkName() throws Exception {
        Enumeration<NetworkInterface> it_ni = NetworkInterface.getNetworkInterfaces();
        while (it_ni.hasMoreElements()) {
            NetworkInterface ni = it_ni.nextElement();
            Enumeration<InetAddress> it_ia = ni.getInetAddresses();
            if (it_ia.hasMoreElements()) {
                Log.i(TAG, "++ NI:   " + ni.getDisplayName());
                while (it_ia.hasMoreElements()) {
                    InetAddress ia = it_ia.nextElement();
                    Log.i(TAG, "-- IA:   " + ia.getCanonicalHostName());
                    Log.i(TAG, "-- host: " + ia.getHostAddress());
                }
            }
        }
    }
}

That will give you an output like this:

TestRunner﹕ started: testNetworkName
++ NI:   lo
-- IA:   ::1%1
-- host: ::1%1
-- IA:   localhost
-- host: 127.0.0.1
++ NI:   p2p0
-- IA:   fe80::1234:1234:1234:1234%p2p0
-- host: fe80::1234:1234:1234:1234%p2p0
++ NI:   wlan0
-- IA:   fe80::1234:1234:1234:1234%wlan0
-- host: fe80::1234:1234:1234:1234%wlan0
-- IA:   android-1234567812345678   <--- right here
-- host: 192.168.1.234

Tip: if InetAddress.getCanonicalHostName().equals(InetAddress.getHostAddress()) you can ignore it as it's not a "real" name.

2 Comments

Sometimes the domain is included so it is not the plain hostname.
Will print my IP address and not android-1234567812345678 :( (Samsung Galaxy S3)
0

For java:

you can get the property by below:

String str = SystemProperties.get("net.hostname");

Comments

0

I used jiangze ren's answer and modified it.

Now you can get hostname by getting IP address of device:

   WifiManager wm = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
                        final String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());
                        InetAddress address = InetAddress.getByName(ip);
                        System.out.println(address.getHostName());  // <-- host name is here and not localhost!

1 Comment

Will print my IP address and not android-1234567812345678 :( (Samsung Galaxy S3)
0

Credits to Merlevede. This is the actually working code based on his answer.

    String hostname;
    Process ifc = null;
    try {
        ifc = Runtime.getRuntime().exec("getprop net.hostname");
        BufferedReader bis = new BufferedReader(new InputStreamReader(ifc.getInputStream()));
        hostname = bis.readLine(); // android-1234567812345678
        ifc.destroy();
    } catch (IOException e) {
        e.printStackTrace();
    }

None of the other answers helped me.

Comments

-2

you can use the code below:

InetAddress address = InetAddress.getLocalHost();  
System.out.println(address.getHostName());  // <-- host name is here!

And actually, you can more info using this function. such as... other devices hostname which are in the same network.

1 Comment

all this is going to return is localhost too.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.