1

Any recommendations for receiving UDP data from an app running on Google Glass?

I need to integrate with an existing system. This system does UDP broadcasts to the local subnet. The Glass will be on the same subnet and the app running on the Glass just needs to listen on a port for UDP packets and then display information contained in them to the user.

I'm integrating with an existing system which I don't have the freedom to change at this point and so want to just receive UDP (as opposed to using other, perhaps better, higher level frameworks).

Looking around the docs I see network related topics pointing me to higher level stuff for interoperating, but I'm really just looking for a low level UDP receiving socket and wondering what would be recommended for doing this on Glass.

Any suggestions?

Nerdtron
  • 1,486
  • 19
  • 32

2 Answers2

2

According to GDK welcome page:

We designed the Glass platform to make the existing Android SDK just work on Glass. This lets you code in a familiar environment, but for a uniquely novel device.

In addition, you can use all of the existing Android development tools, and your Glassware is even delivered as a standard Android package (APK).

So, presumably you should be able to do something along the lines of (untested):

byte[] buf = new byte[1024];
DatagramSocket socket = new DatagramSocket(port);
DatagramPacket packet = new DatagramPacket(buf, buf.length);
socket.receive(packet);

DatagramSocket reference

Community
  • 1
  • 1
Philippe Signoret
  • 13,299
  • 1
  • 40
  • 58
  • Ah, right. Well, I'll mark this as the answer and see how it works soon :) – Nerdtron Feb 28 '14 at 04:11
  • 1
    Don't forget to come back and unmark/update if it doesn't work, or if you find a better answer. – Philippe Signoret Feb 28 '14 at 23:46
  • Looking through the Glass examples and documentation I think I'll need to be able to start/stop the UDP listening activity. If I'm in a socket.receive() call as shown in your example, how can I interrupt and break out of that on demand? – Nerdtron Mar 07 '14 at 17:14
  • I posted a separate question about starting/stopping the UDP listening code on demand [here](http://stackoverflow.com/questions/22309976/need-an-interruptable-way-to-listen-for-udp-packets-in-a-worker-thread) – Nerdtron Mar 10 '14 at 19:55
  • My question was already answered, see [here](http://stackoverflow.com/questions/7962121/how-can-i-stop-the-block-method-datagramsocket-receive-in-a-thread) – Nerdtron Mar 10 '14 at 20:16
  • 1
    I have this up and running now on Glass and so can confirm that it does work. One thing to be aware of is that you must request "INTERNET" permission or else the attempts to create the socket will fail with a permission denied exception. More info on that can be found [here](http://stackoverflow.com/questions/2378607/what-permission-do-i-need-to-access-internet-from-an-android-application). – Nerdtron May 03 '14 at 02:48
1

Nerdtron, did you make your glass UDP code work?

I'm in very similar situation and trying to make my UDP code work as a GDK glassware app.

If you were successful, can you share how you made it work and also your codes?

If not, what I have found so far are:

  1. Networking part should run in a separate thread (not in the main thread). So knowing only about DatagramSocket/Packet won't help much at all.

  2. If the app needs to continuously run and receive data/packets, AsyncTask won't help. So stop using it. I wasted lots of time. We need to create a separate thread for continuously receiving data. AsyncTask is for short or temporary tasks.

  3. GUI parts must not be manipulated in threads other than the main thread (the main thread is 'GUI' thread). Any code which attempt to manipulate any GUI part (even texts) will generate error. So in order to change GUI, thread(s) receiving data via network/internet need(s) to use 'handler' (callback).

So continuously receiving data from network/internet and updating GUI in Android/Glass apps is not easy at all. It requires clearly understanding how things work in Android/Glass apps regarding network and GUI.

I made my code receive UDP packets and update texts in my Glass screen so far. I'll try to share my code, findings, references, etc. later after I clean up my code and make sure it works somewhat correctly.

Let me know if you have any questions.

  • Not yet; I should be there very soon. My initial plan was to do the following. Create a worker thread to receive the UDP packets in the background so as to not hold up the main GUI thread. Anytime I get a packet, decode it and update public shared state in the worker thread and send a notification to the GUI thread. The main GUI thread would then simply wait for notifications and update the state when those occur. So the background thread is just doing UDP and the GUI thread is just doing GUI and they communicate via shared state (or a queue or something). I'm hoping this will work! – Nerdtron Mar 04 '14 at 01:30
  • I have the code up and running now on Glass. See my reply to the marked answer above. I'm creating a worker thread which is in an infinite loop reading from the datagram socket. If the app needs to terminate, I just close the socket which throws an exception and gets my worker thread out of its loop. Seems to be working very nicely. – Nerdtron May 03 '14 at 02:50
  • 1
    Sorry. I forgot to post the code for it. Did you already successfully make your code working? The information and link mentioned above are the minimum things for udp network coding for glass/android. I'll try to post some simple code if you still need some more help. Thanks. – user3261595 May 06 '14 at 18:11
  • Yes, see my comment above -- the code is working great. I'm using the UDP code in the marked answer and then using the technique I mentioned from the other post to break the worker thread out of its loop when ready to stop. – Nerdtron May 06 '14 at 22:08