Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/KeudellCoding/Blazor.WebBluetooth
Blazor service for the experimental WebBluetooth functions.
https://github.com/KeudellCoding/Blazor.WebBluetooth
blazor webbluetooth
Last synced: 3 months ago
JSON representation
Blazor service for the experimental WebBluetooth functions.
- Host: GitHub
- URL: https://github.com/KeudellCoding/Blazor.WebBluetooth
- Owner: KeudellCoding
- License: mit
- Created: 2021-05-16T14:11:37.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2022-12-08T15:25:34.000Z (almost 2 years ago)
- Last Synced: 2023-09-17T05:22:22.498Z (about 1 year ago)
- Topics: blazor, webbluetooth
- Language: C#
- Homepage: https://www.nuget.org/packages/KeudellCoding.Blazor.WebBluetooth/
- Size: 15.6 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
- awesome-blazor - WebBluetooth - ![last commit](https://img.shields.io/github/last-commit/KeudellCoding/Blazor.WebBluetooth?style=flat-square&cacheSeconds=86400) Blazor service for the experimental WebBluetooth functions. Based on [Blazm.Bluetooth](https://github.com/EngstromJimmy/Blazm.Bluetooth). (Libraries & Extensions / Tools & Utilities)
README
# Blazor.WebBluetooth
## Installation
1. Install the NuGet package `KeudellCoding.Blazor.WebBluetooth`.
2. Add the following lines to _Imports.razor
```csharp
@using KeudellCoding.Blazor.WebBluetooth
@using KeudellCoding.Blazor.WebBluetooth.Models
```
3. Register Service
* Option 1: If you are using Blazor WebAssembly, add the following lines to Program.cs
```csharp
using KeudellCoding.Blazor.WebBluetooth;
builder.Services.AddWebBluetooth();
```
* Option 2: If you are using Blazor Server, add the following lines to the Startup.cs in the ConfigureServices function
```csharp
using KeudellCoding.Blazor.WebBluetooth;
services.AddWebBluetooth();
```## Usage Example
```csharp
@page "/"
@inject WebBluetoothControllerService __WebBluetooth
@if (isAvailable.HasValue) {
if (isAvailable == true) {
Connect
Disconnect
@if (connectedDevice != null) {
@connectedDevice.Id
@connectedDevice.Name
}
}
else {
Bluetooth not available!
}
}
else {
Loading Bluetooth...
}@code {
private bool? isAvailable = null;
private Device connectedDevice;protected override async Task OnAfterRenderAsync(bool firstRender) {
if (firstRender) {
isAvailable = await __WebBluetooth.IsAvailableAsync();
StateHasChanged();
}
}
private async Task connect() {
connectedDevice = await __WebBluetooth.RequestDeviceAsync(new RequestDeviceQuery() {
Filters = new List {
new Filter() {
Services = new List {
"<>"
}
}
}
});
StateHasChanged();
}
private async Task disconnect() {
if (connectedDevice == null) return;
await __WebBluetooth.DisconnectAsync(connectedDevice);
}
}
```## Notes
1. I programmed this project based on [EngstromJimmy](https://github.com/EngstromJimmy/Blazm.Bluetooth) and adapted it for my personal needs. Maybe it will help others too.
2. Currently, WebBluetooth is not yet supported by all browsers. A current implementation status can be found [here](https://github.com/WebBluetoothCG/web-bluetooth/blob/main/implementation-status.md).
3. WebBluetooth is a feature that is still under development. There may still be spontaneous massive changes in the JavaScript API that would result in this project no longer working.