简体   繁体   中英

How to get notified using Broadcast Receiver in xamarin forms?

I need to get notification about Bluetooth notification event so I planned to use the broadcast receiver. But I did not get any notification Please guide. Please help me what is the mistake I have done here?

   [BroadcastReceiver(Enabled = true)]
   [IntentFilter(new[] { "android.bluetooth.adapter.action.STATE_CHANGED" })]
   public  class BroadcastMonitor : BroadcastReceiver
    {
        public override void OnReceive(Context context, Intent intent)
        {`
        // Do stuff here.

           String action = intent.Action;
           if (action.Equals(BluetoothAdapter.ActionConnectionStateChanged)) {
              int state = intent.GetIntExtra(BluetoothAdapter.ExtraState,
                                                 BluetoothAdapter.Error);
              if (BluetoothDevice.ActionAclConnected.Equals(action)) {
                 Console.WriteLine("on"); //not trigeered
              }

              if (BluetoothDevice.ActionAclDisconnected.Equals(action)) {
                  Console.WriteLine("off"); //not trigeered
              }  
           }
        }
    } 

//Main Activity code:

    protected override void OnResume()
    {
        RegisterReceiver(broadCastMonitor,new IntentFilter("android.bluetooth.adapter.action.STATE_CHANGED"));
            base.OnResume();
    }

    protected override void OnPause()
    {
        UnregisterReceiver(broadCastMonitor);
        base.OnPause();
    } 
    protected override async void OnCreate(Bundle savedInstanceState)
    {
        broadCastMonitor=new BroadcastMonitor();
        IntentFilter filter = new 
             IntentFilter(BluetoothAdapter.ActionStateChanged);
    }
       IntentFilter filter1;
    IntentFilter filter2;
    IntentFilter filter3;     
 protected override async void OnCreate(Bundle savedInstanceState)
    {    
        filter1 = new IntentFilter(BluetoothDevice.ActionAclConnected);
        filter2 = new IntentFilter(BluetoothDevice.ActionAclDisconnectRequested);
        filter3 = new IntentFilter(BluetoothDevice.ActionAclDisconnected);

        LoadApplication(new App());

    }
    protected override void OnResume()
    {
        this.RegisterReceiver(broadCastMonitor, filter1);
        this.RegisterReceiver(broadCastMonitor, filter2);
        this.RegisterReceiver(broadCastMonitor, filter3);
        base.OnResume();
    }

    protected override void OnPause()
    {
        this.UnregisterReceiver(broadCastMonitor);
        base.OnPause();
    }  
       public override void OnReceive(Context context, Intent intent)
    {
        String action = intent.Action;
        if (BluetoothDevice.ActionAclConnected.Equals(action))
        {
            //Do something if connected
            Toast.MakeText(context, "Bluetooth Connected" + GlobalCache.ConnectedDeviceInfo.Name, ToastLength.Short).Show();
        }
        else if (BluetoothDevice.ActionAclDisconnected.Equals(action))
        {
            Toast.MakeText(context, "Bluetooth disconnected", ToastLength.Short).Show();
        }
        else if (BluetoothDevice.ActionAclDisconnectRequested.Equals(action))
        {
            Toast.MakeText(context, "Bluetooth disconnecting..", ToastLength.Short).Show();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM