From 8e002107b5ebf54f852f0e7c36677384027d53ba Mon Sep 17 00:00:00 2001 From: Miguel Siebenhaar Date: Wed, 21 Jun 2023 15:06:57 +0200 Subject: [PATCH] Version 1. working --- .../Detection/DetectorService.java | 46 ++++++++++++++++--- .../ueberwachungssystem/MainActivity.java | 4 +- .../WifiCommunication.java | 14 ++++-- 3 files changed, 51 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java b/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java index 36e3b94..39da9b8 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java +++ b/app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java @@ -3,6 +3,7 @@ package com.example.ueberwachungssystem.Detection; import android.content.Intent; import android.os.Binder; import android.os.IBinder; +import android.util.Log; import android.widget.ImageView; import androidx.annotation.NonNull; @@ -10,6 +11,8 @@ import androidx.annotation.Nullable; import androidx.camera.core.ExperimentalGetImage; import androidx.lifecycle.LifecycleService; +import com.example.ueberwachungssystem.WifiCommunication; + import java.io.File; @ExperimentalGetImage @@ -24,12 +27,34 @@ public class DetectorService extends LifecycleService { public Accelerometer motionDetector = null; public MicrophoneDetector audioDetector = null; + public WifiCommunication wifiCommunication; + + String wifiData; + StringBuffer stringBufferWifi = new StringBuffer(); + @Override public int onStartCommand(Intent intent, int flags, int startId) { if (isServiceRunning) return START_NOT_STICKY; + /** Wifi Instanz **/ + wifiCommunication = new WifiCommunication(1234); + wifiCommunication.sendTrue("TEst"); + + wifiCommunication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() { + @Override + public void onConnection(String data) { + Log.d("Listener", data); + wifiData = data; + stringToStringbuffer(data); + Log.d("buffer",stringBufferWifi.toString()); + passToServiceListener(stringBufferWifi); + + } + }); + + @@ -38,7 +63,8 @@ public class DetectorService extends LifecycleService { videoDetector.setOnDetectionListener(new Detector.OnDetectionListener() { @Override public void onDetection(@NonNull DetectionReport detectionReport) { - passToServiceListener(detectionReport); + //passToServiceListener(detectionReport); + wifiCommunication.sendTrue(detectionReport.toMessage()); } }); /** Motion Detection**/ @@ -47,7 +73,8 @@ public class DetectorService extends LifecycleService { motionDetector.setOnDetectionListener(new Detector.OnDetectionListener() { @Override public void onDetection(@NonNull DetectionReport detectionReport) { - passToServiceListener(detectionReport); + //passToServiceListener(detectionReport); + wifiCommunication.sendTrue(detectionReport.toMessage()); } }); /** Audio Detection **/ @@ -55,7 +82,8 @@ public class DetectorService extends LifecycleService { audioDetector.setOnDetectionListener(new Detector.OnDetectionListener() { @Override public void onDetection(@NonNull DetectionReport detectionReport) { - passToServiceListener(detectionReport); + //passToServiceListener(detectionReport); + wifiCommunication.sendTrue(detectionReport.toMessage()); } }); /** Audio Recorder**/ @@ -92,18 +120,24 @@ public class DetectorService extends LifecycleService { /** Pass Detection Report to Service Detection Listener and trigger it */ - public void passToServiceListener(DetectionReport detectionReport) { + public void passToServiceListener(StringBuffer stringBuffer) { if (listener != null) { - listener.onDetection(detectionReport); + listener.onDetection(stringBuffer); } } /** On Detection Listener - runs when violation is reported */ public interface OnDetectionListener { - void onDetection(@NonNull DetectionReport detectionReport); + void onDetection(@NonNull StringBuffer stringBuffer); } public void setOnDetectionListener(@NonNull DetectorService.OnDetectionListener listener) { this.listener = listener; } + + public void stringToStringbuffer(String string){ + if(string != null) { + stringBufferWifi.append(string).append("\n"); + } + } } diff --git a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java index 24cdcb3..83617dd 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java +++ b/app/src/main/java/com/example/ueberwachungssystem/MainActivity.java @@ -103,8 +103,8 @@ public class MainActivity extends AppCompatActivity { detectorService.setOnDetectionListener(new DetectorService.OnDetectionListener() { @Override - public void onDetection(@NonNull DetectionReport detectionReport) { - Log.d("onDetection", detectionReport.toMessage()); + public void onDetection(@NonNull StringBuffer stringBuffer) { + Log.d("onDetection", stringBuffer.toString()); } }); } diff --git a/app/src/main/java/com/example/ueberwachungssystem/WifiCommunication.java b/app/src/main/java/com/example/ueberwachungssystem/WifiCommunication.java index d22acbf..2d70a12 100644 --- a/app/src/main/java/com/example/ueberwachungssystem/WifiCommunication.java +++ b/app/src/main/java/com/example/ueberwachungssystem/WifiCommunication.java @@ -1,5 +1,6 @@ package com.example.ueberwachungssystem; import android.annotation.SuppressLint; +import android.util.Log; import android.widget.Toast; import androidx.annotation.NonNull; @@ -51,15 +52,16 @@ public class WifiCommunication { } public interface OnConnectionListener { - void onConnection(StringBuffer data); + void onConnection(String data); } public void setOnConnectionListener(@NonNull OnConnectionListener listener) { this.listener = listener; } - public void sendWifiData(StringBuffer wifiMessage) { + public void sendWifiData(String wifiMessage) { if (listener != null) { listener.onConnection(wifiMessage); + } } @@ -78,8 +80,9 @@ public class WifiCommunication { rxString = new String(receiveData, 0, rxPacket.getLength()); String[] splitrxString = rxString.split(","); if(!previousRxString.equals(rxString) && splitrxString[0].equals("1") && splitrxString.length==7) { - rxStringBuffer.append(rxString).append("\n"); - sendWifiData(rxStringBuffer); + //rxStringBuffer.append(rxString).append("\n"); + Log.d("empfangen", rxString); + sendWifiData(rxString); //mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer)); previousRxString = rxString; } @@ -103,7 +106,8 @@ public class WifiCommunication { Date curDate = new Date(System.currentTimeMillis()); String str = formatter.format(curDate); byte[] send_Data = new byte[512]; - String txString = ("1," +str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," +messageToSend); + String txString = (messageToSend); //"1," +str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," + Log.d("send", txString); send_Data = txString.getBytes(); DatagramPacket txPacket = new DatagramPacket(send_Data, txString.length(), address, port);