WifiCommunication wifiCommunication; | WifiCommunication wifiCommunication; | ||||
StringBuffer dataFromWifi; | |||||
String dataFromWifi; | |||||
@Override | @Override | ||||
public int onStartCommand(Intent intent, int flags, int startId) { | public int onStartCommand(Intent intent, int flags, int startId) { | ||||
wifiCommunication = new WifiCommunication (1234); | wifiCommunication = new WifiCommunication (1234); | ||||
wifiCommunication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() { | wifiCommunication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() { | ||||
@Override | @Override | ||||
public void onConnection(StringBuffer data) { | |||||
public void onConnection(String data) { | |||||
dataFromWifi = data; | dataFromWifi = data; | ||||
} | } | ||||
}); | }); | ||||
return super.onStartCommand(intent, flags, startId); | return super.onStartCommand(intent, flags, startId); | ||||
} | } |
button1.setOnClickListener(new View.OnClickListener() { | button1.setOnClickListener(new View.OnClickListener() { | ||||
@Override | @Override | ||||
public void onClick(View v) { | public void onClick(View v) { | ||||
communication.sendTrue(" Testmessage "); | |||||
communication.sendTrue("Testmessage"); | |||||
} | } | ||||
}); | }); | ||||
} | } | ||||
communication = new WifiCommunication(1234); | communication = new WifiCommunication(1234); | ||||
communication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() { | communication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() { | ||||
@Override | @Override | ||||
public void onConnection(StringBuffer data) { | |||||
public void onConnection(String data) { | |||||
//Log.d("Test", data.toString()); | //Log.d("Test", data.toString()); | ||||
runOnUiThread(new Runnable() { | runOnUiThread(new Runnable() { | ||||
@Override | @Override | ||||
return super.onOptionsItemSelected(item); | return super.onOptionsItemSelected(item); | ||||
} | } | ||||
} | } | ||||
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(); | |||||
} | |||||
} | } | ||||
Button buttonEdit = popupView.findViewById(R.id.RechteAnfordern); | Button buttonEdit = popupView.findViewById(R.id.RechteAnfordern); | ||||
buttonEdit.setOnClickListener(new View.OnClickListener() { | buttonEdit.setOnClickListener(new View.OnClickListener() { | ||||
@Override | @Override | ||||
public void onClick(View v) { | |||||
RechteAnfordern(); | |||||
public void onClick(View v) {RechteAnfordern(); | |||||
} | } | ||||
}); | }); | ||||
} | } | ||||
public interface OnConnectionListener { | public interface OnConnectionListener { | ||||
void onConnection(StringBuffer data); | |||||
void onConnection(String data); | |||||
} | } | ||||
public void setOnConnectionListener(@NonNull OnConnectionListener listener) { | public void setOnConnectionListener(@NonNull OnConnectionListener listener) { | ||||
this.listener = listener; | this.listener = listener; | ||||
} | } | ||||
public void sendWifiData(StringBuffer wifiMessage) { | |||||
public void sendWifiData(String wifiMessage) { | |||||
if (listener != null) { | if (listener != null) { | ||||
listener.onConnection(wifiMessage); | listener.onConnection(wifiMessage); | ||||
} | } | ||||
rxString = new String(receiveData, 0, rxPacket.getLength()); | rxString = new String(receiveData, 0, rxPacket.getLength()); | ||||
String[] splitrxString = rxString.split(","); | String[] splitrxString = rxString.split(","); | ||||
if(!previousRxString.equals(rxString) && splitrxString[0].equals("1") && splitrxString.length==7) { | if(!previousRxString.equals(rxString) && splitrxString[0].equals("1") && splitrxString.length==7) { | ||||
rxStringBuffer.append(rxString).append("\n"); | |||||
sendWifiData(rxStringBuffer); | |||||
//rxStringBuffer.append(rxString).append("\n"); | |||||
sendWifiData(rxString); | |||||
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer)); | //mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer)); | ||||
previousRxString = rxString; | previousRxString = rxString; | ||||
} | } | ||||
if(send) | if(send) | ||||
{ | { | ||||
send = false; | send = false; | ||||
SimpleDateFormat formatter = new SimpleDateFormat("HH:mm:ss"); | |||||
Date curDate = new Date(System.currentTimeMillis()); | |||||
String str = formatter.format(curDate); | |||||
byte[] send_Data = new byte[512]; | byte[] send_Data = new byte[512]; | ||||
String txString = ("1," +str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," +messageToSend); | |||||
String txString = (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 < 300; i++) { | |||||
for(int i = 0; i < 500; i++) { | |||||
socket.send(txPacket); | socket.send(txPacket); | ||||
} | } | ||||
} | } |