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                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Blazor.Bluetooth" Version="1.0.6.2" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blazor.Bluetooth --version 1.0.6.2                
#r "nuget: Blazor.Bluetooth, 1.0.6.2"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// 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

  1. Add Nuget package Blazor.Bluetooth
  2. In Program.cs add builder.Services.AddBluetoothNavigator();
  3. 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>
  1. 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
  2. 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

  1. 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.

  1. Added RequestDeviceCancelledException - by handling this exception user can just inore it, as it indicate, user clicked on cancel button.
  2. Removed Newtonsoft.Json as we can use System.Text.Json
  3. Filter.Services / RequestDeviceQuery.Filters / RequestDeviceQuery.OptionalServices are null by default, and removed useless methods ShouldSerialize_.
  1. Fixed issue if user try to connect to one device, then disconnect, then try to connect to another device.
  2. Fixed issue with Paired bluetooth devices list, it cause issue if you trying to talk to device if you reconnected to the same device.
  1. Added Watch advertisements to the IDevice
  2. Updated tests, also uploaded tester so you can test your device without prepearing IDE.
  3. Fixed all event subscribtions to run synchronously, because it was run method faster then event was subscribet inside JSInteropt.js.
  1. Fixed critical issue #1 to this library on Blazor Server App.
  1. Fixed issue with connecting only for Current device instead of connecting directly by the device ID
  2. Updated sample for testing bluetooth.getAvailability and bluetooth.getDevices.
  1. Fixed reading Characteristic properties.
  2. Add support for net8.
  3. Changed RequestDeviceQuery to RequestDeviceOptions
  4. 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.
  1. Fixed critical issue with JSInterop.
  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.

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 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. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

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 413 7/3/2024
1.0.6.1 112 7/2/2024
1.0.6 119 6/24/2024
1.0.5.6 1,971 5/16/2023
1.0.5.5 1,031 9/15/2022
1.0.5.4 454 9/14/2022
1.0.5.3 405 9/2/2022
1.0.5.2 420 9/2/2022
1.0.5.1 430 8/17/2022
1.0.5 413 8/17/2022
1.0.4 841 1/23/2022
1.0.3.1 441 1/21/2022
1.0.3 446 1/20/2022
1.0.2 416 1/17/2022
1.0.1 355 9/16/2021
1.0.0 346 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.