LSconnect.Bluetooth.Android
4.0.0
dotnet add package LSconnect.Bluetooth.Android --version 4.0.0
NuGet\Install-Package LSconnect.Bluetooth.Android -Version 4.0.0
<PackageReference Include="LSconnect.Bluetooth.Android" Version="4.0.0" />
<PackageVersion Include="LSconnect.Bluetooth.Android" Version="4.0.0" />
<PackageReference Include="LSconnect.Bluetooth.Android" />
paket add LSconnect.Bluetooth.Android --version 4.0.0
#r "nuget: LSconnect.Bluetooth.Android, 4.0.0"
#:package LSconnect.Bluetooth.Android@4.0.0
#addin nuget:?package=LSconnect.Bluetooth.Android&version=4.0.0
#tool nuget:?package=LSconnect.Bluetooth.Android&version=4.0.0
Architecture
This package provides the Android implementation of the LSconnect.Bluetooth abstraction layer.
The solution is structured into:
- platform-agnostic contract layer
- platform-specific implementation layer
Cross-Platform Support
This package is multi-targeted:
net8.0– platform-agnostic abstraction layernet8.0-android– Android implementation using InTheHand.Net.Bluetooth
The shared API is available in the LSconnect.Bluetooth namespace and can be used from common (.NET MAUI shared) code.
On Android, the factory returns a fully functional Bluetooth implementation.
On non-Android platforms (iOS, Windows, macOS), the factory returns a stub implementation that throws PlatformNotSupportedException.
LSconnect.Bluetooth.Android
A lightweight Bluetooth Serial wrapper for .NET MAUI Android applications, designed for communication with devices that send text-based data over Bluetooth.
This package is specialized for a milk analyzer that outputs tab-separated measurement results, but the Bluetooth API remains generic and reusable.
Features
- Bluetooth device discovery (
SearchAsync) - Optional filtering of paired-only devices (
pairedOnly) - Connecting to a Bluetooth device (
ConnectAsync) - Sending text (
SendTextAsync) - Receiving text (
ReceiveTextAsync) - Explicit disconnection (
DisconnectAsync) - Utility helpers for text processing (
BluetoothTextUtils) - High-level API for reading milk quality results:
ReadQualityParametersAsync()→ returnsQualityResult
Internally uses InTheHand.Net.Bluetooth.
Installation
- Add the NuGet package LSconnect.Bluetooth.Android to your .NET MAUI Android project.
- Ensure your project targets
net8.0-android. - Declare and request the required Bluetooth permissions.
Android Permissions
This library does not request runtime permissions automatically.
Your application must declare and request them manually.
Example Platforms/Android/AndroidManifest.xml:
xml <uses-permission android:name="android.permission.BLUETOOTH" /> <uses-permission android:name="android.permission.BLUETOOTH_ADMIN" /> <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.BLUETOOTH_SCAN" /> <uses-permission android:name="android.permission.BLUETOOTH_CONNECT" />
Usage (from shared MAUI code)
csharp using LSconnect.Bluetooth;
var terminal = BluetoothTerminalFactory.Create();
var devices = await terminal.SearchAsync(pairedOnly: true);
await terminal.ConnectAsync(devices.First());
var result = await terminal.ReadQualityParametersAsync();
await terminal.DisconnectAsync();
Basic usage
Create a terminal
IBluetoothTerminal terminal = BluetoothTerminalFactory.Create();
Search for devices
var devices = await terminal.SearchAsync(); var pairedDevices = await terminal.SearchAsync(pairedOnly: true);
Connect to a device
await terminal.ConnectAsync(device);
Send text
await terminal.SendTextAsync("Hello!");
Receive text
var response = await terminal.ReceiveTextAsync();
Disconnect
await terminal.DisconnectAsync();
Paired Devices
When SearchAsync(pairedOnly: true) is used on Android, the package reads the bonded device list directly from the Android Bluetooth adapter.
This makes paired device discovery more reliable than filtering discovery results by authentication state.
Or use automatic disposal:
await using var terminal = BluetoothTerminalFactory.Create();
Connect by Known Device Address
The package also supports direct connection using a previously known Bluetooth device address:
using LSconnect.Bluetooth;
var terminal = BluetoothTerminalFactory.Create();
await terminal.ConnectAsync("00:11:22:33:44:55");
Milk Analyzer Integration
The analyzer sends a single line of tab-separated data in the following format:
"RES\t" + <fields> + "\r\n"
The prefix RES indicates that measurement results follow. All fields are separated by TAB (\t).
QualityResult Model
public class QualityResult { public string Date { get; set; } public string Time { get; set; } public string Supplier { get; set; } public string DeliveryName { get; set; } public string Litres { get; set; } public string SerialNumber { get; set; }
public double Cal { get; set; }
public double Temp { get; set; }
public double Fat { get; set; }
public double SNF { get; set; }
public double Density { get; set; }
public double Protein { get; set; }
public double Lactose { get; set; }
public double Water { get; set; }
public double Solids { get; set; }
public double Conductivity { get; set; }
public double Ph { get; set; }
public double FreezingPoint { get; set; }
public double TotalSolids { get; set; }
}
Reading a Measurement Result
var result = await terminal.ReadQualityParametersAsync();
if (result != null) { Console.WriteLine($"Fat: {result.Fat}"); Console.WriteLine($"Density: {result.Density}"); }
ReadQualityParametersAsync() performs:
Reading one Bluetooth message
Normalizing CR/LF
Splitting the line by TAB
Validating the prefix RES
Parsing fields into QualityResult
Converting numeric values using invariant culture
Utility: BluetoothTextUtils public static class BluetoothTextUtils { public static string Normalize(string? input); public static string[] SplitFields(string? input, char separator); public static string[] SplitTabSeparated(string? input); }
Important Notes
- Bluetooth functionality is supported only on Android.
- The package can still be referenced from shared MAUI code because it provides a platform-agnostic abstraction layer.
- On non-Android platforms, Bluetooth calls throw
PlatformNotSupportedException. - Android Bluetooth permissions must still be declared and requested by the application.
SearchAsync(pairedOnly: true)uses Android bonded devices.
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net8.0-android34.0 is compatible. net9.0-android was computed. net10.0-android was computed. |
-
net8.0-android34.0
- InTheHand.Net.Bluetooth (>= 4.1.40)
- Microsoft.Extensions.DependencyInjection.Abstractions (>= 8.0.0)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.