Skip to content

TWebUSBHID

Below is a list of the most important properties methods and events for the TWebUSBHID. TWebUSBHID is using the WebHID API. This API currently is available in Chromium based browsers from version 89.

Select a device/interface

A HID can have multiple interfaces that can be accessed as a HIDDevice object. For each object a ProductName property is available that can be used to differentiate between them. If you want to handle automatic connection to a device that had been given access before, you can loop through the Devices collection and connect to your device/interface.

procedure TForm2.WebHID1DevicesInitialized(Sender: TObject);
var
  I: Integer;
begin
  if WebHID1.DeviceCount = 0 then
  begin
    //no devices are available, request access from user here
  end
  else
  begin
    for I := 0 to WebHID1.DeviceCount do
    begin
      if WebHID1.Devices.Items[I].ProductName = 'IOW28-ADC' then
       begin
         WebHID1.SelectedDevice := WebHID1.Devices.Items[I];
         Break;
      end;
    end;
  end;
end;

Properties for TWebUSBHID

Property Description
AutoInitializeDevices Automatically calls InitDevices when the application starts.
AutoOpenSelection Connect to the device/interface automatically after selecting it.
AutoCloseSelection Close the current connection automatically when selecting another device/interface.
Devices A collection of available devices and interfaces that had been granted access to by the user.
DeviceCount The count of the available devices and interfaces in the Devices collection.
Filters A collection of options to filter the device selection

Methods for TWebUSBHID

Method Description
CloseDevice Closes the connection of the selected device/interface. This is an asynchronous method.
HasID Returns if the browser supports the WebHID API
InitDevices Initializes a list of devices that has already been granted access by the user previously. This is an asynchronous method.
OpenDevice Opens connection to the selected device/interface. This is an asynchronous method.
RequestDevices Requests access from the user to an available HID. This is an asynchronous method.
SendReport(AReportId: Integer; AData: TJSArrayBufferRecord) Sends a report to the selected and connected device/interface.
SetSelectedDevice(ADevice: THIDDevice) Sets the selected device/interface

Events for TWebUSBHID

Event Description
OnCloseDevice Event triggered when a device/interface connection is closed.
OnConnected Event triggered when a device is connected to computer and it is deceted by the browser.
OnDevicesInitialized Event triggered when the list of devices is initialized
OnDisconnected Event triggered when a device is removed from the computer.
OnError Event triggered when there is an error.
OnInputReport Event triggered when a report arrives from the connected device/interface
OnOpenDevice Event triggered when a device/interface connection is opened