I asked this already on the official forum but did not receive an answer yet.
I'm currently trying to realize my "smart home" project, by controlling several devices (light, shutters, doors, garage) with my smartphone using Xamarin Android. I control my devices with UDP packets.
I now try to receive an UDP packet from my Arduino to check whether a device (for example light) is on or off. For this, I use a Udplistener:
class UdpListener
{
private readonly UdpClient _udpClient = new UdpClient(4210);
public async void StartListening()
{
while (true)
{
var result = await _udpClient.ReceiveAsync();
var message = Encoding.ASCII.GetString(result.Buffer);
if (message.Contains("1"))
{
Licht.GlobalVariables.lampe1 = "1";
}
else
{
Licht.GlobalVariables.lampe1 = "0";
}
}
}
}
When I receive the correct answer, I set an indicator to visible (in onCreateView) so I can see within the App if a light is on/off. But here is the problem I have: the indicator will only be visible if I reload the fragment (which makes sense since it takes some time to receive the UDP packet).
Is there any way to force a "refresh" of my current fragment (view) once I receive the UDP answer? Every code I found was not accepted.
For example this snippet:
// Reload current fragment
Fragment frg = null;
frg = getSupportFragmentManager().findFragmentByTag("Your_Fragment_TAG");
final FragmentTransaction ft =
getSupportFragmentManager().beginTransaction();
ft.detach(frg);
ft.attach(frg);
ft.commit();