Blazor.Bluetooth 1.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package Blazor.Bluetooth --version 1.0.1                
NuGet\Install-Package Blazor.Bluetooth -Version 1.0.1                
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.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Blazor.Bluetooth --version 1.0.1                
#r "nuget: Blazor.Bluetooth, 1.0.1"                
#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.1

// Install Blazor.Bluetooth as a Cake Tool
#tool nuget:?package=Blazor.Bluetooth&version=1.0.1                

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. In your wwwrooot/index.html add <script src="_content/Blazor.Bluetooth/JSInterop.js"></script>
  4. 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
  5. 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.

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  net5.0-windows was computed.  net6.0 was computed.  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 395 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

Supported all the implementation for Web Bluetooth API