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

import android.content.Intent; import android.content.Intent;
import android.os.Binder; import android.os.Binder;
import android.os.IBinder; import android.os.IBinder;
import android.util.Log;
import android.widget.ImageView; import android.widget.ImageView;
import android.widget.Toast;


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


import com.example.ueberwachungssystem.WifiCommunication;

import java.io.File; import java.io.File;


@ExperimentalGetImage @ExperimentalGetImage
VideoDetector videoDetector = null; VideoDetector videoDetector = null;
AudioRecorder audioRecorder = null; AudioRecorder audioRecorder = null;


/** Communication **/

WifiCommunication wifiCommunication;

StringBuffer dataFromWifi;


@Override @Override
public int onStartCommand(Intent intent, int flags, int startId) { public int onStartCommand(Intent intent, int flags, int startId) {




isServiceRunning = true; 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); return super.onStartCommand(intent, flags, startId);
} }

@Override @Override
public void onDestroy() { public void onDestroy() {
super.onDestroy(); super.onDestroy();
} }






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

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

package com.example.ueberwachungssystem; package com.example.ueberwachungssystem;


import static android.content.ContentValues.TAG;

import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.net.wifi.WifiInfo; import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.net.wifi.WifiInfo; import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager; import android.net.wifi.WifiManager;
import android.os.Bundle; import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem; import android.view.MenuItem;
import android.widget.Toast; import android.widget.Toast;
import android.view.Menu; import android.view.Menu;


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


@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); 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 @Override
protected void onResume() { protected void onResume() {
super.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 = new PermissionRequest(MainActivity.this);
permission.rechtePruefen(); permission.rechtePruefen();
switch (item.getItemId()) { switch (item.getItemId()) {
case R.id.Rechteverwaltung: case R.id.Rechteverwaltung:
popUpClass = new PopUpClass(MainActivity.this); popUpClass = new PopUpClass(MainActivity.this);
popUpClass.showPopupWindow(tvMessages);
//popUpClass.showPopupWindow(tvMessages);
popUpClass.RechtePrüfen(); popUpClass.RechtePrüfen();
return true; return true;
case R.id.Sensoren: case R.id.Sensoren:
popUpClass = new PopUpClass(MainActivity.this); popUpClass = new PopUpClass(MainActivity.this);
popUpClass.showPopupWindow(tvMessages);
//popUpClass.showPopupWindow(tvMessages);
popUpClass.Sensoren(); popUpClass.Sensoren();
return true; return true;
case R.id.Impressum: case R.id.Impressum:
popUpClass = new PopUpClass(MainActivity.this); popUpClass = new PopUpClass(MainActivity.this);
popUpClass.showPopupWindow(tvMessages);
//popUpClass.showPopupWindow(tvMessages);
popUpClass.Impressum(); popUpClass.Impressum();
return true; return true;
default: default:

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

import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.widget.Toast; 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.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
import java.net.DatagramSocket; import java.net.DatagramSocket;
import java.util.Enumeration; import java.util.Enumeration;


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


volatile private boolean send; volatile private boolean send;
private final DatagramSocket socket; private final DatagramSocket socket;
volatile private boolean running; volatile private boolean running;

private OnConnectionListener listener;
@SuppressLint("SetTextI18n") @SuppressLint("SetTextI18n")
public WifiCommunication(MainActivity mainActivity, int port) {
this.mainActivity = mainActivity;
public WifiCommunication(int port) {
//this.mainActivity = mainActivity;
this.port = port; this.port = port;
try { try {
socket = new DatagramSocket(this.port); socket = new DatagramSocket(this.port);
} catch (SocketException | UnknownHostException e) { } catch (SocketException | UnknownHostException e) {
throw new RuntimeException(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")); //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 class ReceiveThread extends Thread {
private StringBuffer rxStringBuffer = new StringBuffer(); private StringBuffer rxStringBuffer = new StringBuffer();
private String rxString=""; private String rxString="";
byte[] receiveData = new byte[512]; byte[] receiveData = new byte[512];
DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length); DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(rxPacket); socket.receive(rxPacket);
rxString = new String(rxPacket.getData());
rxString = new String(receiveData, 0, rxPacket.getLength());
String[] splitrxString = rxString.split(","); String[] splitrxString = rxString.split(",");
if(!previousRxString.equals(rxString) && splitrxString[0].equals("1") && splitrxString.length==7) { if(!previousRxString.equals(rxString) && splitrxString[0].equals("1") && splitrxString.length==7) {
rxStringBuffer.append(rxString).append("\n"); rxStringBuffer.append(rxString).append("\n");
mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
sendWifiData(rxStringBuffer);
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
previousRxString = rxString; previousRxString = rxString;
} }
} while (running); } while (running);


DatagramPacket txPacket = new DatagramPacket(send_Data, txString.length(), address, port); 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); socket.send(txPacket);
} }
} }

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

android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="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> </LinearLayout>

Loading…
Cancel
Save