omkar ugale
omkar ugale

Reputation: 116

How to get local network device Host name in android application

Hi I can find local network active devices list of IP and MAC address but not getting Host name using Ip or MAC plz any body tell what I need to do to find host name

I use

 InetAddress inetAddr = InetAddress.getByName("192.168.1.2");
 String hostname = inetAddr.getHostName();            
 System.out.println("Hostname: " + hostname);
O/p:-
Hostname:192.168.1.2

I want name of that device any other way to get that user details uising arp or else

Upvotes: 1

Views: 2114

Answers (1)

Ashish Agrawal
Ashish Agrawal

Reputation: 1977

To get MAC address please use this-

WifiManager manager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
WifiInfo info = manager.getConnectionInfo();
String address = info.getMacAddress();

Permission needed for MAC address-

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>

To get IP Address

WifiManager wm = (WifiManager) getSystemService(WIFI_SERVICE);
String ip = Formatter.formatIpAddress(wm.getConnectionInfo().getIpAddress());

Permission needed for IP address

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

Upvotes: -2

Related Questions