0

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();
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Peter P
  • 83
  • 1
  • 7

1 Answers1

1

Is there any way to force a "refresh" of my current fragment (view) once I receive the UDP answer?

The fragment rebuilds it's UI on onCreateView() method, and that happens when the fragment is created or recreated.

You'll have to implement your own updateUI method or where you will specify what elements and how they should update. You could use register a BroadcastReceiver in your Fragement.

  1. Register a BroadcastReceiver in your Fragment :

    MyBroadcastReceiver mBroadcastReceiver = new MyBroadcastReceiver();
    IntentFilter intentFilter = new IntentFilter();
    intentFilter.AddAction("com.test.demo");
    context.RegisterReceiver(mBroadcastReceiver, intentFilter);
    
  2. Once you receive the UDP answer, you could send a BroadcastReceiver :

    Intent intent = new Intent();
    intent.SetAction("com.test.lizi");
    intent.PutExtra("name", "ET");
    context.SendBroadcast(intent);
    
  3. Update your Fragment :

    public class MyBroadcastReceiver : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {
            updateUI(intent);  //calling function     
        }
    }
    

If this is not enough you could do something like replacing fragment with the same one forcing it to call onCreateView() method like this :

FragmentTransaction ft = this.FragmentManager.BeginTransaction();
ft.Replace(Resource.Id.your_fragment_container, yourFragmentInstance);
ft.Commit();
York Shen
  • 9,014
  • 1
  • 16
  • 40