Browse Source

Kommunikationcopie Version 1.8 UDP Socket

Kommunikationcopie
Miguel Siebenhaar 1 year ago
parent
commit
b667e70fef

+ 17
- 3
app/src/main/java/com/example/ueberwachungssystem/Detection/DetectorService.java View File

@@ -3,13 +3,17 @@ 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 android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.camera.core.ExperimentalGetImage;
import androidx.lifecycle.LifecycleService;

import com.example.ueberwachungssystem.WifiCommunication;

import java.io.File;

@ExperimentalGetImage
@@ -21,6 +25,11 @@ public class DetectorService extends LifecycleService {
VideoDetector videoDetector = null;
AudioRecorder audioRecorder = null;

/** Communication **/

WifiCommunication wifiCommunication;

StringBuffer dataFromWifi;

@Override
public int onStartCommand(Intent intent, int flags, int startId) {
@@ -42,9 +51,16 @@ public class DetectorService extends LifecycleService {


isServiceRunning = true;

wifiCommunication = new WifiCommunication (1234);
wifiCommunication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() {
@Override
public void onConnection(StringBuffer data) {
dataFromWifi = data;
}
});
return super.onStartCommand(intent, flags, startId);
}

@Override
public void onDestroy() {
super.onDestroy();
@@ -140,8 +156,6 @@ public class DetectorService extends LifecycleService {
}




/** pass Detection Report to Service Detection Listener and trigger it */
public void passToServiceListener(DetectionReport detectionReport) {
if (listener != null) {

+ 33
- 7
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

@@ -1,11 +1,14 @@
package com.example.ueberwachungssystem;

import static android.content.ContentValues.TAG;

import android.annotation.SuppressLint;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.Toast;
import android.view.Menu;
@@ -26,19 +29,42 @@ public class MainActivity extends AppCompatActivity {

WifiCommunication communication;
PermissionRequest permission;
boolean communicationRunning = false;
boolean communicationRunning = true;
String dataRecieved;
TextView textview1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textview1 = findViewById(R.id.textView1);

Button button1 = findViewById(R.id.buttonSend);
button1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
communication.sendTrue("Testmessage");
}
});
}
@Override
protected void onResume() {
super.onResume();
if (!communicationRunning){
communication = new WifiCommunication(MainActivity.this, SERVER_PORT);
communicationRunning = true;
if (communicationRunning){
communication = new WifiCommunication(1234);
communication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() {
@Override
public void onConnection(StringBuffer data) {
//Log.d("Test", data.toString());
runOnUiThread(new Runnable() {
@Override
public void run() {
textview1.setText(data);
}
});
}
});
communicationRunning = false;
}
permission = new PermissionRequest(MainActivity.this);
permission.rechtePruefen();
@@ -59,17 +85,17 @@ public class MainActivity extends AppCompatActivity {
switch (item.getItemId()) {
case R.id.Rechteverwaltung:
popUpClass = new PopUpClass(MainActivity.this);
popUpClass.showPopupWindow(tvMessages);
//popUpClass.showPopupWindow(tvMessages);
popUpClass.RechtePrüfen();
return true;
case R.id.Sensoren:
popUpClass = new PopUpClass(MainActivity.this);
popUpClass.showPopupWindow(tvMessages);
//popUpClass.showPopupWindow(tvMessages);
popUpClass.Sensoren();
return true;
case R.id.Impressum:
popUpClass = new PopUpClass(MainActivity.this);
popUpClass.showPopupWindow(tvMessages);
//popUpClass.showPopupWindow(tvMessages);
popUpClass.Impressum();
return true;
default:

+ 30
- 8
app/src/main/java/com/example/ueberwachungssystem/WifiCommunication.java View File

@@ -2,6 +2,12 @@ package com.example.ueberwachungssystem;
import android.annotation.SuppressLint;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.example.ueberwachungssystem.Detection.DetectionReport;
import com.example.ueberwachungssystem.Detection.Detector;
import com.example.ueberwachungssystem.Detection.DetectorService;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
@@ -15,7 +21,7 @@ import java.util.Date;
import java.util.Enumeration;

public class WifiCommunication {
private final MainActivity mainActivity;
//private final MainActivity mainActivity;
private final InetAddress address;
private final int port;

@@ -23,9 +29,11 @@ public class WifiCommunication {
volatile private boolean send;
private final DatagramSocket socket;
volatile private boolean running;

private OnConnectionListener listener;
@SuppressLint("SetTextI18n")
public WifiCommunication(MainActivity mainActivity, int port) {
this.mainActivity = mainActivity;
public WifiCommunication(int port) {
//this.mainActivity = mainActivity;
this.port = port;
try {
socket = new DatagramSocket(this.port);
@@ -38,10 +46,23 @@ public class WifiCommunication {
} catch (SocketException | UnknownHostException e) {
throw new RuntimeException(e);
}
Toast.makeText(mainActivity.getApplicationContext(),"Communication running", Toast.LENGTH_SHORT).show();

//Toast.makeText(mainActivity.getApplicationContext(),"Communication running", Toast.LENGTH_SHORT).show();
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText("Communication running"));
}

public interface OnConnectionListener {
void onConnection(StringBuffer data);
}
public void setOnConnectionListener(@NonNull OnConnectionListener listener) {
this.listener = listener;
}

public void sendWifiData(StringBuffer wifiMessage) {
if (listener != null) {
listener.onConnection(wifiMessage);
}
}

private class ReceiveThread extends Thread {
private StringBuffer rxStringBuffer = new StringBuffer();
private String rxString="";
@@ -54,11 +75,12 @@ public class WifiCommunication {
byte[] receiveData = new byte[512];
DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(rxPacket);
rxString = new String(rxPacket.getData());
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");
mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
sendWifiData(rxStringBuffer);
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
previousRxString = rxString;
}
} while (running);
@@ -86,7 +108,7 @@ public class WifiCommunication {

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

for(int i = 0; i < 500; i++) {
for(int i = 0; i < 300; i++) {
socket.send(txPacket);
}
}

+ 12
- 0
app/src/main/res/layout/activity_main.xml View File

@@ -38,4 +38,16 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="TextView" />

<Button
android:id="@+id/buttonSend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />

</LinearLayout>

Loading…
Cancel
Save