Browse Source

Kommunikation Version 1.1 UDP Socket

Kommunikation
Miguel Siebenhaar 1 year ago
parent
commit
b246ad8624

+ 7
- 1
app/src/main/java/com/example/ueberwachungssystem/MainActivity.java View File

@@ -3,6 +3,8 @@ 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.View;
import android.widget.Button;
import android.widget.EditText;
@@ -33,12 +35,15 @@ public class MainActivity extends AppCompatActivity {

String message = "";

String deviceId;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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);
@@ -50,13 +55,14 @@ public class MainActivity extends AppCompatActivity {
@Override
public void onClick(View v) {
i++;
communication.sendTrue("Test" +i);
communication.sendTrue(deviceId+ "Test" +i);
}
});
}
protected void onResume() {
super.onResume();
communication = new WifiCommunication(MainActivity.this, SERVER_PORT);
//communication.sendTrue(deviceId+ " has Entered");

}
@Override

+ 19
- 12
app/src/main/java/com/example/ueberwachungssystem/WifiCommunication.java View File

@@ -27,7 +27,7 @@ public class WifiCommunication {
try {
socket = new DatagramSocket(this.port);
socket.setBroadcast(true);
address = InetAddress.getByName("255.255.255.255");
address = InetAddress.getByName("10.0.2.255"); //100.82.255.255
running = true;
send = false;
new ReceiveThread().start();
@@ -39,25 +39,27 @@ public class WifiCommunication {
}
private class ReceiveThread extends Thread {
private StringBuffer rxStringBuffer = new StringBuffer();
private String rxString;
private String rxString="";
private String previousRxString = "";

@Override
public void run() {
try {
do {
byte[] rxBuffer = new byte[512];
byte[] receiveData = new byte[512];
InetAddress fromAdress;
int fromPort;
DatagramPacket rxPacket = new DatagramPacket(rxBuffer, rxBuffer.length);
DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length);
socket.receive(rxPacket);
fromAdress = rxPacket.getAddress();
fromPort = rxPacket.getPort();
rxString = new String(rxBuffer, 0, rxPacket.getLength());
rxString = new String(rxPacket.getData());
if(!previousRxString.equals(rxString)) {
rxStringBuffer.append(rxString).append("\n");
mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
rxStringBuffer.append("Adress: " + fromAdress).append(" Port: " + fromPort).append(" Message " +rxString).append("\n");
previousRxString = rxString;
}
previousRxString = rxString;

} while (running);
}
@@ -75,14 +77,19 @@ public class WifiCommunication {
if(send)
{
send = false;
byte[] send_Data = new byte[512];
String txString = (getLocalIpAddress() + " sends:" +messageToSend+ " Count: " + tmpCnt++);
send_Data = txString.getBytes();

String txString = getLocalIpAddress() + " sends:" +messageToSend+ " Count: " + tmpCnt++;
byte[] txBuffer = txString.getBytes();

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

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

+ 16
- 8
app/src/main/res/layout/activity_main.xml View File

@@ -10,20 +10,28 @@
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:hint="ConnectionInfos"
android:textAppearance = "@style/Base.TextAppearance.AppCompat.Medium" />

<Button
android:id="@+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/tvMessages"
android:layout_below="@id/scrollView"
android:layout_centerHorizontal="true"
android:text="SEND" />

<ScrollView
android:id="@+id/scrollView"
android:layout_width="match_parent"
android:layout_below="@id/tvConnectionInfos"
android:layout_height="300dp">
<TextView
android:id = "@+id/tvMessages"
android:layout_width = "match_parent"
android:layout_height = "wrap_content"
android:hint="Messages"
android:inputType = "textMultiLine"
android:textAppearance = "@style/Base.TextAppearance.AppCompat.Medium" />
</ScrollView>
</RelativeLayout>

Loading…
Cancel
Save