Broadcast Receiver with Xamarin PCL not working

Hi Folks,

I am trying to get reading like heart rate,blood pressure, steps count etc from Helo Wearable device and implemented a Broadcast Reciever in Xamarin android form project but receiver is not receiving any message though the same solution is working in java working fine but i am not sure what is the issue with xamarin PCL.

These both are inside Android form project,

Below is MainActivity Code,

`[Activity(Label = "HeloApp.Android",
Theme = "@style/MyTheme", MainLauncher = true,
ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]
public class MainActivity : global::Xamarin.Forms.Platform.Android.FormsAppCompatActivity
{
MeasurementReceiver heartRateRec;
IntentFilter intentFilter;
protected override void OnCreate(Bundle bundle)
{
TabLayoutResource = Resource.Layout.Tabbar;
ToolbarResource = Resource.Layout.Toolbar;

        base.OnCreate(bundle);

        global::Xamarin.Forms.Forms.Init(this, bundle);

        LoadApplication(new App());
        Connector.Instance.Initialize(this, "151265380086941117", "E862D6D7D70ED2C3729CB24BC3AB40753A52EAB0");
        heartRateRec = new MeasurementReceiver();
        intentFilter = new IntentFilter();
        intentFilter.AddAction("com.worldgn.connector.HR_MEASUREMENT");
        intentFilter.AddAction("com.worldgn.connector.BP_MEASUREMENT");
    }

    protected override void OnResume()
    {
        base.OnResume();
        RegisterReceiver(heartRateRec, intentFilter);
    }

    protected override void OnPause()
    {
        base.OnPause();
        UnregisterReceiver(heartRateRec);
    }`

Below is the Broadcast receiver,

                `[BroadcastReceiver(Enabled = true)]
                    public class MeasurementReceiver : BroadcastReceiver
                    {
                        public override void OnReceive(Context context, Intent intent)
                        {
                            Toast.MakeText(context, "Received intent!", ToastLength.Short).Show();
                            if (intent.Action.Equals("com.worldgn.connector.HR_MEASUREMENT"))
                            {
                                var heartRate = intent.GetStringExtra("HR_MEASUREMENT");
                            }
                        }
                    }`

Here is the code written inside Xamarin PCL form,

` [XamlCompilation(XamlCompilationOptions.Compile)]
public partial class BLEDevices : ContentPage
{
public BLEDevices()
{
InitializeComponent();
}

    private void SearchBLE_Clicked(object sender, EventArgs e)
    {
        Connector.Instance.Scan(new DeviceScanCallBack());
    }

    private void HR_Clicked(object sender, EventArgs e)
    {
        Connector.Instance.MeasureHR();
    }

    private void BP_Clicked(object sender, EventArgs e)
    {
        Connector.Instance.MeasureBP();
    }

    private void Disconnect_Clicked(object sender, EventArgs e)
    {
        Connector.Instance.UnbindDevice();
        DisplayAlert("Information", "Device disconnected successfully.", "Cancel");
    }
}

public class DeviceScanCallBack : Java.Lang.Object, IScanCallBack
{
    public void OnLedeviceFound(DeviceItem p0)
    {
        Connector.Instance.Connect(p0);
        App.Current.MainPage.DisplayAlert("Information", "Device connected successfully.", "Cancel");           
    }

    public void OnPairedDeviceNotFound()
    {
        App.Current.MainPage.DisplayAlert("Warning", "Please disconnect the device first.", "OK");
    }

    public void OnScanFinished()
    {

    }

    public void OnScanStarted()
    {

    }
}`

On button click i already initiated with "Connector.Instance.MeasureHR();" but broadcast receiver is not receiving any methods,

Please let me know what went wrong here.

Thanks

Answers

Sign In or Register to comment.