Example #1
0
        public override void OnReceive(Context context, Intent intent)
        {
            String action = intent.Action;

            if (!IsReady && ACTION_USB_PERMISSION.Equals(action))
            {
                lock (this)
                {
                    Device = (UsbDevice)intent.GetParcelableExtra(UsbManager.ExtraDevice);

                    if (intent.GetBooleanExtra(UsbManager.ExtraPermissionGranted, false))
                    {
                        if (Device != null)
                        {
                            DeviceConnection = Manager.OpenDevice(Device);
                            if (DeviceConnection.ClaimInterface(Interface, true))
                            {
                                OutputRequest = new UsbRequest();
                                OutputRequest.Initialize(DeviceConnection, OutputEndpoint);
                                InputRequest = new UsbRequest();
                                InputRequest.Initialize(DeviceConnection, InputEndpoint);
                                HasPermission = true;
                                IsReady       = true;
                            }
                        }
                    }
                }
            }
            else if (!IsReady && /*!HasPermission &&*/ USB_DEVICE_ATTACHED.Equals(action))
            {
                // Parse for usb devices:
                ParseForUsbDevices();

                // Hook up USB :
                PendingIntent pendingIntent = PendingIntent.GetBroadcast(MainActivity, 0, new Intent(ACTION_USB_PERMISSION), 0);
                IntentFilter  filter        = new IntentFilter(ACTION_USB_PERMISSION);
                filter.AddAction(USB_DEVICE_ATTACHED);
                MainActivity.RegisterReceiver(this, filter);

                if (Device != null && Interface != null && OutputEndpoint != null && InputEndpoint != null)
                {
                    Manager.RequestPermission(Device, pendingIntent);
                    HasPermission = Manager.HasPermission(Device);
                }
            }
            else if (USB_DEVICE_DETACHED.Equals(action))
            {
                IsReady = false;
            }
        }