Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/AspNetMonsters/Blazor.Sensors

Blazor interop for browers sensor apis
https://github.com/AspNetMonsters/Blazor.Sensors

Last synced: 18 days ago
JSON representation

Blazor interop for browers sensor apis

Awesome Lists containing this project

README

        

# AspNetMonsters.Blazor.Sensors
This package provides Blazor applications with access to the browser sensor apis. Currently only the [AmbientLightSensor api](https://developer.mozilla.org/en-US/docs/Web/API/AmbientLightSensor) is supported.

## Usage
1) In your Blazor app, add the `AspNetMonsters.Blazor.Sensors` [NuGet package](https://www.nuget.org/packages/AspNetMonsters.Blazor.Sensors/)

```
Install-Package AspNetMonsters.Blazor.Sensors -IncludePrerelease
```

1) In your Blazor app's `Startup.cs` `ConfigureServices` method, register the `AmbientLightSensorService`.

```
services.AddSingleton();
```

1) Now you can inject the `AmbientLightSensorService` into any Blazor page and use it like this:

```
@using AspNetMonsters.Blazor.Sensors
@inject AmbientLightSensorService sensorService

Let there be light!

@sensor?.Illuminance

Stop
Start

@functions
{
AmbientLightSensor sensor;

protected override async Task OnInitAsync()
{
await Start();
}

private async Task Start()
{
sensor = await sensorService.StartReading(() =>
{
StateHasChanged();
});
}

private async Task Stop()
{
if (sensor != null)
{
await sensorService.StopReading(sensor);
sensor = null;
}
}
}
```