Giter Club home page Giter Club logo

webusb's Introduction

Canvas

HTML5 WebUSB API implementation for Microsoft Blazor

Build Package Version NuGet Downloads License

Blazor Extensions

Blazor Extensions are a set of packages with the goal of adding useful things to Blazor.

Blazor Extensions WebUSB

This package wraps HTML5 WebUSB APIs.

Installation

Install-Package Blazor.Extensions.WebUSB

Sample

Usage

  • First add the USB services on Blazor IServiceCollection:
public void ConfigureServices(IServiceCollection services)
{
    services.UseWebUSB(); // Makes IUSB available to the DI container
}

To consume on your .cshtml:

  • On your _ViewImports.cshtml add the using entry:
@using Blazor.Extensions.WebUSB
  • Then, on your .cshtml inject the IUSB:
@inject IUSB usb

And then use the usb object to interact with connected USB devices thru your Blazor application.

To inject on a BlazorComponent class:

Define a property of type IUSB and mark it as [Injectable]:

[Inject] private IUSB _usb { get; set; }

Then use the _usb variable to interact with the connected USB devices.

Note: For now, you have to call await IUSB.Initialize() once in your application. This is a temporary requirement and we are looking on a better way to automatically register to Connect/Disconnect events.

Contributions and feedback

Please feel free to use the component, open issues, fix bugs or provide feedback.

Contributors

The following people are the maintainers of the Blazor Extensions projects:

webusb's People

Contributors

galvesribeiro avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

webusb's Issues

Error: Could not find 'WebUSB' in 'window.BlazorExtensions'

Getting a JSInterop Exception when using this extension:

[Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer] Exception: Microsoft.JSInterop.JSException: Could not find 'WebUSB' in 'window.BlazorExtensions'.
Error: Could not find 'WebUSB' in 'window.BlazorExtensions'.

Followed instructions and checked test implementation in source code, cannot find anything different in my code.

Thanks.

Fix the hack to support TypedArray

This package is basically send/receive calls using byte[] which is translated on the JS side to Uint8Array.

Currently Blazor fail to deserialize Uint8Array to byte[] on the C# side. We are tracking an issue on Blazor repo here https://github.com/aspnet/Blazor/issues/1618 and as soon as it get fixed, we should update this component since the current implementation is not efficient.

For now we are translating the response buffer to a regular array by using this hack: https://github.com/BlazorExtensions/WebUSB/blob/master/src/Blazor.Extensions.WebUSB.JS/src/USBManager.ts#L181

NET 8

Do you have example for NET 8 Blazor client?

Could not find 'BlazorExtensions' in 'window'.

I just installed the webusb extension in my first blazor project. Unfortunately it gives me this error on the console.

Similar errors in the issuelists of other blazor extensions lead me to believe that this is caused by newer versions of blazor breaking something related to static content. But nothing I found suggested worked for me.

As to recreate, I just started a new blazor app from the vs template and added the extension. I also added the code from this project its testpage.

blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100]
Unhandled exception rendering component: Could not find 'BlazorExtensions' in 'window'.
Error: Could not find 'BlazorExtensions' in 'window'.
at https://localhost:44355/_framework/blazor.webassembly.js:1:9130
at Array.forEach ()
at p (https://localhost:44355/_framework/blazor.webassembly.js:1:9090)
at https://localhost:44355/_framework/blazor.webassembly.js:1:9800
at new Promise ()
at Object.beginInvokeJSFromDotNet (https://localhost:44355/_framework/blazor.webassembly.js:1:9773)
at _mono_wasm_invoke_js_marshalled (https://localhost:44355/_framework/wasm/dotnet.3.2.0.js:1:171294)
at do_icall (https://localhost:44355/_framework/wasm/dotnet.wasm:wasm-function[6049]:0x10f8b1)
at do_icall_wrapper (https://localhost:44355/_framework/wasm/dotnet.wasm:wasm-function[1896]:0x50b6a)
at interp_exec_method (https://localhost:44355/_framework/wasm/dotnet.wasm:wasm-function[1120]:0x2588e)
Microsoft.JSInterop.JSException: Could not find 'BlazorExtensions' in 'window'.
Error: Could not find 'BlazorExtensions' in 'window'.
at https://localhost:44355/_framework/blazor.webassembly.js:1:9130
at Array.forEach ()
at p (https://localhost:44355/_framework/blazor.webassembly.js:1:9090)
at https://localhost:44355/_framework/blazor.webassembly.js:1:9800
at new Promise ()
at Object.beginInvokeJSFromDotNet (https://localhost:44355/_framework/blazor.webassembly.js:1:9773)
at _mono_wasm_invoke_js_marshalled (https://localhost:44355/_framework/wasm/dotnet.3.2.0.js:1:171294)
at do_icall (https://localhost:44355/_framework/wasm/dotnet.wasm:wasm-function[6049]:0x10f8b1)
at do_icall_wrapper (https://localhost:44355/_framework/wasm/dotnet.wasm:wasm-function[1896]:0x50b6a)
at interp_exec_method (https://localhost:44355/_framework/wasm/dotnet.wasm:wasm-function[1120]:0x2588e)
at System.Threading.Tasks.ValueTask`1[TResult].get_Result () <0x2df6dd0 + 0x00034> in :0
at Blazor.Extensions.WebUSB.USB.Initialize () [0x00041] in /home/runner/work/WebUSB/WebUSB/src/Blazor.Extensions.WebUSB/USB.cs:78
at Microsoft.AspNetCore.Components.RenderTree.Renderer.GetErrorHandledTask (System.Threading.Tasks.Task taskToHandle) <0x2db6878 + 0x000b6> in :0

Access denied on Device.Open()

Hi,
I use Blazor wasm project and after I choose device in Chrome, I get:

My call:

var requestedDevices = await _usb.RequestDevice(); bool opened = requestedDevices.Opened; Console.WriteLine("Conbox opened state: " + opened); await requestedDevices.Open();

Response:
blazor.webassembly.js:1 crit: Microsoft.AspNetCore.Components.WebAssembly.Rendering.WebAssemblyRenderer[100] Unhandled exception rendering component: Access denied. undefined Microsoft.JSInterop.JSException: Access denied. undefined at System.Threading.Tasks.ValueTask1[TResult].get_Result () <0x306d990 + 0x00034> in :0
at Blazor.Extensions.WebUSB.USBDeviceMethods.Open (Blazor.Extensions.WebUSB.USBDevice device) [0x0003c] in /home/runner/work/WebUSB/WebUSB/src/Blazor.Extensions.WebUSB/USBDevice.Methods.cs:25 `

Connect/Disconnect is unstable

Regardless of the user being listening or not to Connect/Disconnect events, this component should list to it from the navigator.usb connect/disconnect events.

Right now, it requires a call to a hack method IUSB.Initialize() and connect is never fired for whatever reason while disconnect sometimes is fired.

We should fix it...

Collab

Hi,

Really stoked to find this library. Let's collaborate. This is my framework. I'd like to write an implementation for it using your library.

https://github.com/MelbourneDeveloper/Device.Net

I'm currently trying to get your library working.

Code:

@page "/counter"
@inject IUSB usb

<h1>Counter</h1>

<p>Current count: @currentCount</p>

<button class="btn btn-primary" @onclick="IncrementCount">Click me</button>

@code {

    [Inject] private IUSB _usb { get; set; }

    private int currentCount = 0;

    private async Task IncrementCount()
    {
        await _usb.Initialize();
        var devices = await _usb.GetDevices();
        currentCount = devices.Count();
    }
}

But I get this error:
image

Any ideas?

Christian

Cannot consume scoped service 'Microsoft.JSInterop.IJSRuntime' from singleton 'Blazor.Extensions.WebUSB.IUSB'.

When trying to start the application after installing WebUSB, the app crashes with the following message:
An error occurred while starting the application.
AggregateException: Some services are not able to be constructed (Error while validating the service descriptor 'ServiceType: Blazor.Extensions.WebUSB.IUSB Lifetime: Singleton ImplementationType: Blazor.Extensions.WebUSB.USB': Cannot consume scoped service 'Microsoft.JSInterop.IJSRuntime' from singleton 'Blazor.Extensions.WebUSB.IUSB'.)
Microsoft.Extensions.DependencyInjection.ServiceProvider..ctor(IEnumerable serviceDescriptors, ServiceProviderOptions options)

InvalidOperationException: Error while validating the service descriptor 'ServiceType: Blazor.Extensions.WebUSB.IUSB Lifetime: Singleton ImplementationType: Blazor.Extensions.WebUSB.USB': Cannot consume scoped service 'Microsoft.JSInterop.IJSRuntime' from singleton 'Blazor.Extensions.WebUSB.IUSB'.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.