Detecting all devices connected to my wifi router using Android Programming - android

I am trying to build an app in Android that would need the IP addresses of all devices (PCs and other mobile devices) connected to a wifi router (my local router). The IP addresses are the ones assigned to the devices by the router using DHCP. Moreover, the app that I am trying to build would be local to a device connected to the same router. I have looked all over the web for Android code that could accomplish this, but all I found was how to scan for wifi access-points. Is what I am trying to do possible using Android programming?

There's no direct API for this. Its not like the wifi router gives everyone a list of all IPs it assigns. You could try pinging every IP on your wifi network (you can tell what IPs those are by netmask), but that will only work if the device is configured to return ICMP packets and your router doesn't block them.
What might work for your app is Wi-fi direct (http://developer.android.com/guide/topics/connectivity/wifip2p.html).

It totally depends on your router: if it has this sort of functionality exposed via API or other. Most routers don't permit this sort of deep-querying. You might look at tomato or dd-wrt if you want to have more control over it.

You can do this by using the arp cache table by:
BufferedReader br = null;
ArrayList<String[]> ipCache = new ArrayList<>(3);
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] split = line.split(" +");
if (split.length >= 4 ) {
if(!split[0].equals("IP") &&!split[0].equals(ROUTER_IP) ){
ipCache.add(split);
}
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
int ipsPonged = ipCache.size();
if(ipsPonged>0){
for (String[] client :
ipCache) {
// if one ping test succeeds we are fine
if(!ping(client[0])){
ipsPonged--;
}
}
if(ipsPonged == 0){
return true;
}
}else{
return false;
}

Related

How to make an android app to get the IP address of the pc from my android device connected on same wifi

I want to make an android app to find the IP address of all the connected devices connected on the same wifi. I tried this :
for (int i = lower; i <= upper; i++) {
String host = subnet + i;
try {
InetAddress inetAddress = InetAddress.getByName(host);
if (inetAddress.isReachable(timeout)){
publishProgress(inetAddress.toString());
}
} catch (UnknownHostException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
But it can get only the connected mobile phone's IP, not the pc's.
How to get the connected pc's IP address also?
Try this
public ArrayList<String> getClientList() {
ArrayList<String> clientList = new ArrayList<>();
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = br.readLine()) != null) {
String[] clientInfo = line.split(" +");
String mac = clientInfo[3];
if (mac.matches("..:..:..:..:..:..")) { // To make sure its not the title
clientList.add(clientInfo[0]);
}
}
} catch (java.io.IOException aE) {
aE.printStackTrace();
return null;
}
return clientList;
}
and use it like this
ArrayList<String> list = getClientList();
for (String ip:list) {
Log.e(TAG,ip);
}
Result :
192.168.1.9
192.168.1.1
192.168.1.5
also check this question
Many networks ban that entirely, a practice known as "wireless isolation" or "client isolation", which saves battery, improves throughput, improves privacy if the network is public, at a cost of making it difficult for one client to find other hosts on the same WLAN.
For the remaining ones, pinging all of the addresses in the subnet will generally be the best way, even if a little slow. Your code looks okay, provided that the string subnet ends with a dot.
All of this is meaningless on IPv6, of course.

Android get UUID(Services) of own bluetooth device

I am creating a application in which one module is there where i want to retrieve the services supported by my own Bluetooth device...
Currently i am able to fetch the UUID of remote devices, by i havent found out any way to retrieve the UUID of my own device.
Thanks in advance
Finally after a lot of struggling i found a way to find the UUID of own bluetooth device. Sdptool provides the interface for performing SDP queries on Bluetooth devices, and administering a local sdpd. Code snippet for it is follows:This code will only work in devices with root access.
try {
System.setOut(new PrintStream(new FileOutputStream("/mnt/sdcard/abc.txt")));
System.out.println("HelloWorld1");
Process p;
p = Runtime.getRuntime().exec(new String[] { "su", "-c","sdptool", "browse", "local" });
BufferedReader stdInput = new BufferedReader(new InputStreamReader(
p.getInputStream()));
String s;
String res = "";
while ((s = stdInput.readLine()) != null) {
if(s.contains(""))
System.out.println(s);
Log.e("above -----", s);
}
p.destroy();
return res;
} catch (Exception e) {
e.printStackTrace();
}
and in case you want to discover the services of another Bluetooth device then you can replace "local" with the MAC address of the remote device.
Or you can also try running the sdp tool usinf adb shell as follows:
adb shell sdptool browse local

How to get the system ip address after usb tethering of android phone?

