Git Product home page Git Product logo

blazm.bluetooth's Introduction

How to use Blazm.Bluetooth

Blazm.Bluetooth makes it easy to connect Blazor to your Bluetooth devices using Web Bluetooth.

Works both Client-side and Server-side.

Sponsors

Thanks you to much to my sponsors!

Telerik UI for Blazor โ€“ Increase productivity and cut cost in half! Use the Telerik truly native Blazor UI components to cover any app scenario. Give it a try.

Getting Started

  1. Add Nuget package Blazm.Bluetooth
  2. In Program.cs add builder.Services.AddBlazmBluetooth();
  3. In the component you want to connect to a device add the Blazm.Bluetooth Namespace @using Blazm.Bluetooth
  4. Inject the IBluetoothNavigator (the instance that will communicate with your device) @inject IBluetoothNavigator navigator

Now you are all setup now it is time to connect to a device.

Connecting to a device

You need to create a query or a filter to filter devices, you could also specify AcceptAllDevices but that is only recommended while testing. To take a look at available devices you can use (for Microsoft Edge) edge://bluetooth-internals. You can query devices using a ServiceId or by name, you also have to specify all the services that you want to access in OptionalServices

To connect to an Andersson (SenSun) scale you need to do the following (check Pages/AnderssonScaleDemo for more info) Specify the ServiceId and CharacteristicId you want to communicate with.

var serviceId = "0000ffb0-0000-1000-8000-00805f9b34fb";
var characteristicId = "0000ffb2-0000-1000-8000-00805f9b34fb";

Create a filter

var q = new RequestDeviceQuery();
q.Filters.Add(new Filter() { Services = { serviceId } });

Request a device

var device = await navigator.RequestDeviceAsync(q);

This will return a device and it contains an id that you can use to read, write, or set up notifications. Call the SetupNotifyAsync to get notifications when the value changes.

await navigator.SetupNotifyAsync(device, serviceId, characteristicId);
navigator.Notification += Value_Notification;

and add an event listener that parses the data from the notification.

private void Value_Notification(object sender, CharacteristicEventArgs e)
{
    var data = e.Value.ToArray();
    
    // Do something with the data

    StateHasChanged();
}

The same thing goes for read and write

//Write
await navigator.WriteValueAsync(device.Id, serviceId, characteristicId, bytearray);

//Read
var value = await navigator.ReadValueAsync(device.Id, serviceId, characteristicId);

There is still many scenarios to implement but this should cover the basics.

Please feel free to add ideas /problems/needs you might have or make a PR.

blazm.bluetooth's People

Contributors

engstromjimmy avatar czlatea avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.