xamarin.forms – Broadcast receiver not working in Xamarin Forms

xamarin.forms – Broadcast receiver not working in Xamarin Forms

i am initiating the Broadcast receiver from the xamarin forms side.

It means that you want use native API(Android platform API SendBroadcast), is it right? If it is right, in xamarin.Android you can use DependencyService to achieve it.

Four steps:

1.Define interface in PCL which you have called as xamarin forms side:

   public interface ISend
    {
        void Send();
    }

2.Implementation Android Platform:

class SendImpl : ISend
{
    public void Send()
    {
        Intent intent = new Intent(com.worldgn.connector.HR_MEASUREMENT);
        intent.PutExtra(HR_MEASUREMENT,value);
        Forms.Context.SendBroadcast(intent);
    }
}

3.Use DependencyService to Register, add this above the namespace:

[assembly:Dependency(typeof(SendImpl))]

4.Call to DependencyService in PCL, then you will achieve OnReceive:

public void OnLedeviceFound(DeviceItem p0)
{
    Connector.Instance.Connect(p0);
    App.Current.MainPage.DisplayAlert(Information, Device connected successfully., Cancel);

    //Here i am trying to initiate the broadcast receiver
    DependencyService.Get<ISend>().Send();
    Connector.Instance.MeasureHR();
}

xamarin.forms – Broadcast receiver not working in Xamarin Forms

Leave a Reply

Your email address will not be published.