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

<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" /> <uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" /> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" /> <uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.CAMERA" /> <!-- §permissionCamera§ -->
<uses-permission android:name="android.permission.RECORD_AUDIO" />


<application <application
android:allowBackup="true" android:allowBackup="true"
<activity <activity
android:name=".MainActivity" android:name=".MainActivity"
android:exported="true" android:exported="true"
android:label="Server">
android:label="Ueberwachungsapp">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />



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

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.provider.Settings;
import android.telephony.TelephonyManager;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.widget.Button; import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView; import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity; 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.InetAddress;
import java.net.ServerSocket;
import java.net.Socket;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import java.nio.ByteBuffer; import java.nio.ByteBuffer;
import java.nio.ByteOrder; import java.nio.ByteOrder;
TextView tvConnectionInfos; TextView tvConnectionInfos;


WifiCommunication communication; WifiCommunication communication;
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;
float i=0; float i=0;
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
tvMessages = findViewById(R.id.tvMessages); tvMessages = findViewById(R.id.tvMessages);
tvConnectionInfos = findViewById(R.id.tvConnectionInfos); tvConnectionInfos = findViewById(R.id.tvConnectionInfos);
String deviceId = Settings.Secure.getString(getContentResolver(), Settings.Secure.ANDROID_ID);
try { try {
SERVER_IP = getLocalIpAddress(); SERVER_IP = getLocalIpAddress();
tvConnectionInfos.setText("Connection Infos: \n Own IP-Adress: " + SERVER_IP+ " Port: " + SERVER_PORT); tvConnectionInfos.setText("Connection Infos: \n Own IP-Adress: " + SERVER_IP+ " Port: " + SERVER_PORT);
} }
}); });
} }
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() { protected void onResume() {
super.onResume(); super.onResume();
communication = new WifiCommunication(MainActivity.this, SERVER_PORT); communication = new WifiCommunication(MainActivity.this, SERVER_PORT);
//communication.sendTrue(" has Entered ");

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

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

package com.example.ueberwachungssystem; package com.example.ueberwachungssystem;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.widget.Toast;


import java.io.IOException; import java.io.IOException;
import java.net.DatagramPacket; import java.net.DatagramPacket;
} catch (SocketException | UnknownHostException e) { } catch (SocketException | UnknownHostException e) {
throw new RuntimeException(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 class ReceiveThread extends Thread {
private StringBuffer rxStringBuffer = new StringBuffer(); private StringBuffer rxStringBuffer = new StringBuffer();
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(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"); rxStringBuffer.append(rxString).append("\n");
mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer)); mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
previousRxString = rxString; previousRxString = rxString;
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 = (str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," +messageToSend);
String txString = ("1," +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 < 500; 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);

Loading…
Cancel
Save