Blazor.Bluetooth
1.0.5.4
See the version list below for details.
dotnet add package Blazor.Bluetooth --version 1.0.5.4
NuGet\Install-Package Blazor.Bluetooth -Version 1.0.5.4
<PackageReference Include="Blazor.Bluetooth" Version="1.0.5.4" />
paket add Blazor.Bluetooth --version 1.0.5.4
#r "nuget: Blazor.Bluetooth, 1.0.5.4"
// Install Blazor.Bluetooth as a Cake Addin #addin nuget:?package=Blazor.Bluetooth&version=1.0.5.4 // Install Blazor.Bluetooth as a Cake Tool #tool nuget:?package=Blazor.Bluetooth&version=1.0.5.4
How to use Blazor.Bluetooth
Blazor.Bluetooth makes it easy to connect Blazor to your Bluetooth devices using Web Bluetooth.
Works both Client-side and Server-side.
Getting Started
- Add Nuget package Blazor.Bluetooth
- In Program.cs add
builder.Services.AddBluetoothNavigator();
- Add JSInterop.js to the project
For the Client: In your wwwrooot/index.html add
<script src="_content/Blazor.Bluetooth/JSInterop.js"></script>
For the Server: Add the script below script into _Host.cshtml for .net5 and _Layout.cshtml for .net6
<script src="https://blazorbluetooth.azurewebsites.net/_content/Blazor.Bluetooth/JSInterop.js"></script>
- In the component you want to connect to a device add the Blazor.Bluetooth Namespace
@using Blazor.Bluetooth
or add it to your_Imports.razor
- Inject the IBluetoothNavigator (the instance that will communicate with your device)
@inject IBluetoothNavigator navigator
You can check and run SampleClientSide to test device connection. But right now let's see what we can do here
How to use
Bluetooth
First of all you can check if user's browser support Bluetooth connection by calling IBluetoothNavigator.GetAvailability
Request a device
Create a RequestDeviceQuery with all the options you would like to get a device. But note that it's important if AcceptAllDevices is true, then do not set Filters, other ways you have to fill Filters.
var query = new RequestDeviceQuery { AcceptAllDevices = true };
var device = await BluetoothNavigator.RequestDevice(query);
Connect/Disconnect to the device
Connect to the device.
await device.Gatt.Connect();
Note: that for my case I want to able to connect to the device all the time, sometimes it took like 5 times to connect, so it would be nice you to implement some service to run connection for few times untill you will be connected or time out. It you failed to connect, you will get an Exception
Disconnect from the device will raise IDevice.OnGattServerDisconnected event.
await Device.Gatt.Disonnect();
Note: Sometimes to reconnect to the device you will have to wait, it dependec on a device but for my case I needed to wait up to 3 sec. await Task.Delay(3000);
Get characteristic to read/write and get notifications
To read or write a byte array you have to get characteristic, (or descriptor it dependes). Also you can check if your characteristic support write or read.
var service = await device.Gatt.GetPrimaryService("Your service UUID");
var characteristic = await service.GetCharacteristic("Your characteristic UUID");
if (characteristic.Properties.Write)
{
characteristic.OnRaiseCharacteristicValueChanged += (sender, e) => { };
await characteristic.StartNotifications();
await characteristic.WriteValueWithResponse(/* Your byte array */);
}
Note: do not forget to unsubscribe from the event and call StopNotifications.
Others
I didn't describe all the functionality, some of them I didn't have possibility to test so there could be some issues and I would like to hear from you a feedback to make this nuget fully work.
Release notes
- IBluetoothRemoteGATTServer has a new implementation to GetConnected
Why? It means you will check if device connected on runtime. Because the property Connect will be updated only by your actions Connect/Disconnect/GetConnected, so we need to actually check if Connected still connected to device, this method can help. Consider of using this method instead. One more think, we have a bug related to after Connect we not always connected successfully, but Connect is true, but after some time, we receive exceptions as device is disconnected. Consider to connect to device, wait some time, up to 1 sec I guess, then check if you are connected by calling Connect method. I didn't implement this functionality inside Blazor.Bluetooth, because this is only a mirror for real web bluetooth, so this is not a part of real lib.
- Added RequestDeviceCancelledException - by handling this exception user can just inore it, as it indicate, user clicked on cancel button.
- Removed Newtonsoft.Json as we can use System.Text.Json
- Filter.Services / RequestDeviceQuery.Filters / RequestDeviceQuery.OptionalServices are null by default, and removed useless methods ShouldSerialize_.
- Fixed issue if user try to connect to one device, then disconnect, then try to connect to another device.
- Fixed issue with Paired bluetooth devices list, it cause issue if you trying to talk to device if you reconnected to the same device.
- Added Watch advertisements to the IDevice
- Updated tests, also uploaded tester so you can test your device without prepearing IDE.
- Fixed all event subscribtions to run synchronously, because it was run method faster then event was subscribet inside JSInteropt.js.
Additional
- Some features are working only with Experimental feature activated, for example input this reference into your browser (chrome/edge) about:flags/#enable-web-bluetooth-new-permissions-backend and enable it. Or more general option about:flags/#enable-experimental-web-platform-features.
- Try Blazor.Bluetooth Web tester, so you do not have to run the code
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net5.0 is compatible. net5.0-windows was computed. net6.0 is compatible. net6.0-android was computed. net6.0-ios was computed. net6.0-maccatalyst was computed. net6.0-macos was computed. net6.0-tvos was computed. net6.0-windows was computed. net7.0 was computed. net7.0-android was computed. net7.0-ios was computed. net7.0-maccatalyst was computed. net7.0-macos was computed. net7.0-tvos was computed. net7.0-windows was computed. net8.0 was computed. net8.0-android was computed. net8.0-browser was computed. net8.0-ios was computed. net8.0-maccatalyst was computed. net8.0-macos was computed. net8.0-tvos was computed. net8.0-windows was computed. |
-
net5.0
- Microsoft.AspNetCore.Components.Web (>= 5.0.10)
- System.Net.Http.Json (>= 5.0.0)
-
net6.0
- Microsoft.AspNetCore.Components.Web (>= 6.0.9)
- System.Net.Http.Json (>= 6.0.0)
NuGet packages (1)
Showing the top 1 NuGet packages that depend on Blazor.Bluetooth:
Package | Downloads |
---|---|
Oarw.Blazor.InputScan
Provides an input control for use in a blazor form that combines multiple meethods of barcode and QR code scanning. |
GitHub repositories
This package is not used by any popular GitHub repositories.
Version | Downloads | Last updated |
---|---|---|
1.0.6.2 | 320 | 7/3/2024 |
1.0.6.1 | 108 | 7/2/2024 |
1.0.6 | 118 | 6/24/2024 |
1.0.5.6 | 1,950 | 5/16/2023 |
1.0.5.5 | 1,022 | 9/15/2022 |
1.0.5.4 | 453 | 9/14/2022 |
1.0.5.3 | 404 | 9/2/2022 |
1.0.5.2 | 417 | 9/2/2022 |
1.0.5.1 | 429 | 8/17/2022 |
1.0.5 | 411 | 8/17/2022 |
1.0.4 | 840 | 1/23/2022 |
1.0.3.1 | 440 | 1/21/2022 |
1.0.3 | 445 | 1/20/2022 |
1.0.2 | 414 | 1/17/2022 |
1.0.1 | 353 | 9/16/2021 |
1.0.0 | 345 | 9/16/2021 |
1. Supported .net5/.net6
2. Changed exception if no script found.