Kommunikation Version 1.1 UDP Socket
This commit is contained in:
parent
192f88ede5
commit
b246ad8624
@ -3,6 +3,8 @@ import android.annotation.SuppressLint;
|
|||||||
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.View;
|
import android.view.View;
|
||||||
import android.widget.Button;
|
import android.widget.Button;
|
||||||
import android.widget.EditText;
|
import android.widget.EditText;
|
||||||
@ -33,12 +35,15 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
|
|
||||||
String message = "";
|
String message = "";
|
||||||
|
|
||||||
|
String deviceId;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
protected void onCreate(Bundle savedInstanceState) {
|
||||||
super.onCreate(savedInstanceState);
|
super.onCreate(savedInstanceState);
|
||||||
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);
|
||||||
@ -50,13 +55,14 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
@Override
|
@Override
|
||||||
public void onClick(View v) {
|
public void onClick(View v) {
|
||||||
i++;
|
i++;
|
||||||
communication.sendTrue("Test" +i);
|
communication.sendTrue(deviceId+ "Test" +i);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
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(deviceId+ " has Entered");
|
||||||
|
|
||||||
}
|
}
|
||||||
@Override
|
@Override
|
||||||
|
@ -27,7 +27,7 @@ public class WifiCommunication {
|
|||||||
try {
|
try {
|
||||||
socket = new DatagramSocket(this.port);
|
socket = new DatagramSocket(this.port);
|
||||||
socket.setBroadcast(true);
|
socket.setBroadcast(true);
|
||||||
address = InetAddress.getByName("255.255.255.255");
|
address = InetAddress.getByName("10.0.2.255"); //100.82.255.255
|
||||||
running = true;
|
running = true;
|
||||||
send = false;
|
send = false;
|
||||||
new ReceiveThread().start();
|
new ReceiveThread().start();
|
||||||
@ -39,25 +39,27 @@ public class WifiCommunication {
|
|||||||
}
|
}
|
||||||
private class ReceiveThread extends Thread {
|
private class ReceiveThread extends Thread {
|
||||||
private StringBuffer rxStringBuffer = new StringBuffer();
|
private StringBuffer rxStringBuffer = new StringBuffer();
|
||||||
private String rxString;
|
private String rxString="";
|
||||||
private String previousRxString = "";
|
private String previousRxString = "";
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
try {
|
try {
|
||||||
do {
|
do {
|
||||||
byte[] rxBuffer = new byte[512];
|
byte[] receiveData = new byte[512];
|
||||||
InetAddress fromAdress;
|
InetAddress fromAdress;
|
||||||
int fromPort;
|
int fromPort;
|
||||||
DatagramPacket rxPacket = new DatagramPacket(rxBuffer, rxBuffer.length);
|
DatagramPacket rxPacket = new DatagramPacket(receiveData, receiveData.length);
|
||||||
socket.receive(rxPacket);
|
socket.receive(rxPacket);
|
||||||
fromAdress = rxPacket.getAddress();
|
fromAdress = rxPacket.getAddress();
|
||||||
fromPort = rxPacket.getPort();
|
fromPort = rxPacket.getPort();
|
||||||
rxString = new String(rxBuffer, 0, rxPacket.getLength());
|
rxString = new String(rxPacket.getData());
|
||||||
if(!previousRxString.equals(rxString)) {
|
if(!previousRxString.equals(rxString)) {
|
||||||
|
rxStringBuffer.append(rxString).append("\n");
|
||||||
mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
|
mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
|
||||||
rxStringBuffer.append("Adress: " + fromAdress).append(" Port: " + fromPort).append(" Message " +rxString).append("\n");
|
|
||||||
}
|
|
||||||
previousRxString = rxString;
|
previousRxString = rxString;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
} while (running);
|
} while (running);
|
||||||
}
|
}
|
||||||
@ -75,14 +77,19 @@ public class WifiCommunication {
|
|||||||
if(send)
|
if(send)
|
||||||
{
|
{
|
||||||
send = false;
|
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++;
|
DatagramPacket txPacket = new DatagramPacket(send_Data, txString.length(), address, port);
|
||||||
byte[] txBuffer = txString.getBytes();
|
|
||||||
|
|
||||||
DatagramPacket txPacket = new DatagramPacket(txBuffer, txBuffer.length, address, port);
|
for(int i = 0; i < 500; i++) {
|
||||||
|
|
||||||
for(int i = 0; i < 20; i++) {
|
|
||||||
socket.send(txPacket);
|
socket.send(txPacket);
|
||||||
|
/*try{
|
||||||
|
sleep(10);
|
||||||
|
}catch (InterruptedException e){
|
||||||
|
e.printStackTrace();
|
||||||
|
}*/
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} while (running);
|
} while (running);
|
||||||
|
@ -10,20 +10,28 @@
|
|||||||
android:layout_width = "match_parent"
|
android:layout_width = "match_parent"
|
||||||
android:layout_height = "wrap_content"
|
android:layout_height = "wrap_content"
|
||||||
android:inputType = "textMultiLine"
|
android:inputType = "textMultiLine"
|
||||||
android:textAppearance = "@style/Base.TextAppearance.AppCompat.Medium" />
|
android:hint="ConnectionInfos"
|
||||||
<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" />
|
android:textAppearance = "@style/Base.TextAppearance.AppCompat.Medium" />
|
||||||
|
|
||||||
<Button
|
<Button
|
||||||
android:id="@+id/btnSend"
|
android:id="@+id/btnSend"
|
||||||
android:layout_width="wrap_content"
|
android:layout_width="wrap_content"
|
||||||
android:layout_height="wrap_content"
|
android:layout_height="wrap_content"
|
||||||
android:layout_below="@id/tvMessages"
|
android:layout_below="@id/scrollView"
|
||||||
android:layout_centerHorizontal="true"
|
android:layout_centerHorizontal="true"
|
||||||
android:text="SEND" />
|
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>
|
</RelativeLayout>
|
Loading…
x
Reference in New Issue
Block a user