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

package com.example.greenwatch.communication; package com.example.greenwatch.communication;


import android.util.Log;

import com.example.greenwatch.repositories.DeviceRepository; import com.example.greenwatch.repositories.DeviceRepository;


import java.io.IOException; import java.io.IOException;
this.isNewMessage = isNewMessage; 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() { public void stopCommunication() {
running = false; running = false;
socket.close(); socket.close();
{ {
isNewMessage = false; isNewMessage = false;
//todo: adapt send String //todo: adapt send String
String txString = getLocalIpAddress() + " sends. #" + tmpCnt++ + sendMessage;
String txString = sendMessage;
byte[] txBuffer = txString.getBytes(); byte[] txBuffer = txString.getBytes();


DatagramPacket txPacket = new DatagramPacket(txBuffer, txBuffer.length, address, port); DatagramPacket txPacket = new DatagramPacket(txBuffer, txBuffer.length, address, port);

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

package com.example.greenwatch.repositories; package com.example.greenwatch.repositories;




import android.util.Log;

import androidx.lifecycle.MutableLiveData; import androidx.lifecycle.MutableLiveData;


import com.example.greenwatch.models.Device; import com.example.greenwatch.models.Device;
import com.example.greenwatch.communication.WiFiCommunication; 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.text.SimpleDateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Date; import java.util.Date;
import java.util.Enumeration;
import java.util.HashMap; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.UUID; import java.util.UUID;
.append(delimiter) .append(delimiter)
.append("Gruppe3") .append("Gruppe3")
.append(delimiter) .append(delimiter)
.append(device.getDeviceID())
//.append(device.getDeviceID())
.append(getLocalIpAddress(deviceID))
.append(delimiter) .append(delimiter)
.append(device.getSensorStatus()) .append(device.getSensorStatus())
.append(delimiter) .append(delimiter)
} }
return false; 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