Dart wrapper via `dart:js` for webusb

Overview

Dart wrapper via dart:js for https://wicg.github.io/webusb/

Features

  • canUseUsb
  • getDevices/requestDevice
  • open/close
  • reset
  • selectConfiguration
  • claimInterface/releaseInterface
  • transferIn/transferOut

Usage

canUseUsb

bool canUse = canUseUsb();
print('canUse $canUse');

getDevices/requestDevice

List<UsbDevice> getDevices = await usb.getDevices();
_device = getDevices[0];
UsbDevice requestDevice = await usb.requestDevice(RequestOptions(
  filters: [ledgerDeviceIds],
));
_device = requestDevice;

open/close

_device?.open().then((value) {
  print('device.open success');
}).catchError((error) {
  print('device.open $error');
});
_device?.close().then((value) {
  print('device.close success');
}).catchError((error) {
  print('device.close $error');
});

reset

https://developer.mozilla.org/en-US/docs/Web/API/USBDevice/reset

_device?.reset().then((value) {
  print('device.reset success');
}).catchError((error) {
  print('device.reset $error');
});

selectConfiguration

https://developer.mozilla.org/en-US/docs/Web/API/USBDevice/selectConfiguration


claimInterface/releaseInterface

_device?.claimInterface(_interface!.interfaceNumber).then((value) {
  print('device.claimInterface success');
}).catchError((error) {
  print('device.claimInterface $error');
});
_device?.releaseInterface(_interface!.interfaceNumber).then((value) {
  print('device.releaseInterface success');
}).catchError((error) {
  print('device.releaseInterface $error');
});

transferIn/transferOut

var usbInTransferResult = await _device!.transferIn(endpointNumber, packetSize);
print('usbInTransferResult ${usbInTransferResult.data.lengthInBytes}');
var usbOutTransferResult = await _device?.transferOut(endpointNumber, makeBlock(getAppAndVersion));
print('usbOutTransferResult ${usbOutTransferResult?.bytesWritten}');

Additional information

Status in Chromium: https://chromestatus.com/feature/5651917954875392

You might also like...

Camera and Microphone streaming library via RTMP for Flutter.

HaishinKit Plugin A Flutter plugin for iOS, Android. Camera and Microphone streaming library via RTMP. Android iOS Support SDK 21+ iOS 9.0+ 🌏 Depende

Nov 18, 2022

A most easily usable Duolingo API wrapper in Dart. Duolingo4D is an open-sourced Dart library.

A most easily usable Duolingo API wrapper in Dart! 1. About Duolingo4D Duolingo4D is an open-sourced Dart library. With Duolingo4D, you can easily int

Oct 17, 2022

A simple dart zeromq implementation/wrapper around the libzmq C++ library

