Kopie Master Version 1.2 UDP Socket merged with sensor
This commit is contained in:
parent
35708220f8
commit
5f1f127e62
@ -29,7 +29,7 @@ public class DetectorService extends LifecycleService {
|
||||
|
||||
WifiCommunication wifiCommunication;
|
||||
|
||||
StringBuffer dataFromWifi;
|
||||
String dataFromWifi;
|
||||
|
||||
@Override
|
||||
public int onStartCommand(Intent intent, int flags, int startId) {
|
||||
@ -54,9 +54,10 @@ public class DetectorService extends LifecycleService {
|
||||
wifiCommunication = new WifiCommunication (1234);
|
||||
wifiCommunication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() {
|
||||
@Override
|
||||
public void onConnection(StringBuffer data) {
|
||||
public void onConnection(String data) {
|
||||
dataFromWifi = data;
|
||||
}
|
||||
|
||||
});
|
||||
return super.onStartCommand(intent, flags, startId);
|
||||
}
|
||||
|
@ -53,7 +53,7 @@ public class MainActivity extends AppCompatActivity {
|
||||
communication = new WifiCommunication(1234);
|
||||
communication.setOnConnectionListener(new WifiCommunication.OnConnectionListener() {
|
||||
@Override
|
||||
public void onConnection(StringBuffer data) {
|
||||
public void onConnection(String data) {
|
||||
//Log.d("Test", data.toString());
|
||||
runOnUiThread(new Runnable() {
|
||||
@Override
|
||||
@ -101,12 +101,5 @@ public class MainActivity extends AppCompatActivity {
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -50,8 +50,7 @@ public class PopUpClass {
|
||||
Button buttonEdit = popupView.findViewById(R.id.RechteAnfordern);
|
||||
buttonEdit.setOnClickListener(new View.OnClickListener() {
|
||||
@Override
|
||||
public void onClick(View v) {
|
||||
RechteAnfordern();
|
||||
public void onClick(View v) {RechteAnfordern();
|
||||
}
|
||||
});
|
||||
|
||||
|
@ -51,13 +51,13 @@ public class WifiCommunication {
|
||||
}
|
||||
|
||||
public interface OnConnectionListener {
|
||||
void onConnection(StringBuffer data);
|
||||
void onConnection(String data);
|
||||
}
|
||||
public void setOnConnectionListener(@NonNull OnConnectionListener listener) {
|
||||
this.listener = listener;
|
||||
}
|
||||
|
||||
public void sendWifiData(StringBuffer wifiMessage) {
|
||||
public void sendWifiData(String wifiMessage) {
|
||||
if (listener != null) {
|
||||
listener.onConnection(wifiMessage);
|
||||
}
|
||||
@ -78,8 +78,8 @@ public class WifiCommunication {
|
||||
rxString = new String(receiveData, 0, rxPacket.getLength());
|
||||
String[] splitrxString = rxString.split(",");
|
||||
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));
|
||||
previousRxString = rxString;
|
||||
}
|
||||
@ -99,16 +99,13 @@ public class WifiCommunication {
|
||||
if(send)
|
||||
{
|
||||
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];
|
||||
String txString = ("1," +str+ ",Gruppe2," + getLocalIpAddress() + ",An,Video," +messageToSend);
|
||||
String txString = (messageToSend);
|
||||
send_Data = txString.getBytes();
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user