receive UDP data on Google Glass with GDK

耗尽温柔 提交于 2019-12-02 15:21:11

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/22083623/receive-udp-data-on-google-glass-with-gdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!