Skip to content

ph-design/flutter_webhid

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

2 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

flutter_webhid

Dart bindings for the WebHID API using modern dart:js_interop.

This package has been tested and used in production environments. It provides access to HID devices from web browsers that support the WebHID standard (Chrome/Edge 89+).

Installation

dependencies:
  flutter_webhid: ^0.1.0

Usage

import 'package:flutter_webhid/flutter_webhid.dart';

void main() async {
  // Check if WebHID is supported
  if (!WebHID.isSupported) {
    print('WebHID not supported');
    return;
  }

  final webHid = WebHID.instance;
  if (webHid == null) return;

  // Request device access
  final devices = await webHid.requestDevice(
    RequestOptions(
      filters: [
        DeviceFilter(vendorId: 0x1234),
      ],
    ),
  );

  if (devices.isEmpty) return;
  final device = devices.first;

  // Open and use device
  await device.open();

  // Listen for input reports
  device.onInputReport.listen((event) {
    print('Report ${event.reportId}: ${event.data}');
  });

  // Send output report
  await device.sendReport(0, Uint8List.fromList([0x01, 0x02]));

  await device.close();
}

See example/basic_example.dart for more.

API

WebHID

  • WebHID.isSupported - Check if WebHID is available
  • WebHID.instance - Get the WebHID interface
  • requestDevice(options) - Request user permission for devices
  • getDevices() - Get previously authorized devices
  • onConnect / onDisconnect - Device connection events

Device

  • open() / close() / forget() - Device lifecycle
  • sendReport(reportId, data) - Send output report
  • sendFeatureReport(reportId, data) - Send feature report
  • receiveFeatureReport(reportId) - Receive feature report
  • onInputReport - Stream of input reports
  • vendorId, productId, productName - Device info
  • collections - HID collection descriptors

Types

  • RequestOptions - Device request options with filters
  • DeviceFilter - Filter by vendorId, productId, usagePage, usage
  • ConnectionEvent - Device connection/disconnection event
  • InputReportEvent - Input report with data
  • CollectionInfo, ReportInfo, ReportItem - HID descriptors

License

MIT

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages