Browse Source

Kommunikation Version 1.4 UDP Socket

Kommunikation
Miguel Siebenhaar 1 year ago
parent
commit
79c7c16d21

+ 3
- 1
app/src/main/AndroidManifest.xml View File

@@ -5,6 +5,8 @@
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" /> <!-- §permissionCamera§ -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />

<application
android:allowBackup="true"
@@ -19,7 +21,7 @@
<activity
android:name=".MainActivity"
android:exported="true"
android:label="Server">
android:label="Ueberwachungsapp">
<intent-filter>
<action android:name="android.intent.action.MAIN" />


+ 34
- 12
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

@@ -3,20 +3,14 @@ import android.annotation.SuppressLint;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.os.Bundle;
import android.provider.Settings;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.net.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
@@ -27,6 +21,7 @@ public class MainActivity extends AppCompatActivity {
TextView tvConnectionInfos;

WifiCommunication communication;
PermissionRequest permission;
public static String SERVER_IP = "";
public static final int SERVER_PORT = 1234;
float i=0;
@@ -43,7 +38,6 @@ public class MainActivity extends AppCompatActivity {
setContentView(R.layout.activity_main);
tvMessages = findViewById(R.id.tvMessages);
tvConnectionInfos = findViewById(R.id.tvConnectionInfos);
String deviceId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
try {
SERVER_IP = getLocalIpAddress();
tvConnectionInfos.setText("Connection Infos: \n Own IP-Adress: " + SERVER_IP+ " Port: " + SERVER_PORT);
@@ -59,11 +53,39 @@ public class MainActivity extends AppCompatActivity {
}
});
}
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);
}
}

protected void onResume() {
super.onResume();
communication = new WifiCommunication(MainActivity.this, SERVER_PORT);
//communication.sendTrue(" has Entered ");

permission = new PermissionRequest(MainActivity.this);
permission.rechtePruefen();
}
@Override
protected void onPause() {

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

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

import java.io.IOException;
import java.net.DatagramPacket;
@@ -37,7 +38,9 @@ public class WifiCommunication {
} catch (SocketException | UnknownHostException e) {
throw new RuntimeException(e);
}
mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText("Communication running"));
Toast.makeText(mainActivity.getApplicationContext(),"Communication running", Toast.LENGTH_SHORT).show();

//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText("Communication running"));
}
private class ReceiveThread extends Thread {
private StringBuffer rxStringBuffer = new StringBuffer();
@@ -52,7 +55,8 @@ public class WifiCommunication {
DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(rxPacket);
rxString = new String(rxPacket.getData());
if(!previousRxString.equals(rxString)) {
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));
previousRxString = rxString;
@@ -77,18 +81,13 @@ public class WifiCommunication {
Date curDate = new Date(System.currentTimeMillis());
String str = formatter.format(curDate);
byte[] send_Data = new byte[512];
String txString = (str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," +messageToSend);
String txString = ("1," +str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," +messageToSend);
send_Data = txString.getBytes();

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

for(int i = 0; i < 500; i++) {
socket.send(txPacket);
/*try{
sleep(10);
}catch (InterruptedException e){
e.printStackTrace();
}*/
}
}
} while (running);

Loading…
Cancel
Save