UDP Sender/Recevier Program -Android

Hello,
Today we are going to learn about UDP protocol in android for communication with the device. UDP is a too easy protocol for send message and receives the message from the hardware device.

MainActivity.java

public class MainActivity extends AppCompatActivity {

    Thread UDPrecThread = null;
    UDPsender udpsnd = null;
    DatagramSocket ds1 = null;
    @Override    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        send_udp_msg("{" +
                "\"id\":" + "\"" + "id" + "\""  );
    }
    public void send_udp_msg(final String str) {
        try {
            UDPrecThread = new Thread(new UDPreceiver());
            UDPrecThread.start();
            udpsnd = new MainActivity.UDPsender(str);
            udpsnd.start();
            Log.w("device_id111111", str);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    class UDPsender extends Thread {
        DatagramSocket ds = null;
        String msgstr;
        public UDPsender(String str) {
            this.msgstr = str;
        }
        public synchronized void run() {
            try {
                this.ds = new DatagramSocket(null);
                this.ds.setReuseAddress(true);
                this.ds.setBroadcast(true);
                this.ds.bind(new InetSocketAddress(3200));
                this.ds.send(new DatagramPacket(this.msgstr.getBytes(), this.msgstr.length(), InetAddress.getByName("YOUR_IP_ADDRESS"), 2370));
                Log.w("device_ip", "YOUR_IP_ADDRESS");
                this.ds.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    public class UDPreceiver implements Runnable {
        public synchronized void run() {
            try {
                MainActivity.this.ds1 = new DatagramSocket(null);
                MainActivity.this.ds1.setReuseAddress(true);
                MainActivity.this.ds1.setBroadcast(true);
                MainActivity.this.ds1.bind(new InetSocketAddress(3200));
                final byte[] buf = new byte[1000];
                final DatagramPacket dp1 = new DatagramPacket(buf, 500);

                while (true) {
                    ds1.receive(dp1);
                    final String msg = new String(buf, 0, dp1.getLength());
                    dp1.setLength(buf.length);
                    if (msg != "" && msg.length() > 0)
                        MainActivity.this.runOnUiThread(new Runnable() {
                            public void run() {
                                Log.w("deviceinformation", msg);

                            }
                        });
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}


Above code first, we can call the send_udp_msg method and send data and receive a message. Receive message show in android logcat windows. above communication work on 3200 protocol.

Comments