receive UDP data on Google Glass with GDK

时光总嘲笑我的痴心妄想 提交于 2019-12-02 03:40:07

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

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.

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