Reputation: 1129
What I actually want to do is to broadcast a message over wifi (supposing that other cell has app running on it which I have created, but I don't know its IP yet) So that application when receive that message it returns back a message with the IP of that device via wifi, I am thinking about this scenario in ideal condition where there both devices are running the same app I created just wanted to add the functionality to broadcast a message over wifi to other devices, is it possible or how?
Upvotes: 2
Views: 11146
Reputation: 20875
I created this demo program in Java. Each host (Android device) will run both the client and the server code. The client continuously publishes its IP address on a multicast address, where all other devices are listening and keep track of it. The very same device is also a listener on the same multicast address, so he can discover other Androids as they send a packet. Note that this architecture is very heavy on resources usage and doesn't scale at all. I simply wrote this to play with datagrams and it also seems the closest answer to your question
import java.net.DatagramPacket;
import java.net.InetAddress;
import java.net.MulticastSocket;
public class Main {
public static final int PORT = 2345;
public static final String MULTICAST = "224.0.0.99";
/*
* A simple server that prints the name of the first
* device discovered and then terminates
*/
static class Server extends Thread{
@Override
public void run() {
try {
MulticastSocket socket = new MulticastSocket(PORT);
InetAddress group = InetAddress.getByName(MULTICAST);
socket.joinGroup(group);
byte[] buf = new byte[256];
// receive request
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);
System.out.printf("SERVER receives: %s", new String(packet.getData()));
socket.leaveGroup(group);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/*
* This client send own IP address to all machines
* listening on MULTICAST
*/
static class Client extends Thread{
@Override
public void run() {
try {
MulticastSocket socket = new MulticastSocket();
socket.setBroadcast(true);
byte[] buffer = "I'm a new client. My IP is XXX.XXX.XXX.XXX".getBytes();
DatagramPacket packet = new DatagramPacket(buffer, buffer.length, InetAddress.getByName(MULTICAST), PORT);
socket.send(packet);
socket.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
public static void main(String[] args) throws InterruptedException {
new Server().start();
Thread.sleep(1000); // Make sure server started
new Client().start();
}
}
Regarding the Android part, you'll need the permission android.permission.CHANGE_WIFI_MULTICAST_STATE
because multicast packed are filtered out by the Android stack, so you will use a WifiManager.MulticastLock
WifiManager wm = (WifiManager)getSystemService(Context.WIFI_SERVICE);
WifiManager.MulticastLock multicastLock = wm.createMulticastLock("mydebuginfo");
multicastLock.acquire();
Upvotes: 3
Reputation: 9585
I was thinking exactly about this some days ago. My conclusion was that you can not relay on the device's IP as they can obtain it through DHCP and it can change everytime. So, a very easy approach is to send the data attached in an e-mail and send it to the receiver.
You have to implement a parser which can read the data (suppose it is written in an XML file) and do what you need on the receiver. To achieve this you will have to register the correct component (Activity) to respond to an Intent ACTION_VIEW of the type of file you transfer e.g. XML.
Hope it helps!
Upvotes: 1