From 2fa76596ebb534ba1969c17f758ae5c72e9a72e9 Mon Sep 17 00:00:00 2001 From: iron Date: Thu, 9 Jul 2020 21:30:31 +0100 Subject: [PATCH] Upload files to '' --- UDPthing.txt | 154 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 154 insertions(+) create mode 100644 UDPthing.txt diff --git a/UDPthing.txt b/UDPthing.txt new file mode 100644 index 0000000..59296af --- /dev/null +++ b/UDPthing.txt @@ -0,0 +1,154 @@ +package com.example.ironi.udp_switches; + +import android.app.Activity; +import android.os.AsyncTask; +import android.os.Bundle; +import android.util.Log; +import android.widget.CompoundButton; +import android.widget.CompoundButton.OnCheckedChangeListener; +import android.widget.Switch; +import android.widget.TextView; + +import java.io.IOException; +import java.net.DatagramPacket; +import java.net.DatagramSocket; +import java.net.InetAddress; +import java.net.SocketException; +import java.net.UnknownHostException; + +public class MainActivity extends Activity { + private TextView switchStatus; + private Switch mySwitch; + private Switch hallSwitch; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + Log.d("main", "logging works"); + //new udpServer().execute(); + //new udpClient().execute("192.168.0.201", "State"); + + switchStatus = (TextView) findViewById(R.id.switchStatus); + mySwitch = (Switch) findViewById(R.id.mySwitch); + hallSwitch = (Switch) findViewById(R.id.hallSwitch); + //run a status check when you know how? + // + //set the switch to ON + mySwitch.setChecked(true); + //attach a listener to check for changes in state + + hallSwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + + if (isChecked) { + new udpClient().execute("192.168.0.202", "On"); + } else { + new udpClient().execute("192.168.0.202", "Off"); + } + + } + }); + + mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() { + @Override + public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) { + if (isChecked) { + new udpClient().execute("192.168.0.201", "On"); + } else { + new udpClient().execute("192.168.0.201", "Off"); + } + + } + }); + //check the current state before we display the screen + if (mySwitch.isChecked()) { + switchStatus.setText("Switch is currently ON"); + } else { + switchStatus.setText("Switch is currently OFF"); + } + } + + public class udpServer extends AsyncTask { + @Override + protected Void doInBackground(String... params) { + //SOCKET OPEN + + //PACKET READY TO MAKE + byte[] rData = new byte[8]; + DatagramPacket rPacket = new DatagramPacket(rData, rData.length); + + + //READY TO RECIEVE + Log.d("udpServer", "Ready to recieve"); + while (true) { + try { + Thread.sleep(1000); + } catch (InterruptedException e) { + e.printStackTrace(); + } + DatagramSocket rSocket = null; + try { + rSocket = new DatagramSocket(4012); + } catch (SocketException e) { + e.printStackTrace(); + } + Log.d("udpServer", "while"); + + try { + rSocket.setSoTimeout(1000); + rSocket.setReuseAddress(true); + rSocket.receive(rPacket); + String rString; + rString = new String(rPacket.getData()); + Log.d("udpServer", "Recieved: "+rString); + publishProgress(); + rSocket.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + } + + protected void onProgressUpdate(Integer... values) { + Log.d("udpServer", "onProgressUpdate"); + switchStatus.setText("-"); + } + } + + public class udpClient extends AsyncTask { + @Override + protected String doInBackground(String... params) { + String address = params[0]; + String command = params[1]; + + InetAddress client = null; + try { + client = InetAddress.getByName(address); + } catch (UnknownHostException e) { + e.printStackTrace(); + } + int port = 4210; + int len = command.length(); + byte[] udp_command = command.getBytes(); + + DatagramSocket socket = null; + try { + socket = new DatagramSocket(); + } catch (SocketException e) { + e.printStackTrace(); + } + + DatagramPacket packet = new DatagramPacket(udp_command, len, client, port); + + try { + socket.send(packet); + } catch (IOException e) { + e.printStackTrace(); + } + return null; + } + } +}