/// <summary>
        ///   Initializes a new instance of the <see cref="HardwareOnlyKey"/> class.
        /// </summary>
        /// <param name="hidApi"><see cref="HidApi"/> instance wrapping the hidapi library.</param>
        public HardwareOnlyKey(HidApi hidApi)
        {
            if (hidApi == null)
            {
                throw new ArgumentNullException(nameof(hidApi));
            }

            this.hidApi     = hidApi;
            this.disposed   = false;
            this.deviceInfo = this.FindSoleOnlyKey();
            this.device     = this.hidApi.Open(this.deviceInfo.Path);
        }
Exemple #2
0
        private static void DoSomethingWithHid()
        {
            IHidHandle hid1 = null;

            try
            {
                try
                {
                    hid1 = HidApi.Library.Open(deviceIds[0].VendorId, deviceIds[0].ProductId, null);
                }
                catch (InvalidOperationException e)
                {
                    Console.WriteLine($"Could not open once ({e.Message}), retrying");
                    try
                    {
                        hid1 = HidApi.Library.Open(deviceIds[1].VendorId, deviceIds[1].ProductId, null);
                    }
                    catch (InvalidOperationException f)
                    {
                        Console.WriteLine($"No device found ({f.Message}), sorry.");
                        return;
                    }
                }

                if (hid1 == null)
                {
                    Console.WriteLine("No device found, sorry, should not be reached.");
                    return;
                }

                try
                {
                    Console.WriteLine("Manufacturer = {0}", hid1.GetManufacturerString());
                }
                catch (InvalidOperationException e)
                {
                    Console.WriteLine($"Couldn't get manufacturer info: {e.Message}");
                }

                try
                {
                    Console.WriteLine("Product = {0}", hid1.GetProductString());
                }
                catch (InvalidOperationException e)
                {
                    Console.WriteLine($"Couldn't get product info: {e.Message}");
                }

                try
                {
                    Console.WriteLine("Serial_number = {0}", hid1.GetSerialNumber());
                }
                catch (InvalidOperationException e)
                {
                    Console.WriteLine($"Couldn't get serial number: {e.Message}");
                }
            }
            finally
            {
                // NB: normally I should be using the `using' keyword and the
                // hid1.Dispose would be implicit...
                if (hid1 != null)
                {
                    hid1.Dispose();
                    hid1 = null;
                    Console.WriteLine("Closed device");
                }
            }
        }