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;
|
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) {
|
||||||
@ -54,9 +54,10 @@ public class DetectorService extends LifecycleService {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
|
@ -53,7 +53,7 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
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
|
||||||
@ -101,12 +101,5 @@ public class MainActivity extends AppCompatActivity {
|
|||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -50,8 +50,7 @@ public class PopUpClass {
|
|||||||
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) {
|
public void onClick(View v) {RechteAnfordern();
|
||||||
RechteAnfordern();
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -51,13 +51,13 @@ public class WifiCommunication {
|
|||||||
}
|
}
|
||||||
|
|
||||||
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);
|
||||||
}
|
}
|
||||||
@ -78,8 +78,8 @@ public class WifiCommunication {
|
|||||||
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");
|
//rxStringBuffer.append(rxString).append("\n");
|
||||||
sendWifiData(rxStringBuffer);
|
sendWifiData(rxString);
|
||||||
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
|
//mainActivity.runOnUiThread(() -> mainActivity.tvMessages.setText(rxStringBuffer));
|
||||||
previousRxString = rxString;
|
previousRxString = rxString;
|
||||||
}
|
}
|
||||||
@ -99,16 +99,13 @@ public class WifiCommunication {
|
|||||||
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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user