Blazm.Bluetooth makes it easy to connect Blazor to your Bluetooth devices using Web Bluetooth.
Works both Client-side and Server-side.
- Add Nuget package Blazm.Bluetooth
- In Program.cs add
builder.Services.AddBlazmBluetooth();
- In your wwwrooot/index.html add
<script src="_content/Blazm.Bluetooth/JSInterop.js"></script>
- In the component you want to connect to a device add the Blazm.Bluetooth Namespace
@using Blazm.Bluetooth
- Inject the BluetoothNavigator (the instance that will communicate with your device)
@inject BluetoothNavigator navigator
Now you are all setup now it is time to connect to a device.
You need to create a query or a filter to filter devices, you could also specify AcceptAllDevices
but that is only recommended while testing.
To take a look at available devices you can use (for Microsoft Edge) edge://bluetooth-internals.
You can query devices using a ServiceId or by name, you also have to specify all the services that you want to access in OptionalServices
To connect to an Andersson (SenSun) scale you need to do the following (check Pages/AnderssonScaleDemo for more info) Specify the ServiceId and CharacteristicId you want to communicate with.
var serviceid = "0000ffb0-0000-1000-8000-00805f9b34fb";
var characteristicId = "0000ffb2-0000-1000-8000-00805f9b34fb";
Create a filter
var q = new RequestDeviceQuery();
q.Filters.Add(new Filter() { Services = { serviceid } });
Request a device
var device = await navigator.RequestDeviceAsync(q);
This will return a device and it contains an id that you can use to read, write, or set up notifications.
Call the SetupNotifyAsync
to get notifications when the value changes.
await navigator.SetupNotifyAsync(device.Id, serviceid, characteristics);
navigator.Notification += Value_Notification;
and add an event listener that parses the data from the notification.
private void Value_Notification(object sender, CharacteristicEventArgs e)
{
var data = e.Value.ToArray();
// Do something with the data
StateHasChanged();
}
The same thing goes for read and write
//Write
await navigator.WriteValueAsync(device.Id, serviceId, characteristicId, bytearray);
//Read
var value = await navigator.ReadValueAsync(device.Id, serviceId, characteristicId);
There is still many scenarios to implement but this should cover the basics.
Please feel free to add ideas /problems/needs you might have or make a PR.