Browse Source

Kommunikation Version 1.5 UDP Socket

Kommunikation
Miguel Siebenhaar 1 year ago
parent
commit
a85fb1f5ca

+ 65
- 2
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

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

import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.content.DialogInterface;
import android.content.pm.PackageManager;
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.widget.Button; import android.widget.Button;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast; import android.widget.Toast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity; import androidx.appcompat.app.AppCompatActivity;
import java.net.InetAddress; import java.net.InetAddress;
import java.net.UnknownHostException; import java.net.UnknownHostException;
public class MainActivity extends AppCompatActivity { public class MainActivity extends AppCompatActivity {
TextView tvMessages; TextView tvMessages;
TextView tvConnectionInfos; TextView tvConnectionInfos;

WifiCommunication communication; WifiCommunication communication;
PermissionRequest permission; PermissionRequest permission;
public static String SERVER_IP = ""; public static String SERVER_IP = "";
public static final int SERVER_PORT = 1234; public static final int SERVER_PORT = 1234;
//private static final int PERMISSION_REQUEST_CODE = 123;
float i=0; float i=0;


boolean communicationRunning = false;

@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
} }
}); });
} }

/*public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE && grantResults.length > 0) {
boolean rechtKamera = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean rechtMikrofon = grantResults[1] == PackageManager.PERMISSION_GRANTED;
boolean rechtInternet = grantResults[2] == PackageManager.PERMISSION_GRANTED;

if (rechtKamera && rechtMikrofon && rechtInternet) {

Toast.makeText(getApplicationContext(), "Alle benötigten Rechte sind gewährt.", Toast.LENGTH_LONG).show();


//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText("Alle benötigten Rechte sind gewährt"));
}
else {
if (!rechtKamera) {
Toast.makeText(getApplicationContext(),"Kein Recht auf Kamera gewährt.", Toast.LENGTH_SHORT).show();

if (shouldShowRequestPermissionRationale(CAMERA)) {
zeigeDialog("Du musst uns Zugriff auf deine Kamera gestatten, damit deine Umgebung Ueberwacht werden kann.");
}
}
if (!rechtMikrofon) {
Toast.makeText(getApplicationContext(),"Kein Recht auf Mikrofon gewährt.", Toast.LENGTH_SHORT).show();

if (shouldShowRequestPermissionRationale(RECORD_AUDIO)) {
zeigeDialog("Du musst uns Zugriff auf dein Mikrofon gestatten, damit die Geraeuschueberwachung funktioniert.");
}
}
if (!rechtInternet) {
Toast.makeText(getApplicationContext(),"Kein Recht auf Internet.", Toast.LENGTH_SHORT).show();

if (shouldShowRequestPermissionRationale(INTERNET)) {
zeigeDialog("Du musst uns Zugriff auf dein Internet gestatten, damit wir eine Verbindung aufbauen können.");
}
}
}
}
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
private void zeigeDialog(String begruendung) {
new AlertDialog.Builder(this)
.setMessage(begruendung)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
requestPermissions(new String[]{CAMERA, RECORD_AUDIO, INTERNET}, PERMISSION_REQUEST_CODE);
}
})
.setNegativeButton("Abbruch", null)
.create()
.show();
}*/
public boolean onCreateOptionsMenu(Menu menu) { public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.options_menu, menu); getMenuInflater().inflate(R.menu.options_menu, menu);
return true; return true;


protected void onResume() { protected void onResume() {
super.onResume(); super.onResume();
communication = new WifiCommunication(MainActivity.this, SERVER_PORT);
if (!communicationRunning){
communication = new WifiCommunication(MainActivity.this, SERVER_PORT);
communicationRunning = true;
}
permission = new PermissionRequest(MainActivity.this); permission = new PermissionRequest(MainActivity.this);
permission.rechtePruefen(); permission.rechtePruefen();
} }

+ 6
- 14
app/src/main/java/com/example/ueberwachungssystem/PermissionRequest.java View File

public class PermissionRequest extends AppCompatActivity{ public class PermissionRequest extends AppCompatActivity{
private static final int PERMISSION_REQUEST_CODE = 123; private static final int PERMISSION_REQUEST_CODE = 123;
private final MainActivity mainActivity; private final MainActivity mainActivity;

boolean prüfen= false;

Handler handler = new Handler(); Handler handler = new Handler();

public PermissionRequest(MainActivity mainActivity) { public PermissionRequest(MainActivity mainActivity) {
this.mainActivity = mainActivity; this.mainActivity = mainActivity;
} }
} }


public void rechteAnfordern() { public void rechteAnfordern() {
/*handler.post(new Runnable() {
@Override
public void run() {
Toast.makeText(mainActivity.getApplicationContext(),"Fordere Rechte an", Toast.LENGTH_LONG).show();
}
});*/
mainActivity.requestPermissions(new String[]{CAMERA, RECORD_AUDIO, INTERNET}, PERMISSION_REQUEST_CODE); mainActivity.requestPermissions(new String[]{CAMERA, RECORD_AUDIO, INTERNET}, PERMISSION_REQUEST_CODE);
} }


@Override
/*@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) { public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_CODE && grantResults.length > 0) { if (requestCode == PERMISSION_REQUEST_CODE && grantResults.length > 0) {
boolean rechtKamera = grantResults[0] == PackageManager.PERMISSION_GRANTED; boolean rechtKamera = grantResults[0] == PackageManager.PERMISSION_GRANTED;
boolean rechtMikrofon = grantResults[1] == PackageManager.PERMISSION_GRANTED; boolean rechtMikrofon = grantResults[1] == PackageManager.PERMISSION_GRANTED;
boolean rechtInternet = grantResults[2] == PackageManager.PERMISSION_GRANTED; boolean rechtInternet = grantResults[2] == PackageManager.PERMISSION_GRANTED;


if (rechtKamera && rechtMikrofon && rechtInternet)
if (rechtKamera && rechtMikrofon && rechtInternet) {
handler.post(new Runnable() { handler.post(new Runnable() {
@Override @Override
public void run() { public void run() {
Toast.makeText(mainActivity.getApplicationContext(),"Alle benötigten Rechte sind gewährt.", Toast.LENGTH_LONG).show();
Toast.makeText(mainActivity.getApplicationContext(), "Alle benötigten Rechte sind gewährt.", Toast.LENGTH_LONG).show();
} }
}); });
mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText("Alle benötigten Rechte sind gewährt"));
}
else { else {
if (!rechtKamera) { if (!rechtKamera) {
handler.post(new Runnable() { handler.post(new Runnable() {
.setNegativeButton("Abbruch", null) .setNegativeButton("Abbruch", null)
.create() .create()
.show(); .show();
}
}*/
} }

+ 0
- 1
app/src/main/res/layout/popup_window.xml View File

android:layout_gravity="center" android:layout_gravity="center"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:text="Aktualisieren"
android:textSize="20sp" android:textSize="20sp"
android:textStyle="italic" android:textStyle="italic"
android:padding="10dp"/> android:padding="10dp"/>

Loading…
Cancel
Save