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; | ||||
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); | ||||
@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 |
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(); | ||||
} | } | ||||
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); | ||||
} | } | ||||
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++; | |||||
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); | socket.send(txPacket); | ||||
/*try{ | |||||
sleep(10); | |||||
}catch (InterruptedException e){ | |||||
e.printStackTrace(); | |||||
}*/ | |||||
} | } | ||||
} | } | ||||
} while (running); | } while (running); |
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" /> | |||||
<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" /> | 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> |