Blazor.Bluetooth 1.0.5.4

There is a newer version of this package available.
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                
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.5.4" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blazor.Bluetooth --version 1.0.5.4                
#r "nuget: Blazor.Bluetooth, 1.0.5.4"                
#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.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

  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: 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>

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

  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.

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