Gruppe 1
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

WifiCommunication.java 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. package com.example.ueberwachungssystem;
  2. import android.annotation.SuppressLint;
  3. import java.io.IOException;
  4. import java.net.DatagramPacket;
  5. import java.net.DatagramSocket;
  6. import java.net.Inet4Address;
  7. import java.net.InetAddress;
  8. import java.net.NetworkInterface;
  9. import java.net.SocketException;
  10. import java.net.UnknownHostException;
  11. import java.util.Enumeration;
  12. public class WifiCommunication {
  13. private final MainActivity mainActivity;
  14. private final InetAddress address;
  15. private final int port;
  16. private String messageToSend;
  17. volatile private boolean send;
  18. private final DatagramSocket socket;
  19. volatile private boolean running;
  20. @SuppressLint("SetTextI18n")
  21. public WifiCommunication(MainActivity mainActivity, int port) {
  22. this.mainActivity = mainActivity;
  23. this.port = port;
  24. try {
  25. socket = new DatagramSocket(this.port);
  26. socket.setBroadcast(true);
  27. address = InetAddress.getByName("255.255.255.255");
  28. running = true;
  29. send = false;
  30. new ReceiveThread().start();
  31. new SendThread().start();
  32. } catch (SocketException | UnknownHostException e) {
  33. throw new RuntimeException(e);
  34. }
  35. mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText("Communication running"));
  36. }
  37. private class ReceiveThread extends Thread {
  38. private StringBuffer rxStringBuffer = new StringBuffer();
  39. private String rxString;
  40. private String previousRxString = "";
  41. @Override
  42. public void run() {
  43. try {
  44. do {
  45. byte[] rxBuffer = new byte[512];
  46. InetAddress fromAdress;
  47. int fromPort;
  48. DatagramPacket rxPacket = new DatagramPacket(rxBuffer, rxBuffer.length);
  49. socket.receive(rxPacket);
  50. fromAdress = rxPacket.getAddress();
  51. fromPort = rxPacket.getPort();
  52. rxString = new String(rxBuffer, 0, rxPacket.getLength());
  53. if(!previousRxString.equals(rxString)) {
  54. mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
  55. rxStringBuffer.append("Adress: " + fromAdress).append(" Port: " + fromPort).append(" Message " +rxString).append("\n");
  56. }
  57. previousRxString = rxString;
  58. } while (running);
  59. }
  60. catch (IOException e) {
  61. e.printStackTrace();
  62. }
  63. }
  64. }
  65. private class SendThread extends Thread {
  66. private int tmpCnt = 0;
  67. @Override
  68. public void run() {
  69. try {
  70. do {
  71. if(send)
  72. {
  73. send = false;
  74. String txString = getLocalIpAddress() + " sends:" +messageToSend+ " Count: " + tmpCnt++;
  75. byte[] txBuffer = txString.getBytes();
  76. DatagramPacket txPacket = new DatagramPacket(txBuffer, txBuffer.length, address, port);
  77. for(int i = 0; i < 20; i++) {
  78. socket.send(txPacket);
  79. }
  80. }
  81. } while (running);
  82. } catch (IOException e) {
  83. e.printStackTrace();
  84. }
  85. }
  86. }
  87. public static String getLocalIpAddress() {
  88. try {
  89. for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
  90. NetworkInterface networkInterface = (NetworkInterface) ((Enumeration<?>) en).nextElement();
  91. for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) {
  92. InetAddress inetAddress = addresses.nextElement();
  93. if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
  94. return inetAddress.getHostAddress();
  95. }
  96. }
  97. }
  98. } catch (SocketException ex) {
  99. ex.printStackTrace();
  100. }
  101. return null;
  102. }
  103. public void sendTrue(String message){
  104. send = true;
  105. messageToSend = message;
  106. }
  107. public void stopCommunication() {
  108. running = false;
  109. socket.close();
  110. }
  111. }