Simple cross-platform Bluetooth LE library
-
reference the
triaxis.BluetoothLE
nuget from your MAUI or Xamarin application -
make the
IBluetoothLE
available to the applicationMAUI: call the
IServiceCollection.AddBluetoothLE ()
extension method during application startup:var builder = MauiApp.CreateBuilder(); // register platform-specific IBluetoothLE implementation in the container builder.Services.AddBluetoothLE();
Xamarin: you can use the the DependencyService to register the
triaxis.BluetoothLE.Platform
class as anIBluetoothLE
implementation -
observe the
IBluetoothLE.WhenAdapterChanges()
observable - it will always return at least the current adapter state -
when the returned
IAdapter.State
==AdapterState.On
, you can initiate a scan for advertisements usingIAdapter.Scan()
which returns another observable ofIAdvertisement
s -
you can use the
IAdvertisement
s to either just monitor nearby devices through the data exposed by the interface, or use theIAdvertisement.Peripheral
to connect to the device
For Android applications to be able to access Bluetooth LE, at least one of the location permissions must be requested, and the application must also ask the system for location access.
A simple trick to handle the location request is to use the Xamarin/MAUI essentials to retrieve the current location in the MainActivity
protected override void OnPostResume()
{
base.OnPostResume();
_ = Geolocation.GetLastKnownLocationAsync();
}
A reason string has to be provided in the corresponding Info.plist
file, e.g.:
<key>NSBluetoothAlwaysUsageDescription</key>
<string>This app scans for Bluetooth devices</string>
See the DeviceBrowser example for a very simple MAUI application that just scans for advertising devices
This package is licensed under the MIT License
Copyright © 2022 triaxis s.r.o.