Browse Source

Senden der Geräte-IP-Adresse (in Device Repository)

Senden der zusammengesetzten Nachricht vom Message String Builder --> Teile der alten Testnachricht in WiFiCommunication entfernt
DoNotMergeAddCommunicationStrigDependencies
Maria Nutz 1 year ago
parent
commit
0423a56117

+ 3
- 18
app/src/main/java/com/example/greenwatch/communication/WiFiCommunication.java View File

@@ -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<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 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);

+ 30
- 1
app/src/main/java/com/example/greenwatch/repositories/DeviceRepository.java View File

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

Loading…
Cancel
Save