I am working on raspberry pi and android application in which everytime application is get opned it search for raspberry pi in network and if raspberry pi is found then do further operation else give aknwolegement to the user. I just need IP address of raspberry pi to do further process.
Solution purposed -
Making raspberry pi IP address static - Not applicable because application distributed from play store and dont have access to router.
Searching for the raspberry pi in network - Working on this.
What i tried is used SSDP, DLNA, UPNP protocol to create a server on raspberry pi and everytime app comes online search for the raspberry pi in network.
Used resourcee
- https://github.com/resourcepool/ssdp-client
- https://gist.github.com/ismaelgaudioso/4cff466459646e022332
- https://gist.githubusercontent.com/ismaelgaudioso/4cff466459646e022332/raw/2f9fb030790102c31bc656a960307028c28bad51/server.py
- https://www.javatips.net/api/serket-master/serket-ssdp/src/main/java/org/saintandreas/serket/ssdp/SSDPServer.java
Here is what i have done
private static final String tag = "SSDP";
private static final String query = "M-SEARCH * HTTP/1.1\r\n" + "HOST: 239.255.255.250:1900\r\n" + "MAN: \"ssdp:discover\"\r\n" + "MX: 1\r\n" +
//"ST: urn:schemas-upnp-org:device:MediaServer:1\r\n" +
"ST: ssdp:all\r\n"+
"\r\n";
private static final int port = 1900;
String request() {
String response = "";
byte[] sendData;
byte[] receiveData = new byte[1024];
sendData = query.getBytes();
DatagramPacket sendPacket = null;
try {
sendPacket = new DatagramPacket(sendData, sendData.length, InetAddress.getByName("239.255.255.250"), port);
} catch (UnknownHostException e) {
Log.d(tag, "Unknown Host Exception Thrown after creating DatagramPacket to send to server");
e.printStackTrace();
}
DatagramSocket clientSocket = null;
try {
clientSocket = new DatagramSocket();
} catch (SocketException e) {
Log.d(tag, "Socket Exception thrown when creating socket to transport data");
e.printStackTrace();
}
try {
if (clientSocket != null) {
clientSocket.setSoTimeout(50000);
clientSocket.send(sendPacket);
}
} catch (IOException e) {
Log.d(tag, "IOException thrown when sending data to socket");
e.printStackTrace();
}
DatagramPacket receivePacket = new DatagramPacket(receiveData, receiveData.length);
try {
if (clientSocket != null) {
clientSocket.receive(receivePacket);
}
} catch (IOException e) {
Log.d(tag, "IOException thrown when receiving data");
e.printStackTrace();
}
//the target package should not be empty
//try three times
for (int i = 0; i < 3; i++) {
Log.d(tag, "Checking target package to see if its empty on iteration#: " + i);
response = new String(receivePacket.getData());
Log.d(tag, "Response contains: " + response);
if (response.contains("Location:")) {
break;
}
}
String adress = "";
//filter IP address from "Location"
Matcher ma = Pattern.compile("Location: (.*)").matcher(response);
if (ma.find()) {
adress += ma.group(1);
adress = adress.split("/")[2].split(":")[0];
}
return adress;
}
Using above methode and solution i was able to find out router IP address everytime but not of pi. Also gone through each of every library i can found on internet but not worked. Apart from this method if there any other way suggested will be appraciated.
Now eventually i figured out the solution i will share my answer step by step. I am using ssdp protocol to find out the Pi on network where the Pi have dynamic Ip address. So i created the server in python and using android as a client. lets start with server first -
Also you can find the server script https://github.com/ZeWaren/python-upnp-ssdp-example
This will create an exuction point for the server script.
A class implementing a SSDP server. The notify_received and searchReceived methods are called when the appropriate type of datagram is received by the server.
A HTTP handler that serves the UPnP XML files. Lets scan the Pi on network using android.
Now lets create a new asyncTask because it is network releated process and add the below code in asyncTask
With above code you can also create media server on Pi.