I'm developing a mobile application in android.
Here I want to detect the IP address of the computer,system,etc after the usb tethering of the any android phone
I cannot find the solution.
If I put the following code then it takes the only the IP address of phone ,I need IP address of system
The following are code
ArrayList<InetAddress> arrayList=new ArrayList<InetAddress>();
try {
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
NetworkInterface intf = en.nextElement();
for (Enumeration<InetAddress> enumIpAddr = intf.getInetAddresses(); enumIpAddr.hasMoreElements();) {
InetAddress inetAddress = enumIpAddr.nextElement();
arrayList.add(inetAddress);
inetAddress=null;
}
}
} catch (SocketException ex) {
Log.e("SALMAN", ex.toString());
}
return arrayList;
Please help me to get the system's IP address,If we cannot able to get means so please mention me. Because I'm new to android.
I'm using android 1.6 .
There is server side application in the windows xp system. That application is a windows service which is developed by C# .net.
That windows service listen to some port such like 234,etc.If some data comes to port then it will process the data and send response through that port.
In android the android application is send the data to the windows service via socket.
The android phone is USB tethered to the system in which windows service is running.Then system assume android phone is modem and additional IP address is generated for the system.This ip address is dynamically generated when the android phone is tethered.
For data transfer form mobile to system via socket .I will need to give the ip address of the system (after tethered) in my android coding.
If there is any method in android coding to get this IP address.
All are please give your ideas on regarding this.
Its not possible to find IP address created in PC from android after tethering. There is no API or other way to find it.
If you use InetAddress , it will return 192.168.42.129 - which is a DHCP address created by USB Tethering. It wont help you to communicate.
The other way is to scan the list of IP. USB Tethering will create ip ranging for 192.168.42.1 to 192.168.42.255 . You can write a simple scanner to find which one is active. But it will take some time.
Thanks to 'Swim N Swim' above. I found a code at
Retrieve IP and MAC addresses from /proc/net/arp (Android)
and modified a bit to get first IP having valid mac address. Works great when developing as a single user on your PC with tethered. You may follow above link for further selective IPs based on company name etc.
public static String getUSBThetheredIP() {
BufferedReader bufferedReader = null;
String ips="";
try {
bufferedReader = new BufferedReader(new FileReader("/proc/net/arp"));
String line;
while ((line = bufferedReader.readLine()) != null) {
String[] splitted = line.split(" +");
if (splitted != null && splitted.length >= 4) {
String ip = splitted[0];
String mac = splitted[3];
if (mac.matches("..:..:..:..:..:..")) {
if (mac.matches("00:00:00:00:00:00")) {
//Log.d("DEBUG", "Wrong:" + mac + ":" + ip);
} else {
//Log.d("DEBUG", "Correct:" + mac + ":" + ip);
ips = ip;
break;
}
}
}
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} finally{
try {
bufferedReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return ips;
}
Note that each time you tether after untether, you must start your apache or other processes on PC to take new IP effective. THis is what I experienced.

wifi search for the devices connected to the same network i.e other than acces point(for android)

i want to make a modification to my project and right now the project status is.....
it is searches the available WiFi networks and shows the list with info of the network this works properly.Now i want to search and see the details of the devices connected to the network.
Is there any way to find these devices ?
Your comment will be useful for me, Thanks.
You can loop over the IP ranges, and "ping" them.
It is not the best / fastest method (UDP is better) but, it works in many cases.
The sample code below returns the list of the IP addresses connected to the current network.
private int LoopCurrentIP = 0;
public ArrayList<InetAddress> getConnectedDevices(String YourPhoneIPAddress) {
ArrayList<InetAddress> ret = new ArrayList<InetAddress>();
LoopCurrentIP = 0;
String IPAddress = "";
String[] myIPArray = YourPhoneIPAddress.split("\\.");
InetAddress currentPingAddr;
for (int i = 0; i <= 255; i++) {
try {
// build the next IP address
currentPingAddr = InetAddress.getByName(myIPArray[0] + "." +
myIPArray[1] + "." +
myIPArray[2] + "." +
Integer.toString(LoopCurrentIP));
// 50ms Timeout for the "ping"
if (currentPingAddr.isReachable(50)) {
ret.add(currentPingAddr);
}
} catch (UnknownHostException ex) {
} catch (IOException ex) {
}
LoopCurrentIP++;
}
return ret;
}
Would you like to discover a specific device ? Or you need the list of all connected devices? The second I don't think is possible.
EDIT
Discovering specific devices:
Using UDP Broadcast. Some reference can be found here!
There are some protocols that are supported by some devices( routers, HDD, etc...), like UPNP!
If you develop a software on the device which you would like to discover you could create a UDP server listening on a specific port.
Your client will just send a broadcast message on that port and your Server will send a response with the information you need.
Here it is a simple example.

Detecting android devices connected to Wifi

I want to make an android application that connects to a Wifi network, say network SSID = "ABC".Assume that it is connected to the Wifi ABC. After connecting to ABC, i would want my application to display the ips of all the android devices that are connected to the same wifi ABC network. How can i achieve that? Thanks
Check out the file: /proc/net/arp on your phone.
It has the ip and MAC addreses of all the other devices connected to the same network. However I am affraid you wont be able to differentiate if they are android phones or not.
You will want to use tcpdump to put the network card into promiscous mode and then capture packets to identify what other clients are on your network.
How to use tcpdump on android:
http://source.android.com/porting/tcpdump.html
You can run commands in your code like so:
try {
// Executes the command.
Process process = Runtime.getRuntime().exec("/system/bin/ls /sdcard");
// Reads stdout.
// NOTE: You can write to stdin of the command using
// process.getOutputStream().
BufferedReader reader = new BufferedReader(
new InputStreamReader(process.getInputStream()));
int read;
char[] buffer = new char[4096];
StringBuffer output = new StringBuffer();
while ((read = reader.read(buffer)) > 0) {
output.append(buffer, 0, read);
}
reader.close();
// Waits for the command to finish.
process.waitFor();
return output.toString();
} catch (IOException e) {
throw new RuntimeException(e);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}

Categories

Resources