public SmartScopeUsbInterfaceXamarin(Context context, UsbManager usbManager, UsbDevice device)
        {
            Destroyed = false;
            if(!usbManager.HasPermission(device))
            {
                Logger.Error("Permission denied");
                throw new Exception("Device permission not obtained");
            }

            UsbInterface interf = device.GetInterface(0);
            for (int i = 0; i < interf.EndpointCount; i++)
            {
                if (interf.GetEndpoint(i).EndpointNumber == 1)
                    dataEndpoint = interf.GetEndpoint(i);
                else if (interf.GetEndpoint(i).EndpointNumber == 2)
                    commandWriteEndpoint = interf.GetEndpoint(i);
                else if (interf.GetEndpoint(i).EndpointNumber == 3)
                    commandReadEndpoint = interf.GetEndpoint(i);
            }
            usbConnection = usbManager.OpenDevice(device);
            usbConnection.ClaimInterface(interf, true);
        }
        public override void Open()
        {
            if (IsOpened) {
                return;
            }

            bool openedSuccessfully = false;
            try
            {
                CreateConnection();

                Log.Debug(Tag, "claiming interfaces, count=" + UsbDevice.InterfaceCount);
                mControlInterface = UsbDevice.GetInterface(0);
                Log.Debug(Tag, "Control iface=" + mControlInterface);
                // class should be USB_CLASS_COMM

                if (!Connection.ClaimInterface(mControlInterface, true))
                {
                    throw new IOException("Could not claim control interface.");
                }
                mControlEndpoint = mControlInterface.GetEndpoint(0);
                Log.Debug(Tag, "Control endpoint direction: " + mControlEndpoint.Direction);

                Log.Debug(Tag, "Claiming data interface.");
                mDataInterface = UsbDevice.GetInterface(1);
                Log.Debug(Tag, "data iface=" + mDataInterface);
                // class should be USB_CLASS_CDC_DATA

                if (!Connection.ClaimInterface(mDataInterface, true))
                {
                    throw new IOException("Could not claim data interface.");
                }
                mReadEndpoint = mDataInterface.GetEndpoint(1);
                Log.Debug(Tag, "Read endpoint direction: " + mReadEndpoint.Direction);
                mWriteEndpoint = mDataInterface.GetEndpoint(0);
                Log.Debug(Tag, "Write endpoint direction: " + mWriteEndpoint.Direction);
                if (mEnableAsyncReads)
                {
                    Log.Debug(Tag, "Async reads enabled");
                }
                else
                {
                    Log.Debug(Tag, "Async reads disabled.");
                }
                ResetParameters();
                openedSuccessfully = true;
            }
            finally {
                if (openedSuccessfully) {
                    IsOpened = true;
                    StartUpdating ();
                } else {
                    CloseConnection();
                }
            }
        }
        public override void Open()
        {
            bool openedSuccessfully = false;
            try
            {
                CreateConnection();

                UsbInterface usbInterface = UsbDevice.GetInterface(0);
                if (!Connection.ClaimInterface(usbInterface, true))
                {
                    throw new IOException("Error claiming Prolific interface 0");
                }

                for (int i = 0; i < usbInterface.EndpointCount; ++i)
                {
                    UsbEndpoint currentEndpoint = usbInterface.GetEndpoint(i);

                    switch ((int)currentEndpoint.Address)
                    {
                        case READ_ENDPOINT:
                            mReadEndpoint = currentEndpoint;
                            break;

                        case WRITE_ENDPOINT:
                            mWriteEndpoint = currentEndpoint;
                            break;

                        case INTERRUPT_ENDPOINT:
                            mInterruptEndpoint = currentEndpoint;
                            break;
                    }
                }

                if (UsbDevice.DeviceClass == UsbClass.Comm)
                {
                    mDeviceType = DEVICE_TYPE_0;
                }
                else
                {
                    try
                    {
                        byte[] rawDescriptors = Connection.GetRawDescriptors();
                        byte maxPacketSize0 = rawDescriptors[7];
                        if (maxPacketSize0 == 64)
                        {
                            mDeviceType = DEVICE_TYPE_HX;
                        }
                        else if ((UsbDevice.DeviceClass == UsbClass.PerInterface) || (UsbDevice.DeviceClass == UsbClass.VendorSpec))
                        {
                            mDeviceType = DEVICE_TYPE_1;
                        }
                        else
                        {
                            Log.Warn(TAG, "Could not detect PL2303 subtype, "
                                + "Assuming that it is a HX device");
                            mDeviceType = DEVICE_TYPE_HX;
                        }
                    }
                    catch (Exception e)
                    {
                        Log.Error(TAG, "An unexpected exception occured while trying "
                                + "to detect PL2303 subtype", e);
                    }
                }

                SetControlLines(mControlLinesValue);
                ResetDevice();
                DoBlackMagic();
                ResetParameters();

                openedSuccessfully = true;
            }
            finally {
                if (openedSuccessfully) {
                    IsOpened = true;
                    StartUpdating ();
                } else {
                    CloseConnection();
                }
            }
        }
        public override void Open()
        {
            bool openedSuccessfully = false;
            try
            {
                CreateConnection();

                for (int i = 0; i < UsbDevice.InterfaceCount; i++)
                {
                    UsbInterface usbIface = UsbDevice.GetInterface(i);
                    if (Connection.ClaimInterface(usbIface, true))
                    {
                        Log.Debug(TAG, "ClaimInterface " + i + " SUCCESS");
                    }
                    else
                    {
                        Log.Debug(TAG, "ClaimInterface " + i + " FAIL");
                    }
                }

                UsbInterface dataIface = UsbDevice.GetInterface(UsbDevice.InterfaceCount - 1);
                for (int i = 0; i < dataIface.EndpointCount; i++)
                {
                    UsbEndpoint ep = dataIface.GetEndpoint(i);
                    if (ep.Type == UsbAddressing.XferBulk)
                    { // UsbConstants.USB_ENDPOINT_XFER_BULK
                        if (ep.Direction == UsbAddressing.In)
                        { // UsbConstants.USB_DIR_IN
                            mReadEndpoint = ep;
                        }
                        else
                        {
                            mWriteEndpoint = ep;
                        }
                    }
                }

                SetConfigSingle(SilabsER_IFC_ENABLE_REQUEST_CODE, UART_ENABLE);
                SetConfigSingle(SilabsER_SET_MHS_REQUEST_CODE, MCR_ALL | CONTROL_WRITE_DTR | CONTROL_WRITE_RTS);
                SetConfigSingle(SilabsER_SET_BAUDDIV_REQUEST_CODE, BAUD_RATE_GEN_FREQ / DEFAULT_BAUD_RATE);
                ResetParameters();
                openedSuccessfully = true;
            }
            finally {
                if (openedSuccessfully) {
                    IsOpened = true;
                    StartUpdating ();
                } else {
                    CloseConnection();
                }
            }
        }
        public async Task<bool> OpenAsync()
        {
            if (connected)
                return false;

            usbManager.RequestPermission(usbDevice, PendingIntent.GetBroadcast(ConnectionService.Instance.Context, 0, new Intent(ConnectionService.Instance.ActionUsbPermission), PendingIntentFlags.CancelCurrent));


            UsbInterface intf = usbDevice.GetInterface(0);
            pinReportEndpoint = intf.GetEndpoint(0);
            peripheralResponseEndpoint = intf.GetEndpoint(1);
            pinConfigEndpoint = intf.GetEndpoint(2);
            peripheralConfigEndpoint = intf.GetEndpoint(3);
            connection = usbManager.OpenDevice(usbDevice);
            if (connection != null)
            {
                bool intfClaimed = connection.ClaimInterface(intf, true);
                if (intfClaimed)
                {
                    connected = true;
                    return true;
                }
            }
            return false;
        }