Created
September 6, 2012 11:18
-
-
Save finnjohnsen/3654994 to your computer and use it in GitHub Desktop.
Android UDP Broadcast listener service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package no.nsb.ombord; | |
import java.net.DatagramPacket; | |
import java.net.DatagramSocket; | |
import java.net.InetAddress; | |
import java.net.SocketTimeoutException; | |
import java.net.UnknownHostException; | |
import org.apache.http.util.ExceptionUtils; | |
import android.app.Service; | |
import android.content.Intent; | |
import android.os.IBinder; | |
import android.util.Log; | |
import android.widget.Toast; | |
/* | |
* Linux command to send UDP: | |
* #socat - UDP-DATAGRAM:192.168.1.255:11111,broadcast,sp=11111 | |
*/ | |
public class UDPListenerService extends Service { | |
static String UDP_BROADCAST = "UDPBroadcast"; | |
//Boolean shouldListenForUDPBroadcast = false; | |
DatagramSocket socket; | |
private void listenAndWaitAndThrowIntent(InetAddress broadcastIP, Integer port) throws Exception { | |
byte[] recvBuf = new byte[15000]; | |
if (socket == null || socket.isClosed()) { | |
socket = new DatagramSocket(port, broadcastIP); | |
socket.setBroadcast(true); | |
} | |
//socket.setSoTimeout(1000); | |
DatagramPacket packet = new DatagramPacket(recvBuf, recvBuf.length); | |
Log.e("UDP", "Waiting for UDP broadcast"); | |
socket.receive(packet); | |
String senderIP = packet.getAddress().getHostAddress(); | |
String message = new String(packet.getData()).trim(); | |
Log.e("UDP", "Got UDB broadcast from " + senderIP + ", message: " + message); | |
broadcastIntent(senderIP, message); | |
socket.close(); | |
} | |
private void broadcastIntent(String senderIP, String message) { | |
Intent intent = new Intent(UDPListenerService.UDP_BROADCAST); | |
intent.putExtra("sender", senderIP); | |
intent.putExtra("message", message); | |
sendBroadcast(intent); | |
} | |
Thread UDPBroadcastThread; | |
void startListenForUDPBroadcast() { | |
UDPBroadcastThread = new Thread(new Runnable() { | |
public void run() { | |
try { | |
InetAddress broadcastIP = InetAddress.getByName("172.16.238.255"); //172.16.238.42 //192.168.1.255 | |
Integer port = 11111; | |
while (shouldRestartSocketListen) { | |
listenAndWaitAndThrowIntent(broadcastIP, port); | |
} | |
//if (!shouldListenForUDPBroadcast) throw new ThreadDeath(); | |
} catch (Exception e) { | |
Log.i("UDP", "no longer listening for UDP broadcasts cause of error " + e.getMessage()); | |
} | |
} | |
}); | |
UDPBroadcastThread.start(); | |
} | |
private Boolean shouldRestartSocketListen=true; | |
void stopListen() { | |
shouldRestartSocketListen = false; | |
socket.close(); | |
} | |
@Override | |
public void onCreate() { | |
}; | |
@Override | |
public void onDestroy() { | |
stopListen(); | |
} | |
@Override | |
public int onStartCommand(Intent intent, int flags, int startId) { | |
shouldRestartSocketListen = true; | |
startListenForUDPBroadcast(); | |
Log.i("UDP", "Service started"); | |
return START_STICKY; | |
} | |
@Override | |
public IBinder onBind(Intent intent) { | |
return null; | |
} | |
} |
bind failed: EADDRNOTAVAIL (Cannot assign requested address) - answer:
socket = new DatagramSocket(port, broadcastIP);
change to this ---> socket = new DatagramSocket(port);
how i can send message with that service active? port is busy? can u help?
Hi can anyone help me please . I want to listen on port number 15000 for a UDP packet .
I don't know if the IP is necessary as well . I'm trying to listen to a UDP packet sent from infinite flight app on my device and I don't know what IP address the app is sending the UDP port# is 15000
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Udp broadcast is received, with the above code. In my case i am receiving packets mainly strings from a server at quick constant intervals, all works fine apart from some packet loss(UDP signals are not reliable) my actual problem is after using the service for a long time i am not receiving any signal from the server. Is this because android is killing our service or Thread, or is it because of the the system buffer overflow. I have no insight in network programming and i am stuck. Is there any way we could detect that the thread is stoped and we are not receiving any signals from the server any more and thus restart the whole process?