Advertisement
Guest User

Untitled

a guest
Oct 17th, 2019
169
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.94 KB | None | 0 0
  1. package cpe.moi.hello.iotprojet;
  2.  
  3. import androidx.appcompat.app.AppCompatActivity;
  4.  
  5. import android.os.AsyncTask;
  6. import android.os.Bundle;
  7. import android.text.Editable;
  8. import android.text.TextWatcher;
  9. import android.widget.EditText;
  10.  
  11. import java.io.IOException;
  12. import java.net.DatagramPacket;
  13. import java.net.DatagramSocket;
  14. import java.net.InetAddress;
  15.  
  16. public class MainActivity extends AppCompatActivity {
  17.  
  18. private String IP;
  19. private int PORT;
  20. private InetAddress address;
  21. private DatagramSocket udpSocket;
  22. public String message;
  23.  
  24. @Override
  25. protected void onCreate(Bundle savedInstanceState) {
  26. super.onCreate(savedInstanceState);
  27. setContentView(R.layout.activity_main);
  28.  
  29. //Todo: change ip
  30. EditText txtIP = findViewById(R.id.editText);
  31. EditText txtPORT = findViewById(R.id.editText2);
  32.  
  33. txtIP.addTextChangedListener(new TextWatcher() {
  34. @Override
  35. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  36.  
  37. }
  38.  
  39. @Override
  40. public void onTextChanged(CharSequence s, int start, int before, int count) {
  41.  
  42. }
  43.  
  44. @Override
  45. public void afterTextChanged(Editable s) {
  46. IP = s.toString();
  47. }
  48. });
  49.  
  50. txtPORT.addTextChangedListener(new TextWatcher() {
  51. @Override
  52. public void beforeTextChanged(CharSequence s, int start, int count, int after) {
  53.  
  54. }
  55.  
  56. @Override
  57. public void onTextChanged(CharSequence s, int start, int before, int count) {
  58.  
  59. }
  60.  
  61. @Override
  62. public void afterTextChanged(Editable s) {
  63. PORT = Integer.valueOf(s.toString());
  64. }
  65. });
  66.  
  67. }
  68.  
  69. private void sendPacket() {
  70. (new Thread() {
  71. public void run() {
  72. try{
  73. udpSocket = new DatagramSocket();
  74. address = InetAddress.getByName(IP);
  75. byte[] data = message.getBytes();
  76. DatagramPacket packet = new DatagramPacket(data, data.length, address, PORT);
  77. udpSocket.send(packet);
  78. } catch(IOException e){
  79. e.printStackTrace();
  80. }
  81. }
  82. }).start();
  83. }
  84.  
  85. private class ReceiverTask extends AsyncTask<Void, byte[], Void> {
  86. @Override
  87. protected Void doInBackground(Void... voids) {
  88. while(true){
  89. byte[] data = new byte [1024];
  90. DatagramPacket packet = new DatagramPacket(data, data.length);
  91. udpSocket.receive(packet);
  92. int size = packet.getLength();
  93. publishProgress(java.util.Arrays.copyOf(data, size));
  94. }
  95. }
  96.  
  97. protected void onProgressUpdate(byte[]... data){
  98.  
  99. }
  100.  
  101. }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement