Hi,
I have a problem with the broadcastreceiver in xamarin. When I attach my usb device, the OnReceive method is not firing, The strange this is that the OnReceive mehtod DOES FIRE, when I disconnect my device.
Here is my code:
public class UsbDeviceIntentReceiver : BroadcastReceiver { public UsbDeviceIntentReceiver() { Factory.Instance.UsbFilter = new IntentFilter(); Factory.Instance.UsbFilter.AddAction(Factory.Instance.ACTION_USB_PERMISSION); Factory.Instance.UsbFilter.AddAction(UsbManager.ActionUsbDeviceAttached); Factory.Instance.UsbFilter.AddAction(UsbManager.ActionUsbDeviceDetached); Factory.Instance.MainActivity.RegisterReceiver(this, Factory.Instance.UsbFilter); }
public override void OnReceive(Context context, Intent intent)
{
if (Factory.Instance.ACTION_USB_PERMISSION == intent.Action && intent.GetBooleanExtra(UsbManager.ExtraPermissionGranted,false))
{
if(Factory.Instance.Device == null)
Factory.Instance.Attach();
}
else if (UsbManager.ActionUsbDeviceDetached == intent.Action)
{
var usbDeviceReceived = (UsbDevice)intent.GetParcelableExtra(UsbManager.ExtraDevice);
if (Factory.Instance.Device != null && usbDeviceReceived != null && Factory.Instance.Device.DeviceId == usbDeviceReceived.DeviceId)
{
Factory.Instance.Detach();
}
}
}
}
Thanks