Back to project page OHC-android.
The source code is released under:
MIT License
If you think the Android project OHC-android listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package io.openhc.ohc.skynet; //from ww w .ja v a 2 s . com import android.os.AsyncTask; import org.json.JSONObject; import java.net.DatagramPacket; import java.net.DatagramSocket; import java.util.logging.Level; import io.openhc.ohc.OHC; import io.openhc.ohc.basestation.Basestation; /*Using an async task to get received data transfered to the UI thread without having to care about synchronisation manually*/ public class Receiver extends AsyncTask<Void, JSONObject, Void> { private DatagramSocket socket; private Basestation basestation; public Receiver(DatagramSocket socket, Basestation base) { this.socket = socket; this.basestation = base; } public Void doInBackground(Void... args) { while(true) { byte[] data = new byte[1024]; DatagramPacket packet = new DatagramPacket(data, data.length); try { this.socket.receive(packet); String jsonStr = new String(packet.getData(), "UTF-8"); OHC.logger.log(Level.INFO, "Packet received: " + jsonStr); JSONObject json = new JSONObject(jsonStr); //Publish received data to UI thread via progress notification this.publishProgress(json); } catch(Exception ex) { OHC.logger.log(Level.WARNING, "Error receiving data on udp socket: " + ex.toString()); break; } } return null; } //This function is synchronized to the UI thread @Override public void onProgressUpdate(JSONObject... args) { this.basestation.handle_packet(args[0]); } public int get_port() { return this.socket.getLocalPort(); } }