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 5.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. package com.example.ueberwachungssystem;
  2. import android.annotation.SuppressLint;
  3. import android.widget.Toast;
  4. import androidx.annotation.NonNull;
  5. import com.example.ueberwachungssystem.Detection.DetectionReport;
  6. import com.example.ueberwachungssystem.Detection.Detector;
  7. import com.example.ueberwachungssystem.Detection.DetectorService;
  8. import java.io.IOException;
  9. import java.net.DatagramPacket;
  10. import java.net.DatagramSocket;
  11. import java.net.Inet4Address;
  12. import java.net.InetAddress;
  13. import java.net.NetworkInterface;
  14. import java.net.SocketException;
  15. import java.net.UnknownHostException;
  16. import java.text.SimpleDateFormat;
  17. import java.util.Date;
  18. import java.util.Enumeration;
  19. public class WifiCommunication {
  20. //private final MainActivity mainActivity;
  21. private final InetAddress address;
  22. private final int port;
  23. private String messageToSend;
  24. volatile private boolean send;
  25. private final DatagramSocket socket;
  26. volatile private boolean running;
  27. private OnConnectionListener listener;
  28. @SuppressLint("SetTextI18n")
  29. public WifiCommunication(int port) {
  30. //this.mainActivity = mainActivity;
  31. this.port = port;
  32. try {
  33. socket = new DatagramSocket(this.port);
  34. socket.setBroadcast(true);
  35. address = InetAddress.getByName("255.255.255.255"); //100.82.255.255
  36. running = true;
  37. send = false;
  38. new ReceiveThread().start();
  39. new SendThread().start();
  40. } catch (SocketException | UnknownHostException e) {
  41. throw new RuntimeException(e);
  42. }
  43. //Toast.makeText(mainActivity.getApplicationContext(),"Communication running", Toast.LENGTH_SHORT).show();
  44. //mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText("Communication running"));
  45. }
  46. public interface OnConnectionListener {
  47. void onConnection(StringBuffer data);
  48. }
  49. public void setOnConnectionListener(@NonNull OnConnectionListener listener) {
  50. this.listener = listener;
  51. }
  52. public void sendWifiData(StringBuffer wifiMessage) {
  53. if (listener != null) {
  54. listener.onConnection(wifiMessage);
  55. }
  56. }
  57. private class ReceiveThread extends Thread {
  58. private StringBuffer rxStringBuffer = new StringBuffer();
  59. private String rxString="";
  60. private String previousRxString = "";
  61. @Override
  62. public void run() {
  63. try {
  64. do {
  65. byte[] receiveData = new byte[512];
  66. DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length);
  67. socket.receive(rxPacket);
  68. rxString = new String(receiveData, 0, rxPacket.getLength());
  69. String[] splitrxString = rxString.split(",");
  70. if(!previousRxString.equals(rxString) && splitrxString[0].equals("1") && splitrxString.length==7) {
  71. rxStringBuffer.append(rxString).append("\n");
  72. sendWifiData(rxStringBuffer);
  73. //mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
  74. previousRxString = rxString;
  75. }
  76. } while (running);
  77. }
  78. catch (IOException e) {
  79. e.printStackTrace();
  80. }
  81. }
  82. }
  83. private class SendThread extends Thread {
  84. private int tmpCnt = 0;
  85. @Override
  86. public void run() {
  87. try {
  88. do {
  89. if(send)
  90. {
  91. send = false;
  92. SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss");
  93. Date curDate = new Date(System.currentTimeMillis());
  94. String str = formatter.format(curDate);
  95. byte[] send_Data = new byte[512];
  96. String txString = ("1," +str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," +messageToSend);
  97. send_Data = txString.getBytes();
  98. DatagramPacket txPacket = new DatagramPacket(send_Data, txString.length(), address, port);
  99. for(int i = 0; i < 300; i++) {
  100. socket.send(txPacket);
  101. }
  102. }
  103. } while (running);
  104. } catch (IOException e) {
  105. e.printStackTrace();
  106. }
  107. }
  108. }
  109. public static String getLocalIpAddress() {
  110. try {
  111. for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
  112. NetworkInterface networkInterface = (NetworkInterface) ((Enumeration<?>) en).nextElement();
  113. for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) {
  114. InetAddress inetAddress = addresses.nextElement();
  115. if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
  116. return inetAddress.getHostAddress();
  117. }
  118. }
  119. }
  120. } catch (SocketException ex) {
  121. ex.printStackTrace();
  122. }
  123. return null;
  124. }
  125. public void sendTrue(String message){
  126. send = true;
  127. messageToSend = message;
  128. }
  129. public void stopCommunication() {
  130. running = false;
  131. socket.close();
  132. }
  133. }