Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/dotnet-bluetooth-le/dotnet-bluetooth-le
Bluetooth LE plugin for Xamarin/MAUI, supporting Android, iOS, Mac, Windows
https://github.com/dotnet-bluetooth-le/dotnet-bluetooth-le
ble bluetooth iot maui mvvmcross-plugin xamarin xamarin-android xamarin-forms xamarin-ios
Last synced: 5 days ago
JSON representation
Bluetooth LE plugin for Xamarin/MAUI, supporting Android, iOS, Mac, Windows
- Host: GitHub
- URL: https://github.com/dotnet-bluetooth-le/dotnet-bluetooth-le
- Owner: dotnet-bluetooth-le
- License: apache-2.0
- Created: 2015-09-10T14:20:09.000Z (over 9 years ago)
- Default Branch: master
- Last Pushed: 2024-12-18T09:26:14.000Z (about 1 month ago)
- Last Synced: 2025-01-09T18:23:40.688Z (12 days ago)
- Topics: ble, bluetooth, iot, maui, mvvmcross-plugin, xamarin, xamarin-android, xamarin-forms, xamarin-ios
- Language: C#
- Homepage:
- Size: 9.82 MB
- Stars: 862
- Watchers: 55
- Forks: 319
- Open Issues: 285
-
Metadata Files:
- Readme: README.md
- Funding: .github/FUNDING.yml
- License: LICENSE
Awesome Lists containing this project
README
# Bluetooth LE plugin for Xamarin & MAUI
Build status: [![Build status](https://github.com/dotnet-bluetooth-le/dotnet-bluetooth-le/actions/workflows/dotnet.yml/badge.svg)](https://github.com/dotnet-bluetooth-le/dotnet-bluetooth-le/actions/workflows/dotnet.yml?branch=master)
[Xamarin](https://github.com/xamarin), [MAUI](https://github.com/dotnet/maui) and [MvvMCross](https://github.com/MvvmCross) plugin for accessing the bluetooth functionality. The plugin is loosely based on the BLE implementation of [Monkey Robotics](https://github.com/xamarin/Monkey.Robotics).
**Important Note:** With the term *"vanilla"* we mean the non-MvvmCross version, i.e. the pure Xamarin or MAUI plugin. You **can** use it without MvvmCross, if you download the vanilla package.
## Support & Limitations
[Release Notes](doc/changelog.md)
| Platform | Version | Limitations |
| --------------- | ----------------- | ----------- |
| Xamarin.Android | 4.3 | |
| Xamarin.iOS | 7.0 | |
| Xamarin.Mac | 10.9 (Mavericks) | >= 2.1.0 |
| Xamarin.UWP | 1709 - 10.0.16299 | >= 2.2.0 |
| MAUI (Android, iOS, Mac, WinUI) | | >= 3.0.0 |## Nuget Packages
| package | stable | beta | downloads |
| ---------------------|:---------:|:---------:|:---------:|
| Plugin.BLE | [![NuGet](https://img.shields.io/nuget/v/Plugin.BLE.svg?style=flat)](https://www.nuget.org/packages/Plugin.BLE) | [![NuGet Beta](https://img.shields.io/nuget/vpre/Plugin.BLE.svg?style=flat)](https://www.nuget.org/packages/Plugin.BLE) | [![Downloads](https://img.shields.io/nuget/dt/Plugin.BLE.svg)](https://www.nuget.org/packages/Plugin.BLE)
| MvvmCross.Plugin.BLE | [![NuGet MvvMCross](https://img.shields.io/nuget/v/MvvmCross.Plugin.BLE.svg?style=flat)](https://www.nuget.org/packages/MvvmCross.Plugin.BLE) | [![NuGet MvvMCross Beta](https://img.shields.io/nuget/vpre/MvvmCross.Plugin.BLE.svg?style=flat)](https://www.nuget.org/packages/MvvmCross.Plugin.BLE) | [![Downloads](https://img.shields.io/nuget/dt/MvvmCross.Plugin.BLE.svg)](https://www.nuget.org/packages/MvvmCross.Plugin.BLE)## Installation
**Vanilla**
```
// stable
Install-Package Plugin.BLE
// or pre-release
Install-Package Plugin.BLE -Pre
```**MvvmCross**
```
Install-Package MvvmCross.Plugin.BLE
// or
Install-Package MvvmCross.Plugin.BLE -Pre
```## Permissions
### Android
Add these permissions to AndroidManifest.xml. For Marshmallow and above, please follow [Requesting Runtime Permissions in Android Marshmallow](https://devblogs.microsoft.com/xamarin/requesting-runtime-permissions-in-android-marshmallow/) and don't forget to prompt the user for the location permission.
```xml
```
Android 12 and above may require one or more of the following additional runtime permissions, depending on which features of the library you are using (see [the android Bluetooth permissions documentation](https://developer.android.com/guide/topics/connectivity/bluetooth/permissions))
```xml```
Add this line to your manifest if you want to declare that your app is available to BLE-capable devices **only**:
```xml````
### iOS
On iOS you must add the following keys to your `Info.plist`
```xml
UIBackgroundModes
bluetooth-central
bluetooth-peripheralNSBluetoothPeripheralUsageDescription
YOUR CUSTOM MESSAGENSBluetoothAlwaysUsageDescription
YOUR CUSTOM MESSAGE
````### MacOS
On MacOS (version 11 and above) you must add the following keys to your `Info.plist`:
```xml
NSBluetoothAlwaysUsageDescription
YOUR CUSTOM MESSAGE
````### UWP
Add this line to the Package Manifest (.appxmanifest):
```xml
```
## Sample app
We provide a sample Xamarin.Forms app, that is a basic bluetooth LE scanner. With this app, it's possible to
- check the BLE status
- discover devices
- connect/disconnect
- discover the services
- discover the characteristics
- see characteristic details
- read/write and register for notifications of a characteristicHave a look at the code and use it as starting point to learn about the plugin and play around with it.
## Usage
**Vanilla**
```csharp
var ble = CrossBluetoothLE.Current;
var adapter = CrossBluetoothLE.Current.Adapter;
```**MvvmCross**
The MvvmCross plugin registers `IBluetoothLE` and `IAdapter` as lazy initialized singletons. You can resolve/inject them as any other MvvmCross service. You don't have to resolve/inject both. It depends on your use case.
```csharp
var ble = Mvx.Resolve();
var adapter = Mvx.Resolve();
```
or
```csharp
MyViewModel(IBluetoothLE ble, IAdapter adapter)
{
this.ble = ble;
this.adapter = adapter;
}
```Please make sure you have this code in your LinkerPleaseLink.cs file
```csharp
public void Include(MvvmCross.Plugins.BLE.Plugin plugin)
{
plugin.Load();
}
```### IBluetoothLE
#### Get the bluetooth status
```csharp
var state = ble.State;
```
You can also listen for State changes. So you can react if the user turns on/off bluetooth on your smartphone.
```csharp
ble.StateChanged += (s, e) =>
{
Debug.WriteLine($"The bluetooth state changed to {e.NewState}");
};
```### IAdapter
#### Scan for devices
```csharp
adapter.DeviceDiscovered += (s,a) => deviceList.Add(a.Device);
await adapter.StartScanningForDevicesAsync();
```#### Scan Filtering
```csharp
var scanFilterOptions = new ScanFilterOptions();
scanFilterOptions.ServiceUuids = new [] {guid1, guid2, etc}; // cross platform filter
scanFilterOptions.ManufacturerDataFilters = new [] { new ManufacturerDataFilter(1), new ManufacturerDataFilter(2) }; // android only filter
scanFilterOptions.DeviceAddresses = new [] {"80:6F:B0:43:8D:3B","80:6F:B0:25:C3:15",etc}; // android only filter
await adapter.StartScanningForDevicesAsync(scanFilterOptions);
```##### ScanTimeout
Set `adapter.ScanTimeout` to specify the maximum duration of the scan.##### ScanMode
Set `adapter.ScanMode` to specify scan mode. It must be set **before** calling `StartScanningForDevicesAsync()`. Changing it while scanning, will not affect the current scan.#### Connect to device
`ConnectToDeviceAsync` returns a Task that finishes if the device has been connected successful. Otherwise a `DeviceConnectionException` gets thrown.```csharp
try
{
await _adapter.ConnectToDeviceAsync(device);
}
catch(DeviceConnectionException e)
{
// ... could not connect to device
}
```#### Connect to known Device
`ConnectToKnownDeviceAsync` can connect to a device with a given GUID. This means that if the device GUID is known, no scan is necessary to connect to a device. This can be very useful for a fast background reconnect.
Always use a cancellation token with this method.
- On **iOS** it will attempt to connect indefinitely, even if out of range, so the only way to cancel it is with the token.
- On **Android** this will throw a GATT ERROR in a couple of seconds if the device is out of range.```csharp
try
{
await _adapter.ConnectToKnownDeviceAsync(guid, cancellationToken);
}
catch(DeviceConnectionException e)
{
// ... could not connect to device
}
```#### Get services
```csharp
var services = await connectedDevice.GetServicesAsync();
```
or get a specific service:
```csharp
var service = await connectedDevice.GetServiceAsync(Guid.Parse("ffe0ecd2-3d16-4f8d-90de-e89e7fc396a5"));
```#### Get characteristics
```csharp
var characteristics = await service.GetCharacteristicsAsync();
```
or get a specific characteristic:
```csharp
var characteristic = await service.GetCharacteristicAsync(Guid.Parse("d8de624e-140f-4a22-8594-e2216b84a5f2"));
```#### Read characteristic
```csharp
var bytes = await characteristic.ReadAsync();
```#### Write characteristic
```csharp
await characteristic.WriteAsync(bytes);
```#### Characteristic notifications
```csharp
characteristic.ValueUpdated += (o, args) =>
{
var bytes = args.Characteristic.Value;
};await characteristic.StartUpdatesAsync();
```
#### Get descriptors
```csharp
var descriptors = await characteristic.GetDescriptorsAsync();
```#### Read descriptor
```csharp
var bytes = await descriptor.ReadAsync();
```#### Write descriptor
```csharp
await descriptor.WriteAsync(bytes);
```#### Get System Devices
Returns all BLE devices connected or bonded (only Android) to the system. In order to use the device in the app you have to first call ConnectAsync.
- For iOS the implementation uses get [retrieveConnectedPeripherals(services)](https://developer.apple.com/reference/corebluetooth/cbcentralmanager/1518924-retrieveconnectedperipherals)
- For Android this function merges the functionality of the following API calls:
- [getConnectedDevices](https://developer.android.com/reference/android/bluetooth/BluetoothManager.html#getConnectedDevices(int))
- [getBondedDevices()](https://developer.android.com/reference/android/bluetooth/BluetoothAdapter.html#getBondedDevices())```csharp
var systemDevices = adapter.GetSystemConnectedOrPairedDevices();
foreach(var device in systemDevices)
{
await _adapter.ConnectToDeviceAsync(device);
}```
## Caution! Important remarks / API limitationsThe BLE API implementation (especially on **Android**) has the following limitations:
- *Characteristic/Descriptor Write*: make sure you call characteristic.**WriteAsync**(...) from the **main thread**, failing to do so will most probably result in a GattWriteError.
- *Sequential calls*: **Always** wait for the previous BLE command to finish before invoking the next. The Android API needs its calls to be serial, otherwise calls that do not wait for the previous ones will fail with some type of GattError. A more explicit example: if you call this in your view lifecycle (onAppearing etc) all these methods return **void** and 100% don't guarantee that any await bleCommand() called here will be truly awaited by other lifecycle methods.
- *Scan with services filter*: On **specifically Android 4.3** the *scan services filter does not work* (due to the underlying android implementation). For android 4.3 you will have to use a workaround and scan without a filter and then manually filter by using the advertisement data (which contains the published service GUIDs).## Best practice
### API
- Surround Async API calls in try-catch blocks. Most BLE calls can/will throw an exception in certain cases, this is especially true for Android. We will try to update the xml doc to reflect this.
```csharp
try
{
await _adapter.ConnectToDeviceAsync(device);
}
catch(DeviceConnectionException ex)
{
//specific
}
catch(Exception ex)
{
//generic
}
```- **Avoid caching of Characteristic or Service instances between connection sessions**. This includes saving a reference to them in your class between connection sessions etc. After a device has been disconnected all Service & Characteristic instances become **invalid**. Always **use GetServiceAsync and GetCharacteristicAsync to get a valid instance**.
### General BLE iOS, Android
- Scanning: Avoid performing BLE device operations like Connect, Read, Write etc while scanning for devices. Scanning is battery-intensive.
- Try to stop scanning before performing device operations (connect/read/write/etc).
- Try to stop scanning as soon as you find the desired device.
- Never scan on a loop, and set a time limit on your scan.## How to build the nuget package
1) Build
Open a console, change to the folder "dotnet-bluetooth-le/.build" and run `cake`.
2) pack the nuget
`nuget pack ../Source/Plugin.BLE/Plugin.BLE.csproj`
`nuget pack ../Source/MvvmCross.Plugins.BLE/MvvmCross.Plugins.BLE.csproj`
## Extended topics
- [How to set custom trace method?](doc/howto_custom_trace.md)
- [Characteristic Properties](doc/characteristics.md)
- [Scan Mode Mapping](doc/scanmode_mapping.md)
- [iOS state restoration (basic support)](doc/ios_state_restoration.md)## Useful Links
- [Bluetooth Core Specification v4.2 (2014)](https://www.bluetooth.com/specifications/specs/core-specification-4-2/)
- [Bluetooth Core Specification v5.4 (2023)](https://www.bluetooth.com/specifications/specs/core-specification-5-4/)
- [Android Bluetooth LE guideline](https://developer.android.com/guide/topics/connectivity/bluetooth-le.html)
- [iOS CoreBluetooth Best Practices](https://developer.apple.com/library/ios/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/BestPracticesForInteractingWithARemotePeripheralDevice/BestPracticesForInteractingWithARemotePeripheralDevice.html)
- [iOS CoreBluetooth Background Modes](https://developer.apple.com/library/archive/documentation/NetworkingInternetWeb/Conceptual/CoreBluetooth_concepts/CoreBluetoothBackgroundProcessingForIOSApps/PerformingTasksWhileYourAppIsInTheBackground.html#//apple_ref/doc/uid/TP40013257-CH7-SW7)
- [Monkey Robotics](https://github.com/xamarin/Monkey.Robotics)## How to contribute
We usually do our development work on a branch with the name of the milestone. So please base your pull requests on the currently open development branch.
## Licence
[Apache 2.0](https://github.com/dotnet-bluetooth-le/dotnet-bluetooth-le/blob/master/LICENSE)