Blazor.Bluetooth
1.0.6.2
dotnet add package Blazor.Bluetooth --version 1.0.6.2
NuGet\Install-Package Blazor.Bluetooth -Version 1.0.6.2
<PackageReference Include="Blazor.Bluetooth" Version="1.0.6.2" />
paket add Blazor.Bluetooth --version 1.0.6.2
#r "nuget: Blazor.Bluetooth, 1.0.6.2"
// Install Blazor.Bluetooth as a Cake Addin #addin nuget:?package=Blazor.Bluetooth&version=1.0.6.2 // Install Blazor.Bluetooth as a Cake Tool #tool nuget:?package=Blazor.Bluetooth&version=1.0.6.2
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: In your _Host.cshtml for .net5 and _Layout.cshtml for .net6 add
<script src="_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 RequestDeviceOptions 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 options = new RequestDeviceOptions { AcceptAllDevices = true };
var device = await BluetoothNavigator.RequestDevice(options);
Note: see full info about requests here Request Device Options
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.
- Fixed critical issue #1 to this library on Blazor Server App.
- Fixed issue with connecting only for Current device instead of connecting directly by the device ID
- Updated sample for testing bluetooth.getAvailability and bluetooth.getDevices.
- Fixed reading Characteristic properties.
- Add support for net8.
- Changed RequestDeviceQuery to RequestDeviceOptions
- Added all the options due to official docs, but not tested as have no possibility, also the question for services and identifiers, full info is here https://developer.mozilla.org/en-US/docs/Web/API/Bluetooth/requestDevice#options.
- Fixed critical issue with JSInterop.
- Added Forgot function for Bluetooth Device.
- Fixed Advertisements manufacturer data, service data, tx power properties.
- Upgraded advertisenets handlers to handle advertisent per device, not we can handle multiple advertisements for multiple device.
- Added BluetoothUUID to get UUID for service/characteristic/descriptor/canonical.
- Add GetCharacteristics without UUID parameter for Service.
- Add StartNotifications to handle for many characteristics.
Additionaly
- 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 is compatible. 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)
-
net8.0
- Microsoft.AspNetCore.Components.Web (>= 8.0.6)
- System.Net.Http.Json (>= 8.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 | 581 | 7/3/2024 |
1.0.6.1 | 119 | 7/2/2024 |
1.0.6 | 127 | 6/24/2024 |
1.0.5.6 | 2,041 | 5/16/2023 |
1.0.5.5 | 1,037 | 9/15/2022 |
1.0.5.4 | 458 | 9/14/2022 |
1.0.5.3 | 409 | 9/2/2022 |
1.0.5.2 | 425 | 9/2/2022 |
1.0.5.1 | 434 | 8/17/2022 |
1.0.5 | 417 | 8/17/2022 |
1.0.4 | 845 | 1/23/2022 |
1.0.3.1 | 445 | 1/21/2022 |
1.0.3 | 452 | 1/20/2022 |
1.0.2 | 420 | 1/17/2022 |
1.0.1 | 359 | 9/16/2021 |
1.0.0 | 350 | 9/16/2021 |
1. Added Forgot function for Bluetooth Device.
2. Fixed Advertisements manufacturer data, service data, tx power properties.
3. Upgraded advertisenets handlers to handle advertisent per device, not we can handle multiple advertisements for multiple device.
4. Added BluetoothUUID to get UUID for service/characteristic/descriptor/canonical.
5. Add GetCharacteristics without UUID parameter for Service.
6. Add StartNotifications to handle for many characteristics.