Kommunikation Version 1 UDP Socket
This commit is contained in:
parent
35583a4cdd
commit
3c1366bd10
@ -26,6 +26,9 @@ android {
|
||||
sourceCompatibility JavaVersion.VERSION_1_8
|
||||
targetCompatibility JavaVersion.VERSION_1_8
|
||||
}
|
||||
buildFeatures {
|
||||
viewBinding true
|
||||
}
|
||||
}
|
||||
|
||||
dependencies {
|
||||
@ -33,6 +36,7 @@ dependencies {
|
||||
implementation 'androidx.appcompat:appcompat:1.6.1'
|
||||
implementation 'com.google.android.material:material:1.8.0'
|
||||
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
|
||||
implementation 'com.google.android.gms:play-services-nearby:18.0.0'
|
||||
testImplementation 'junit:junit:4.13.2'
|
||||
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
|
||||
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
|
||||
|
@ -2,18 +2,24 @@
|
||||
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools="http://schemas.android.com/tools">
|
||||
|
||||
<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" />
|
||||
|
||||
<application
|
||||
android:allowBackup="true"
|
||||
android:dataExtractionRules="@xml/data_extraction_rules"
|
||||
android:fullBackupContent="@xml/backup_rules"
|
||||
android:icon="@mipmap/ic_launcher"
|
||||
android:label="@string/app_name"
|
||||
android:roundIcon="@mipmap/ic_launcher_round"
|
||||
android:supportsRtl="true"
|
||||
android:theme="@style/Theme.Ueberwachungssystem"
|
||||
tools:targetApi="31">
|
||||
<activity
|
||||
android:name=".MainActivity"
|
||||
android:exported="true">
|
||||
android:exported="true"
|
||||
android:label="Server">
|
||||
<intent-filter>
|
||||
<action android:name="android.intent.action.MAIN" />
|
||||
|
||||
|
@ -1,15 +1,74 @@
|
||||
package com.example.ueberwachungssystem;
|
||||
|
||||
import androidx.appcompat.app.AppCompatActivity;
|
||||
|
||||
import android.annotation.SuppressLint;
|
||||
import android.net.wifi.WifiInfo;
|
||||
import android.net.wifi.WifiManager;
|
||||
import android.os.Bundle;
|
||||
import android.view.View;
|
||||
import android.widget.Button;
|
||||
import android.widget.EditText;
|
||||
import android.widget.TextView;
|
||||
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;
|
||||
|
||||
@SuppressLint("SetTextI18n")
|
||||
public class MainActivity extends AppCompatActivity {
|
||||
TextView tvMessages;
|
||||
TextView tvConnectionInfos;
|
||||
|
||||
WifiCommunication communication;
|
||||
public static String SERVER_IP = "";
|
||||
public static final int SERVER_PORT = 2222;
|
||||
int i=0;
|
||||
|
||||
boolean sending = false;
|
||||
|
||||
String message;
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState) {
|
||||
super.onCreate(savedInstanceState);
|
||||
setContentView(R.layout.activity_main);
|
||||
//
|
||||
tvMessages = findViewById(R.id.tvMessages);
|
||||
tvConnectionInfos = findViewById(R.id.tvConnectionInfos);
|
||||
try {
|
||||
SERVER_IP = getLocalIpAddress();
|
||||
tvConnectionInfos.setText("Connection Infos: \n Own IP-Adress: " + SERVER_IP+ " Port: " + SERVER_PORT);
|
||||
} catch (UnknownHostException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
Button btnSend = findViewById(R.id.btnSend);
|
||||
btnSend.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
i++;
|
||||
communication.sendTrue("Test" +i);
|
||||
}
|
||||
});
|
||||
}
|
||||
protected void onResume() {
|
||||
super.onResume();
|
||||
communication = new WifiCommunication(MainActivity.this, SERVER_PORT);
|
||||
|
||||
}
|
||||
@Override
|
||||
protected void onPause() {
|
||||
super.onPause();
|
||||
communication.stopCommunication();
|
||||
}
|
||||
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();
|
||||
}
|
||||
}
|
@ -0,0 +1,119 @@
|
||||
package com.example.ueberwachungssystem;
|
||||
import android.annotation.SuppressLint;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.DatagramPacket;
|
||||
import java.net.DatagramSocket;
|
||||
import java.net.Inet4Address;
|
||||
import java.net.InetAddress;
|
||||
import java.net.NetworkInterface;
|
||||
import java.net.SocketException;
|
||||
import java.net.UnknownHostException;
|
||||
import java.util.Enumeration;
|
||||
|
||||
public class WifiCommunication {
|
||||
private final MainActivity mainActivity;
|
||||
private final InetAddress address;
|
||||
private final int port;
|
||||
|
||||
private String messageToSend;
|
||||
volatile private boolean send;
|
||||
private final DatagramSocket socket;
|
||||
volatile private boolean running;
|
||||
@SuppressLint("SetTextI18n")
|
||||
public WifiCommunication(MainActivity mainActivity, int port) {
|
||||
this.mainActivity = mainActivity;
|
||||
this.port = port;
|
||||
try {
|
||||
socket = new DatagramSocket(this.port);
|
||||
socket.setBroadcast(true);
|
||||
address = InetAddress.getByName("255.255.255.255");
|
||||
running = true;
|
||||
send = false;
|
||||
new ReceiveThread().start();
|
||||
new SendThread().start();
|
||||
} catch (SocketException | UnknownHostException e) {
|
||||
throw new RuntimeException(e);
|
||||
}
|
||||
mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText("Communication running"));
|
||||
}
|
||||
private class ReceiveThread extends Thread {
|
||||
private StringBuffer rxStringBuffer = new StringBuffer();
|
||||
private String rxString;
|
||||
private String previousRxString = "";
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
do {
|
||||
byte[] rxBuffer = new byte[512];
|
||||
InetAddress fromAdress;
|
||||
int fromPort;
|
||||
DatagramPacket rxPacket = new DatagramPacket(rxBuffer, rxBuffer.length);
|
||||
socket.receive(rxPacket);
|
||||
fromAdress = rxPacket.getAddress();
|
||||
fromPort = rxPacket.getPort();
|
||||
rxString = new String(rxBuffer, 0, rxPacket.getLength());
|
||||
if(!previousRxString.equals(rxString)) {
|
||||
mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
|
||||
rxStringBuffer.append("Adress: " + fromAdress).append(" Port: " + fromPort).append(" Message " +rxString).append("\n");
|
||||
}
|
||||
previousRxString = rxString;
|
||||
|
||||
} while (running);
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
private class SendThread extends Thread {
|
||||
private int tmpCnt = 0;
|
||||
@Override
|
||||
public void run() {
|
||||
try {
|
||||
do {
|
||||
if(send)
|
||||
{
|
||||
send = false;
|
||||
|
||||
String txString = getLocalIpAddress() + " sends:" +messageToSend+ " Count: " + tmpCnt++;
|
||||
byte[] txBuffer = txString.getBytes();
|
||||
|
||||
DatagramPacket txPacket = new DatagramPacket(txBuffer, txBuffer.length, address, port);
|
||||
|
||||
for(int i = 0; i < 20; i++) {
|
||||
socket.send(txPacket);
|
||||
}
|
||||
}
|
||||
} while (running);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
}
|
||||
public static String getLocalIpAddress() {
|
||||
try {
|
||||
for (Enumeration<NetworkInterface> en = NetworkInterface.getNetworkInterfaces(); en.hasMoreElements();) {
|
||||
NetworkInterface networkInterface = (NetworkInterface) ((Enumeration<?>) en).nextElement();
|
||||
for (Enumeration<InetAddress> addresses = networkInterface.getInetAddresses(); addresses.hasMoreElements();) {
|
||||
InetAddress inetAddress = addresses.nextElement();
|
||||
if (!inetAddress.isLoopbackAddress() && inetAddress instanceof Inet4Address) {
|
||||
return inetAddress.getHostAddress();
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (SocketException ex) {
|
||||
ex.printStackTrace();
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public void sendTrue(String message){
|
||||
send = true;
|
||||
messageToSend = message;
|
||||
}
|
||||
public void stopCommunication() {
|
||||
running = false;
|
||||
socket.close();
|
||||
}
|
||||
}
|
@ -1,18 +1,29 @@
|
||||
<?xml version = "1.0" encoding = "utf-8"?>
|
||||
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:app="http://schemas.android.com/apk/res-auto"
|
||||
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
|
||||
xmlns:tools = "http://schemas.android.com/tools"
|
||||
android:layout_width = "match_parent"
|
||||
android:layout_height = "match_parent"
|
||||
android:layout_margin = "16dp"
|
||||
tools:context = ".MainActivity">
|
||||
|
||||
<TextView
|
||||
android:id = "@+id/tvConnectionInfos"
|
||||
android:layout_width = "match_parent"
|
||||
android:layout_height = "wrap_content"
|
||||
android:inputType = "textMultiLine"
|
||||
android:textAppearance = "@style/Base.TextAppearance.AppCompat.Medium" />
|
||||
<TextView
|
||||
android:id = "@+id/tvMessages"
|
||||
android:layout_width = "match_parent"
|
||||
android:layout_height = "wrap_content"
|
||||
android:layout_below="@id/tvConnectionInfos"
|
||||
android:inputType = "textMultiLine"
|
||||
android:textAppearance = "@style/Base.TextAppearance.AppCompat.Medium" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/btnSend"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:text="Hello World!"
|
||||
app:layout_constraintBottom_toBottomOf="parent"
|
||||
app:layout_constraintEnd_toEndOf="parent"
|
||||
app:layout_constraintStart_toStartOf="parent"
|
||||
app:layout_constraintTop_toTopOf="parent" />
|
||||
|
||||
</androidx.constraintlayout.widget.ConstraintLayout>
|
||||
android:layout_below="@id/tvMessages"
|
||||
android:layout_centerHorizontal="true"
|
||||
android:text="SEND" />
|
||||
</RelativeLayout>
|
@ -14,3 +14,4 @@ dependencyResolutionManagement {
|
||||
}
|
||||
rootProject.name = "Ueberwachungssystem"
|
||||
include ':app'
|
||||
include ':app'
|
||||
|
Loading…
x
Reference in New Issue
Block a user