From 0423a5611758d5f732154fb4e0530b023940bc26 Mon Sep 17 00:00:00 2001 From: nutzma75019 Date: Tue, 20 Jun 2023 10:42:04 +0200 Subject: [PATCH] =?UTF-8?q?Senden=20der=20Ger=C3=A4te-IP-Adresse=20(in=20D?= =?UTF-8?q?evice=20Repository)=20Senden=20der=20zusammengesetzten=20Nachri?= =?UTF-8?q?cht=20vom=20Message=20String=20Builder=20-->=20Teile=20der=20al?= =?UTF-8?q?ten=20Testnachricht=20in=20WiFiCommunication=20entfernt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../communication/WiFiCommunication.java | 21 ++----------- .../repositories/DeviceRepository.java | 31 ++++++++++++++++++- 2 files changed, 33 insertions(+), 19 deletions(-) diff --git a/app/src/main/java/com/example/greenwatch/communication/WiFiCommunication.java b/app/src/main/java/com/example/greenwatch/communication/WiFiCommunication.java index 065bbdc..43cb10f 100644 --- a/app/src/main/java/com/example/greenwatch/communication/WiFiCommunication.java +++ b/app/src/main/java/com/example/greenwatch/communication/WiFiCommunication.java @@ -1,5 +1,7 @@ package com.example.greenwatch.communication; +import android.util.Log; + import com.example.greenwatch.repositories.DeviceRepository; import java.io.IOException; @@ -56,23 +58,6 @@ public class WiFiCommunication { this.isNewMessage = isNewMessage; } - public static String getLocalIpAddress() { - try { - for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { - NetworkInterface networkInterface = (NetworkInterface) ((Enumeration) en).nextElement(); - for (Enumeration 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 stopCommunication() { running = false; socket.close(); @@ -89,7 +74,7 @@ public class WiFiCommunication { { isNewMessage = false; //todo: adapt send String - String txString = getLocalIpAddress() + " sends. #" + tmpCnt++ + sendMessage; + String txString = sendMessage; byte[] txBuffer = txString.getBytes(); DatagramPacket txPacket = new DatagramPacket(txBuffer, txBuffer.length, address, port); diff --git a/app/src/main/java/com/example/greenwatch/repositories/DeviceRepository.java b/app/src/main/java/com/example/greenwatch/repositories/DeviceRepository.java index cf39c10..8530980 100644 --- a/app/src/main/java/com/example/greenwatch/repositories/DeviceRepository.java +++ b/app/src/main/java/com/example/greenwatch/repositories/DeviceRepository.java @@ -1,14 +1,21 @@ package com.example.greenwatch.repositories; +import android.util.Log; + import androidx.lifecycle.MutableLiveData; import com.example.greenwatch.models.Device; import com.example.greenwatch.communication.WiFiCommunication; +import java.net.Inet4Address; +import java.net.InetAddress; +import java.net.NetworkInterface; +import java.net.SocketException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; +import java.util.Enumeration; import java.util.HashMap; import java.util.List; import java.util.UUID; @@ -106,7 +113,8 @@ public class DeviceRepository { .append(delimiter) .append("Gruppe3") .append(delimiter) - .append(device.getDeviceID()) + //.append(device.getDeviceID()) + .append(getLocalIpAddress(deviceID)) .append(delimiter) .append(device.getSensorStatus()) .append(delimiter) @@ -329,4 +337,25 @@ public class DeviceRepository { } return false; } + + public String getLocalIpAddress(String deviceID) { + String checkedDeviceID = checkDeviceID(deviceID); + Device device = connectedDevicesList.get(checkedDeviceID); + if(device != null){ + try { + for (Enumeration en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) { + NetworkInterface networkInterface = (NetworkInterface) ((Enumeration) en).nextElement(); + for (Enumeration addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) { + InetAddress inetAddress = addresses.nextElement(); + if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) { + return inetAddress.getHostAddress(); + } + } + } + } catch (SocketException ex) { + ex.printStackTrace(); + } + } + return ""; + } }