dartzmq A simple dart zeromq implementation/wrapper around the libzmq C++ library Features Currently supported: Creating sockets (pair, pub, sub, req,

Dec 29, 2022

Unofficial wrapper for using Rapid7 insightOps logs (former LogEntries) with Dart.

An unofficial wrapper for using Rapid7 insightOps logs (former LogEntries) with Dart. This package is using logging package to do the actual logging,

Mar 3, 2021

A wrapper around our Cocoa and Java client library SDKs, providing iOS and Android support for those using Flutter and Dart.

A wrapper around our Cocoa and Java client library SDKs, providing iOS and Android support for those using Flutter and Dart.

Ably Flutter Plugin A Flutter plugin wrapping the ably-cocoa (iOS) and ably-java (Android) client library SDKs for Ably, the platform that powers sync

Dec 13, 2022

A Dart wrapper of the SMHI Open Data API

SMHI Open Data for Dart This package is in early development and some features may not work as intended. If so, feel free to submit a pull request. A

Sep 19, 2022

A MangaDex API wrapper for Dart/Flutter

A MangaDex API wrapper for Dart Usage A simple usage example: import 'package:mangadex_api/mangadex_api.dart'; main() { var client = MDClient();

Dec 2, 2022

A most easily usable RESAS API wrapper in Dart. With this library, you can easily integrate your application with the RESAS API.

A most easily usable RESAS API wrapper library in Dart! 1. About 1.1. What Is RESAS? 1.2. Introduction 1.2.1. Install Library 1.2.2. Import It 1.2.3.

Apr 7, 2022

A most easily usable JSON wrapper library in Dart

A most easily usable JSON response wrapper library in Dart! 1. About 1.1. Introd

Jan 4, 2022
Comments
  • Unable to send data in printable format to USB POS printer from flutter web

    Unable to send data in printable format to USB POS printer from flutter web

    Hi,

    First of all, thanks for your package and this help for me to connect my local mac machine POS USB printer from flutter web. Everything good and i able to connect and send the data. but the problem is, the data that is being printed in printer is not readable character. so i need to convert the data into correct format and send to transferOut api.

    Code is below.

    static Future<void> connectUsbPrinter() async {
       bool canUse = canUseUsb();
       if (canUse) {
        //* connect the specific USB printer device using the device's vendor id and product id.
         UsbDevice requestDevice = await usb.requestDevice(
           RequestOptions(
             filters: [
               RequestOptionsFilter(
                 vendorId: 0x04B8,
                 productId: 0x0E02,
               ),
             ],
           ),
         );
         
            await requestDevice.open();
            await requestDevice.reset();
            int firstInterfaceNumber =
            requestDevice.configuration?.interfaces[0].interfaceNumber ?? 0;
            await requestDevice.claimInterface(firstInterfaceNumber);
    
    
    //* i am using dart-pdf package to generate the pdf document with 80 mm size and if i print manually 
    //*the generated pdf from flutter web, then all is good. but my expectation is send the pdf data to printer directly but 
    based on the current limitation in dart-pdf, printing package, i was unable to send data to local printer directly from flutter web. 
    //*but later based on some research, i ended up your package to connect the local printer from flutter web using webusb package. 
    
              //* The below function actually generates the pdf and respond the data in Uint8List type. now i want to send this data to 
              //* your transferOut api to printer in printable format.
    
              final pdfDataInUint8ListType = await generatePdf(PdfPageFormat.roll80);
    
    //* here is the link for dart-pdf pacakge how to connect the printer and how to convert Uint8List type printable format.
    [https://github.com/DavBfr/dart_pdf/wiki/Document-Output#using-another-plugin]
    
             //* this is the api from web_usb to send data to device.  This is how i am sending today but its not printing in readable 
             //* format.
    
              var usbOutTransferResult =   await requestDevice.transferOut(1, pdfDataInUint8ListType);
              
             //* all of them are working and i am able to connect the local machine device from flutter web and able to send the data 
             //*but the problem is, data is being printed as undisplayable character in paper.  as per dart-pdf page example,  i see below code to convert from Uint8List to some printable format. I dont know whether i can use this to trasferOut api from this code returns List<int> type but transferOut api expected TypedData. your makeBlock function returns array length error.
             
      Future<void> printTicket() async {
      final printer = PrinterNetworkManager('192.168.0.123');
      final res = await printer.connect();
    
      if (res != PosPrintResult.success) {
        throw Exception('Unable to connect to the printer');
      }
    
      final profile = await CapabilityProfile.load();
      final generator = Generator(PaperSize.mm80, profile);
      var ticket = <int>[];
    
      await for (var page
          in Printing.raster(await _generateTicket(), dpi: dpi)) {
        final image = page.asImage();
        ticket += generator.image(image);
        ticket += generator.feed(2);
        ticket += generator.cut();
      }
    
      printer.printTicket(ticket);
      printer.disconnect();
    }
         
         //* ideally the question is, how to convert the Uint8List to printable character type and send to transferOut api. transferOut api accepts TypedData type to send data.
            
       }
    

    I looked at your example in the below link. but it seems to be ledger nano device. i want to do the same for USB printer. https://github.com/woodemi/web_usb.dart/blob/master/example/lib/ledger_nano_s_page.dart

    Can you please share example of converting Uint8List type TypedData so that it can print in readable character?

    Appreciate your response.

    opened by techttiru 3
Owner
Woodemi Co., Ltd
Woodemi Co., Ltd
A Dart Build Plugin that uploads debug symbols for Android, iOS/macOS and source maps for Web to Sentry via sentry-cli

Sentry Dart Plugin A Dart Build Plugin that uploads debug symbols for Android, iOS/macOS and source maps for Web to Sentry via sentry-cli. For doing i

Sentry 36 Jan 4, 2023
Package your Flutter app into OS-specific bundles (.dmg, .exe, etc.) via Dart or the command line.

flutter_distributor Package your Flutter app into OS-specific bundles (.dmg, .exe, etc.) via Dart or the command line. The flutter_distributor source

LeanFlutter 416 Dec 24, 2022
Immutable Dart collections via the builder pattern.

Built Collections for Dart Introduction Built Collections are immutable collections using the builder pattern. Each of the core SDK collections is spl

Google 250 Dec 20, 2022
QR.Flutter is a Flutter library for simple and fast QR code rendering via a Widget or custom painter.

QR.Flutter is a Flutter library for simple and fast QR code rendering via a Widget or custom painter. Need help? Please do not submit an issue for a "

Yakka 614 Jan 8, 2023
💳 A Flutter package for making payments via credo central. Provides support for both Android and iOS

?? Credo Package for Flutter TODO: Put a short description of the package here that helps potential users know whether this package might be useful fo

Samuel Abada 0 Dec 26, 2021
Sangre - Sangre streams your backend queries in realtime to your clients minimizing the load via diffs

Sangre Sangre streams your backend queries in realtime to your clients minimizin

P.O.M 5 Nov 27, 2022
Helper app to run code on Aliucord iOS via websocket.

aliucordebug A new Flutter project. Getting Started This project is a starting point for a Flutter application. A few resources to get you started if

Zoey 2 Jan 25, 2022
Flutter implementation for ExotikArch via a simple todos CRUD application.

ExotikArch - Flutter setState on steriods ⚡ ExotikArch for Flutter. Deliver production apps fast with flutter. Global State Management Navigation Serv

null 0 Jun 2, 2022
Add beautiful animated effects & builders in Flutter, via an easy, highly customizable unified API.

Flutter Animate A performant library that makes it simple to add almost any kind of animated effect in Flutter. Pre-built effects, like fade, scale, s

Grant Skinner 352 Dec 25, 2022
Integrating ChatMessaging via WebSocket (socket_io package) in Flutter Application

Chat Messaging via WebSocket Integrating ChatMessaging via WebSocket (socket_io_client package) in Flutter Application. The server is also built in Da

Saksham Gupta 2 Jul 26, 2022