Compare commits
17
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
b9c66f7533 | ||
|
|
7e1143880e | ||
|
|
81c3542654 | ||
|
|
d33ab299ab | ||
|
|
8a5d720685 | ||
|
|
90aea5b1bc | ||
|
|
8a357e0d26 | ||
|
|
83268c763b | ||
|
|
3d02d8d4c0 | ||
|
|
cafe763c4a | ||
|
|
2cff334056 | ||
|
|
6c4905d0e7 | ||
|
|
402b73e4bd | ||
|
|
9e5b669986 | ||
|
|
8275d8bd5a | ||
|
|
4999f1b998 | ||
|
|
620528302e |
No files matched your search
@@ -0,0 +1,3 @@
|
|||||||
|
Sensor als Service einrichten, der aus Activity heraus gestartet werden kann.
|
||||||
|
Stichwort: Intent
|
||||||
|
Siehe Skript Teil 1
|
||||||
@@ -26,9 +26,6 @@ android {
|
|||||||
sourceCompatibility JavaVersion.VERSION_1_8
|
sourceCompatibility JavaVersion.VERSION_1_8
|
||||||
targetCompatibility JavaVersion.VERSION_1_8
|
targetCompatibility JavaVersion.VERSION_1_8
|
||||||
}
|
}
|
||||||
buildFeatures {
|
|
||||||
viewBinding true
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
dependencies {
|
dependencies {
|
||||||
@@ -36,7 +33,6 @@ dependencies {
|
|||||||
implementation 'androidx.appcompat:appcompat:1.6.1'
|
implementation 'androidx.appcompat:appcompat:1.6.1'
|
||||||
implementation 'com.google.android.material:material:1.8.0'
|
implementation 'com.google.android.material:material:1.8.0'
|
||||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||||
implementation 'com.google.android.gms:play-services-nearby:18.0.0'
|
|
||||||
testImplementation 'junit:junit:4.13.2'
|
testImplementation 'junit:junit:4.13.2'
|
||||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||||
|
|||||||
@@ -0,0 +1,108 @@
|
|||||||
|
package com.example.ueberwachungssystem.Detection;
|
||||||
|
|
||||||
|
import static java.lang.Math.sqrt;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.hardware.Sensor;
|
||||||
|
import android.hardware.SensorEvent;
|
||||||
|
import android.hardware.SensorEventListener;
|
||||||
|
import android.hardware.SensorManager;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Accelerometer inherits some methods from abstract Detector class (more info there)
|
||||||
|
*
|
||||||
|
*
|
||||||
|
* USE FROM MAIN ACTIVITY:
|
||||||
|
*
|
||||||
|
* Accelerometer beschleunigungssensor = new Accelerometer(this);
|
||||||
|
* onCreate:
|
||||||
|
* //Accelerometer Setup
|
||||||
|
* beschleunigungssensor = new Accelerometer(this, logger, textViewLog); //logger and textview only for debugging necessary
|
||||||
|
* beschleunigungssensor.getSensor();
|
||||||
|
*
|
||||||
|
* //Starting Detection:
|
||||||
|
* beschleunigungssensor.startDetection();
|
||||||
|
* //Stopping Detection: also recommended at onPause to avoid unnecessary battery consumption
|
||||||
|
* beschleunigungssensor.stopDetection();
|
||||||
|
*
|
||||||
|
* */
|
||||||
|
|
||||||
|
public class Accelerometer extends Detector implements SensorEventListener {
|
||||||
|
|
||||||
|
public SensorManager sensorManager;
|
||||||
|
private static final int sensorType = Sensor.TYPE_LINEAR_ACCELERATION;
|
||||||
|
private Sensor accelerometer;
|
||||||
|
private Context context;
|
||||||
|
boolean alarm = false;
|
||||||
|
//Preallocate memory for the data of each axis of the acceleration sensor
|
||||||
|
float x;
|
||||||
|
float y;
|
||||||
|
float z;
|
||||||
|
float betrag; //Betrag aller drei Achsen sqrt(x*x + y*y + z*z)
|
||||||
|
private DetectionReport detectionReport;
|
||||||
|
|
||||||
|
// In constructor pass Activity, Context and TextView from MainActivity in Accelerometer class
|
||||||
|
public Accelerometer(Context context){
|
||||||
|
super(); //von Detektor
|
||||||
|
this.context = context;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void getSensor(){
|
||||||
|
sensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE);
|
||||||
|
if(sensorManager.getSensorList(sensorType).size()==0) {
|
||||||
|
accelerometer = null;
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
accelerometer = sensorManager.getSensorList(sensorType).get(0);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onSensorChanged(SensorEvent event) {
|
||||||
|
try {
|
||||||
|
checkAlarm(event);
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void checkAlarm (SensorEvent event) throws InterruptedException {
|
||||||
|
x = event.values[0];
|
||||||
|
y = event.values[1];
|
||||||
|
z = event.values[2];
|
||||||
|
betrag = (float) sqrt(x*x + y*y + z*z);
|
||||||
|
float threshold = 1.5F;
|
||||||
|
|
||||||
|
if (!alarm) {
|
||||||
|
if (betrag > threshold) {
|
||||||
|
alarm = true;
|
||||||
|
reportViolation("Bewegung", betrag);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if (betrag < threshold) {
|
||||||
|
alarm = false;
|
||||||
|
} else {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void startDetection() {
|
||||||
|
// entspricht void start()
|
||||||
|
//getSensor();
|
||||||
|
if (accelerometer != null) {
|
||||||
|
sensorManager.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void stopDetection() {
|
||||||
|
// entspricht void stop()
|
||||||
|
sensorManager.unregisterListener(this, accelerometer);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -3,17 +3,13 @@ package com.example.ueberwachungssystem.Detection;
|
|||||||
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
|
||||||
@@ -25,11 +21,6 @@ public class DetectorService extends LifecycleService {
|
|||||||
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) {
|
||||||
@@ -51,16 +42,9 @@ public class DetectorService extends LifecycleService {
|
|||||||
|
|
||||||
|
|
||||||
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();
|
||||||
@@ -156,6 +140,8 @@ public class DetectorService extends LifecycleService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/** 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) {
|
||||||
|
|||||||
@@ -1,113 +1,27 @@
|
|||||||
package com.example.ueberwachungssystem;
|
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;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
import android.view.View;
|
|
||||||
import android.widget.Button;
|
|
||||||
import android.widget.TextView;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import java.net.InetAddress;
|
|
||||||
import java.net.UnknownHostException;
|
|
||||||
import java.nio.ByteBuffer;
|
|
||||||
import java.nio.ByteOrder;
|
|
||||||
|
|
||||||
@SuppressLint("SetTextI18n")
|
import android.os.Bundle;
|
||||||
|
|
||||||
public class MainActivity extends AppCompatActivity {
|
public class MainActivity extends AppCompatActivity {
|
||||||
|
|
||||||
WifiCommunication communication;
|
WifiCommunication communication;
|
||||||
PermissionRequest permission;
|
|
||||||
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, 1234);
|
||||||
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();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onPause() {
|
protected void onPause() {
|
||||||
super.onPause();
|
super.onPause();
|
||||||
//communication.stopCommunication();
|
communication.stopCommunication();
|
||||||
}
|
|
||||||
public boolean onCreateOptionsMenu(Menu menu) {
|
|
||||||
getMenuInflater().inflate(R.menu.options_menu, menu);
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
public boolean onOptionsItemSelected(MenuItem item) {
|
|
||||||
Toast.makeText(this, "Selected Item: " + item.getTitle(), Toast.LENGTH_SHORT).show();
|
|
||||||
PopUpClass popUpClass;
|
|
||||||
switch (item.getItemId()) {
|
|
||||||
case R.id.Rechteverwaltung:
|
|
||||||
popUpClass = new PopUpClass(MainActivity.this);
|
|
||||||
//popUpClass.showPopupWindow(tvMessages);
|
|
||||||
popUpClass.RechtePrüfen();
|
|
||||||
return true;
|
|
||||||
case R.id.Sensoren:
|
|
||||||
popUpClass = new PopUpClass(MainActivity.this);
|
|
||||||
//popUpClass.showPopupWindow(tvMessages);
|
|
||||||
popUpClass.Sensoren();
|
|
||||||
return true;
|
|
||||||
case R.id.Impressum:
|
|
||||||
popUpClass = new PopUpClass(MainActivity.this);
|
|
||||||
//popUpClass.showPopupWindow(tvMessages);
|
|
||||||
popUpClass.Impressum();
|
|
||||||
return true;
|
|
||||||
default:
|
|
||||||
return super.onOptionsItemSelected(item);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
private String getLocalIpAddress() throws UnknownHostException {
|
|
||||||
WifiManager wifiManager = (WifiManager) getApplicationContext().getSystemService(WIFI_SERVICE);
|
|
||||||
assert wifiManager != null;
|
|
||||||
WifiInfo wifiInfo = wifiManager.getConnectionInfo();
|
|
||||||
int ipInt = wifiInfo.getIpAddress();
|
|
||||||
return InetAddress.getByAddress(ByteBuffer.allocate(4).order(ByteOrder.LITTLE_ENDIAN).putInt(ipInt).array()).getHostAddress();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
@@ -1,48 +0,0 @@
|
|||||||
package com.example.ueberwachungssystem;
|
|
||||||
|
|
||||||
import static android.Manifest.permission.INTERNET;
|
|
||||||
import static android.Manifest.permission.CAMERA;
|
|
||||||
import static android.Manifest.permission.RECORD_AUDIO;
|
|
||||||
|
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.os.Handler;
|
|
||||||
import android.widget.Toast;
|
|
||||||
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
|
||||||
import androidx.core.content.ContextCompat;
|
|
||||||
|
|
||||||
public class PermissionRequest extends AppCompatActivity{
|
|
||||||
private static final int PERMISSION_REQUEST_CODE = 123;
|
|
||||||
private final MainActivity mainActivity;
|
|
||||||
|
|
||||||
Handler handler = new Handler();
|
|
||||||
|
|
||||||
public PermissionRequest(MainActivity mainActivity) {
|
|
||||||
this.mainActivity = mainActivity;
|
|
||||||
}
|
|
||||||
|
|
||||||
public StringBuilder rechtePruefen() {
|
|
||||||
boolean rechtKamera = ContextCompat.checkSelfPermission(mainActivity, CAMERA) == PackageManager.PERMISSION_GRANTED;
|
|
||||||
boolean rechtMikrofon = ContextCompat.checkSelfPermission(mainActivity, RECORD_AUDIO) == PackageManager.PERMISSION_GRANTED;
|
|
||||||
boolean rechtInternet = ContextCompat.checkSelfPermission(mainActivity, INTERNET) == PackageManager.PERMISSION_GRANTED;
|
|
||||||
StringBuilder sb = new StringBuilder();
|
|
||||||
sb.append("Rechte prüfen:")
|
|
||||||
.append("\nKamera: ").append(rechtKamera)
|
|
||||||
.append("\nMikrofon: ").append(rechtMikrofon)
|
|
||||||
.append("\nInternet: ").append(rechtInternet);
|
|
||||||
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(sb));
|
|
||||||
if (!(rechtKamera && rechtMikrofon && rechtInternet)){
|
|
||||||
handler.post(new Runnable() {
|
|
||||||
@Override
|
|
||||||
public void run() {
|
|
||||||
Toast.makeText(mainActivity.getApplicationContext(),"Es werden Rechte benötigt", Toast.LENGTH_LONG).show();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
return sb;
|
|
||||||
}
|
|
||||||
|
|
||||||
public void rechteAnfordern() {
|
|
||||||
mainActivity.requestPermissions(new String[]{CAMERA, RECORD_AUDIO, INTERNET}, PERMISSION_REQUEST_CODE);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,86 +0,0 @@
|
|||||||
package com.example.ueberwachungssystem;
|
|
||||||
|
|
||||||
import android.view.Gravity;
|
|
||||||
import android.view.LayoutInflater;
|
|
||||||
import android.view.MotionEvent;
|
|
||||||
import android.view.View;
|
|
||||||
import android.widget.Button;
|
|
||||||
import android.widget.LinearLayout;
|
|
||||||
import android.widget.PopupWindow;
|
|
||||||
import android.widget.TextView;
|
|
||||||
|
|
||||||
public class PopUpClass {
|
|
||||||
private final MainActivity mainActivity;
|
|
||||||
|
|
||||||
PermissionRequest permission;
|
|
||||||
TextView PopUpText;
|
|
||||||
|
|
||||||
|
|
||||||
public PopUpClass(MainActivity mainActivity) {
|
|
||||||
this.mainActivity = mainActivity;
|
|
||||||
permission = new PermissionRequest(mainActivity);
|
|
||||||
}
|
|
||||||
|
|
||||||
//PopupWindow display method
|
|
||||||
|
|
||||||
public void showPopupWindow(final View view) {
|
|
||||||
|
|
||||||
|
|
||||||
//Create a View object yourself through inflater
|
|
||||||
LayoutInflater inflater = (LayoutInflater) view.getContext().getSystemService(view.getContext().LAYOUT_INFLATER_SERVICE);
|
|
||||||
View popupView = inflater.inflate(R.layout.popup_window, null);
|
|
||||||
|
|
||||||
//Specify the length and width through constants
|
|
||||||
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
|
|
||||||
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
|
|
||||||
|
|
||||||
//Make Inactive Items Outside Of PopupWindow
|
|
||||||
boolean focusable = true;
|
|
||||||
|
|
||||||
//Create a window with our parameters
|
|
||||||
final PopupWindow popupWindow = new PopupWindow(popupView,width*1, height*1, focusable);
|
|
||||||
|
|
||||||
//Set the location of the window on the screen
|
|
||||||
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
|
|
||||||
|
|
||||||
//Initialize the elements of our window, install the handler
|
|
||||||
|
|
||||||
PopUpText = popupView.findViewById(R.id.titleText);
|
|
||||||
|
|
||||||
Button buttonEdit = popupView.findViewById(R.id.RechteAnfordern);
|
|
||||||
buttonEdit.setOnClickListener(new View.OnClickListener() {
|
|
||||||
@Override
|
|
||||||
public void onClick(View v) {
|
|
||||||
RechteAnfordern();
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
//Handler for clicking on the inactive zone of the window
|
|
||||||
|
|
||||||
popupView.setOnTouchListener(new View.OnTouchListener() {
|
|
||||||
@Override
|
|
||||||
public boolean onTouch(View v, MotionEvent event) {
|
|
||||||
|
|
||||||
//Close the window when clicked
|
|
||||||
popupWindow.dismiss();
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
public void RechtePrüfen(){
|
|
||||||
StringBuilder Text = permission.rechtePruefen();
|
|
||||||
PopUpText.setText(Text);
|
|
||||||
}
|
|
||||||
public void RechteAnfordern(){
|
|
||||||
permission.rechteAnfordern();
|
|
||||||
StringBuilder Text = permission.rechtePruefen();
|
|
||||||
PopUpText.setText(Text);
|
|
||||||
}
|
|
||||||
public void Sensoren(){
|
|
||||||
PopUpText.setText("Es können 3 verschiedene Sensoren verwendet werden \n -1. Beschleunigungssensor\n -2. Mikrofon\n -3. Kamera");
|
|
||||||
}
|
|
||||||
public void Impressum(){
|
|
||||||
PopUpText.setText("Die Ueberwachungsapp wurde im Rahmen eines Praktikums der TH-Nürnberg programmiert");
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -1,12 +1,5 @@
|
|||||||
package com.example.ueberwachungssystem;
|
package com.example.ueberwachungssystem;
|
||||||
import android.annotation.SuppressLint;
|
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.io.IOException;
|
||||||
import java.net.DatagramPacket;
|
import java.net.DatagramPacket;
|
||||||
@@ -21,7 +14,7 @@ import java.util.Date;
|
|||||||
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;
|
||||||
|
|
||||||
@@ -29,11 +22,9 @@ public class WifiCommunication {
|
|||||||
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(int port) {
|
public WifiCommunication(MainActivity mainActivity, int port) {
|
||||||
//this.mainActivity = mainActivity;
|
this.mainActivity = mainActivity;
|
||||||
this.port = port;
|
this.port = port;
|
||||||
try {
|
try {
|
||||||
socket = new DatagramSocket(this.port);
|
socket = new DatagramSocket(this.port);
|
||||||
@@ -46,23 +37,8 @@ public class WifiCommunication {
|
|||||||
} 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();
|
|
||||||
//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="";
|
||||||
@@ -73,16 +49,20 @@ public class WifiCommunication {
|
|||||||
try {
|
try {
|
||||||
do {
|
do {
|
||||||
byte[] receiveData = new byte[512];
|
byte[] receiveData = new byte[512];
|
||||||
|
InetAddress fromAdress;
|
||||||
|
int fromPort;
|
||||||
DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length);
|
DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length);
|
||||||
socket.receive(rxPacket);
|
socket.receive(rxPacket);
|
||||||
rxString = new String(receiveData, 0, rxPacket.getLength());
|
fromAdress = rxPacket.getAddress();
|
||||||
String[] splitrxString = rxString.split(",");
|
fromPort = rxPacket.getPort();
|
||||||
if(!previousRxString.equals(rxString) && splitrxString[0].equals("1") && splitrxString.length==7) {
|
rxString = new String(rxPacket.getData());
|
||||||
|
if(!previousRxString.equals(rxString)) {
|
||||||
rxStringBuffer.append(rxString).append("\n");
|
rxStringBuffer.append(rxString).append("\n");
|
||||||
sendWifiData(rxStringBuffer);
|
|
||||||
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
|
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
|
||||||
previousRxString = rxString;
|
previousRxString = rxString;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
} while (running);
|
} while (running);
|
||||||
}
|
}
|
||||||
catch (IOException e) {
|
catch (IOException e) {
|
||||||
@@ -103,13 +83,18 @@ public class WifiCommunication {
|
|||||||
Date curDate = new Date(System.currentTimeMillis());
|
Date curDate = new Date(System.currentTimeMillis());
|
||||||
String str = formatter.format(curDate);
|
String str = formatter.format(curDate);
|
||||||
byte[] send_Data = new byte[512];
|
byte[] send_Data = new byte[512];
|
||||||
String txString = ("1," +str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," +messageToSend);
|
String txString = (str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," +messageToSend);
|
||||||
send_Data = txString.getBytes();
|
send_Data = txString.getBytes();
|
||||||
|
|
||||||
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 < 300; i++) {
|
for(int i = 0; i < 500; i++) {
|
||||||
socket.send(txPacket);
|
socket.send(txPacket);
|
||||||
|
/*try{
|
||||||
|
sleep(10);
|
||||||
|
}catch (InterruptedException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (running);
|
} while (running);
|
||||||
|
|||||||
@@ -38,16 +38,4 @@
|
|||||||
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>
|
||||||
@@ -1,29 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
|
||||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:paddingLeft="10dp"
|
|
||||||
android:paddingRight="10dp"
|
|
||||||
android:gravity="center"
|
|
||||||
android:background="#A5ACB2">
|
|
||||||
|
|
||||||
<TextView
|
|
||||||
android:id="@+id/titleText"
|
|
||||||
android:layout_gravity="center"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:textSize="20sp"
|
|
||||||
android:textStyle="italic"
|
|
||||||
android:padding="10dp"/>
|
|
||||||
|
|
||||||
<Button
|
|
||||||
android:id="@+id/RechteAnfordern"
|
|
||||||
android:layout_width="wrap_content"
|
|
||||||
android:layout_height="wrap_content"
|
|
||||||
android:layout_weight="1"
|
|
||||||
android:text="Rechte Anfordern" />
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
</LinearLayout>
|
|
||||||
@@ -1,9 +0,0 @@
|
|||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
|
|
||||||
<item android:id="@+id/Rechteverwaltung"
|
|
||||||
android:title="Rechteverwaltung" />
|
|
||||||
<item android:id="@+id/Sensoren"
|
|
||||||
android:title="Sensoren" />
|
|
||||||
<item android:id="@+id/Impressum"
|
|
||||||
android:title="Impressum" />
|
|
||||||
</menu>
|
|
||||||
+2
-2
@@ -1,5 +1,5 @@
|
|||||||
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
// Top-level build file where you can add configuration options common to all sub-projects/modules.
|
||||||
plugins {
|
plugins {
|
||||||
id 'com.android.application' version '7.4.2' apply false
|
id 'com.android.application' version '8.0.0' apply false
|
||||||
id 'com.android.library' version '7.4.2' apply false
|
id 'com.android.library' version '8.0.0' apply false
|
||||||
}
|
}
|
||||||
@@ -19,3 +19,5 @@ android.useAndroidX=true
|
|||||||
# resources declared in the library itself and none from the library's dependencies,
|
# resources declared in the library itself and none from the library's dependencies,
|
||||||
# thereby reducing the size of the R class for that library
|
# thereby reducing the size of the R class for that library
|
||||||
android.nonTransitiveRClass=true
|
android.nonTransitiveRClass=true
|
||||||
|
android.defaults.buildfeatures.buildconfig=true
|
||||||
|
android.nonFinalResIds=false
|
||||||
+1
-1
@@ -1,6 +1,6 @@
|
|||||||
#Thu May 11 15:04:30 CEST 2023
|
#Thu May 11 15:04:30 CEST 2023
|
||||||
distributionBase=GRADLE_USER_HOME
|
distributionBase=GRADLE_USER_HOME
|
||||||
distributionUrl=https\://services.gradle.org/distributions/gradle-7.5-bin.zip
|
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0-bin.zip
|
||||||
distributionPath=wrapper/dists
|
distributionPath=wrapper/dists
|
||||||
zipStorePath=wrapper/dists
|
zipStorePath=wrapper/dists
|
||||||
zipStoreBase=GRADLE_USER_HOME
|
zipStoreBase=GRADLE_USER_HOME
|
||||||
@@ -14,4 +14,3 @@ dependencyResolutionManagement {
|
|||||||
}
|
}
|
||||||
rootProject.name = "Ueberwachungssystem"
|
rootProject.name = "Ueberwachungssystem"
|
||||||
include ':app'
|
include ':app'
|
||||||
include ':app'
|
|
||||||
Reference in new issue
Block a user