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 ""; + } }