|
|
@@ -0,0 +1,131 @@ |
|
|
|
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.text.SimpleDateFormat; |
|
|
|
import java.util.Date; |
|
|
|
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"); //100.82.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[] receiveData = new byte[512]; |
|
|
|
InetAddress fromAdress; |
|
|
|
int fromPort; |
|
|
|
DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length); |
|
|
|
socket.receive(rxPacket); |
|
|
|
fromAdress = rxPacket.getAddress(); |
|
|
|
fromPort = rxPacket.getPort(); |
|
|
|
rxString = new String(rxPacket.getData()); |
|
|
|
if(!previousRxString.equals(rxString)) { |
|
|
|
rxStringBuffer.append(rxString).append("\n"); |
|
|
|
mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer)); |
|
|
|
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; |
|
|
|
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); |
|
|
|
Date curDate = new Date(System.currentTimeMillis()); |
|
|
|
String str = formatter.format(curDate); |
|
|
|
byte[] send_Data = new byte[512]; |
|
|
|
String txString = (str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," +messageToSend); |
|
|
|
send_Data = txString.getBytes(); |
|
|
|
|
|
|
|
DatagramPacket txPacket = new DatagramPacket(send_Data, txString.length(), address, port); |
|
|
|
|
|
|
|
for(int i = 0; i < 500; i++) { |
|
|
|
socket.send(txPacket); |
|
|
|
/*try{ |
|
|
|
sleep(10); |
|
|
|
}catch (InterruptedException e){ |
|
|
|
e.printStackTrace(); |
|
|
|
}*/ |
|
|
|
} |
|
|
|
} |
|
|
|
} 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(); |
|
|
|
} |
|
|
|
} |