123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119 |
- package com.example.ueberwachungssystem;
- import android.annotation.SuppressLint;
-
- import java.io.IOException;
- import java.net.DatagramPacket;
- import java.net.DatagramSocket;
- import java.net.Inet4Address;
- import java.net.InetAddress;
- import java.net.NetworkInterface;
- import java.net.SocketException;
- import java.net.UnknownHostException;
- import java.util.Enumeration;
-
- public class WifiCommunication {
- private final MainActivity mainActivity;
- private final InetAddress address;
- private final int port;
-
- private String messageToSend;
- volatile private boolean send;
- private final DatagramSocket socket;
- volatile private boolean running;
- @SuppressLint("SetTextI18n")
- public WifiCommunication(MainActivity mainActivity, int port) {
- this.mainActivity = mainActivity;
- this.port = port;
- try {
- socket = new DatagramSocket(this.port);
- socket.setBroadcast(true);
- address = InetAddress.getByName("255.255.255.255");
- running = true;
- send = false;
- new ReceiveThread().start();
- new SendThread().start();
- } catch (SocketException | UnknownHostException e) {
- throw new RuntimeException(e);
- }
- mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText("Communication running"));
- }
- private class ReceiveThread extends Thread {
- private StringBuffer rxStringBuffer = new StringBuffer();
- private String rxString;
- private String previousRxString = "";
- @Override
- public void run() {
- try {
- do {
- byte[] rxBuffer = new byte[512];
- InetAddress fromAdress;
- int fromPort;
- DatagramPacket rxPacket = new DatagramPacket(rxBuffer, rxBuffer.length);
- socket.receive(rxPacket);
- fromAdress = rxPacket.getAddress();
- fromPort = rxPacket.getPort();
- rxString = new String(rxBuffer, 0, rxPacket.getLength());
- if(!previousRxString.equals(rxString)) {
- mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
- rxStringBuffer.append("Adress: " + fromAdress).append(" Port: " + fromPort).append(" Message " +rxString).append("\n");
- }
- previousRxString = rxString;
-
- } while (running);
- }
- catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- private class SendThread extends Thread {
- private int tmpCnt = 0;
- @Override
- public void run() {
- try {
- do {
- if(send)
- {
- send = false;
-
- String txString = getLocalIpAddress() + " sends:" +messageToSend+ " Count: " + tmpCnt++;
- byte[] txBuffer = txString.getBytes();
-
- DatagramPacket txPacket = new DatagramPacket(txBuffer, txBuffer.length, address, port);
-
- for(int i = 0; i < 20; i++) {
- socket.send(txPacket);
- }
- }
- } while (running);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- }
- public static String getLocalIpAddress() {
- try {
- for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
- NetworkInterface networkInterface = (NetworkInterface) ((Enumeration<?>) en).nextElement();
- for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) {
- InetAddress inetAddress = addresses.nextElement();
- if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
- return inetAddress.getHostAddress();
- }
- }
- }
- } catch (SocketException ex) {
- ex.printStackTrace();
- }
- return null;
- }
-
- public void sendTrue(String message){
- send = true;
- messageToSend = message;
- }
- public void stopCommunication() {
- running = false;
- socket.close();
- }
- }
|