CSoft.Communication.BLE
11.0.3
dotnet add package CSoft.Communication.BLE --version 11.0.3
NuGet\Install-Package CSoft.Communication.BLE -Version 11.0.3
<PackageReference Include="CSoft.Communication.BLE" Version="11.0.3" />
<PackageVersion Include="CSoft.Communication.BLE" Version="11.0.3" />
<PackageReference Include="CSoft.Communication.BLE" />
paket add CSoft.Communication.BLE --version 11.0.3
#r "nuget: CSoft.Communication.BLE, 11.0.3"
#:package CSoft.Communication.BLE@11.0.3
#addin nuget:?package=CSoft.Communication.BLE&version=11.0.3
#tool nuget:?package=CSoft.Communication.BLE&version=11.0.3
CSoft.Communication.BLE
低功耗蓝牙 GATT 通讯包。BluetoothLE 扫描设备,连接指定服务,订阅 Notify 特征并向 Write 特征发送字节。BluetoothLE_Server 提供 GATT 外设入口,按 Guid 管理客户端及接收管道。经典蓝牙 SPP 设备请使用 CSoft.Communication.Bluetooth。 经典蓝牙包
安装
dotnet add package CSoft.Communication.BLE --version 11.0.3
平台与设备条件
| 平台 | 包目标和当前能力 |
|---|---|
| Windows | net8.0-windows10.0.19041.0 至 net11.0-windows10.0.19041.0。提供 GATT 客户端和服务端。服务端需要支持 BLE 外设角色的适配器。 |
| Android | net10.0-android、net11.0-android。提供客户端与服务端实现。服务端需要支持 BLE 广播的设备。当前服务端没有显式配置 CCCD,依赖通知订阅的互通须在目标设备验证。 |
| iOS、Mac Catalyst | .NET 10 和 11 平台资产。客户端使用 Core Bluetooth 提供程序。服务端可添加服务并发起广播,写入和订阅回调尚未实现。 |
| Linux | net8.0 至 net11.0 通用目标由上游提供 BlueZ 客户端。系统需启用 BlueZ 和 D-Bus。Linux GATT 服务端尚未实现,启动会抛出 NotImplementedException。 |
netstandard2.0 |
提供兼容资产,客户端依赖宿主选择的蓝牙提供程序。此资产中的 GATT 服务端启动会抛出 PlatformNotSupportedException。 |
Windows 请在应用项目中选择 -windows10.0.19041.0 或兼容的平台 TFM,以便 NuGet 选中 Windows 实现。可以用系统的 BluetoothAdapter.IsPeripheralRoleSupported 检查适配器能否作为外设。 Windows 外设角色检查
Android 应用按扫描、连接或广播用途声明并申请蓝牙权限。Apple 应用在 Info.plist 中填写 NSBluetoothAlwaysUsageDescription。 Android 蓝牙权限 · Apple Core Bluetooth 权限
客户端
从 BluetoothLE.ScanAsync() 获取 Name 和 Id,将 Id 作为程序的第一个参数。三个 UUID 必须与对端服务、通知特征和写入特征一致。以下值与仓库的 Windows BLE 示例一致。
using Communication.BLE;
using Parser.Parsers;
using System.Text;
using TopPortLib;
var service = Guid.Parse("0000ffe0-0000-1000-8000-00805f9b34fb");
var write = Guid.Parse("0000ffe1-0000-1000-8000-00805f9b34fb");
var notify = Guid.Parse("0000ffe2-0000-1000-8000-00805f9b34fb");
var peripheral = new BluetoothLE(
deviceAddressOrName: args[0],
serviceUuid: service,
notifyCharacteristicUuid: notify,
writeCharacteristicUuid: write);
await using var client = new TopPort(peripheral, new TimeParser());
client.OnReceiveParsedData += data =>
{
Console.WriteLine(Encoding.UTF8.GetString(data));
return Task.CompletedTask;
};
await client.OpenAsync();
await client.SendAsync(Encoding.UTF8.GetBytes("ping"));
Console.ReadLine();
Windows GATT 服务端
客户端订阅 Notify 特征后再写入数据。服务端在首次收到写入时建立客户端上下文,示例把收到的字节通过 Notify 回送给该客户端。
using Communication.BLE;
using Communication.Extensions;
var service = Guid.Parse("0000ffe0-0000-1000-8000-00805f9b34fb");
var write = Guid.Parse("0000ffe1-0000-1000-8000-00805f9b34fb");
var notify = Guid.Parse("0000ffe2-0000-1000-8000-00805f9b34fb");
await using var server = new BluetoothLE_Server(
serviceUuid: service,
writeCharacteristicUuid: write,
notifyCharacteristicUuid: notify);
server.OnClientConnect += (clientId, reader) =>
{
reader.StartConsuming(data => server.SendDataAsync(clientId, data));
return Task.CompletedTask;
};
await server.StartAsync();
Console.ReadLine();
客户端 CloseAsync() 会停止通知并断开 GATT 连接。服务端 StopAsync() 会停止广播并清理客户端上下文。退出时使用异步释放。每次管道读取到的字节块可能跨越多次写入,应用应按协议确定帧边界,并按设备支持的 GATT 写入长度分段。
接入 Pigeon
安装 CSoft.Communication.BLE 11.0.3 会自动还原 CSoft.TopPortLib、CSoft.CommTask、CSoft.Communication 和 CSoft.Parser 11.0.3。使用本地包时,先按仓库说明配置本地包源。
客户端将 BluetoothLE 交给 TopPort,再用 PigeonPort 包装。支持 GATT 外设的目标平台可用 BluetoothLE_Server、TopPort_Server 和 PigeonPort_Server 组合。GATT 服务、特征和平台要求见上文。
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. 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 is compatible. 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. net8.0-windows10.0.19041 is compatible. net9.0 is compatible. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net9.0-windows10.0.19041 is compatible. net10.0 is compatible. net10.0-android was computed. net10.0-android36.0 is compatible. net10.0-browser was computed. net10.0-ios was computed. net10.0-ios26.0 is compatible. net10.0-maccatalyst was computed. net10.0-maccatalyst26.0 is compatible. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. net10.0-windows10.0.19041 is compatible. net11.0 is compatible. net11.0-android37.0 is compatible. net11.0-ios27.2 is compatible. net11.0-maccatalyst27.2 is compatible. net11.0-windows10.0.19041 is compatible. |
| .NET Core | netcoreapp2.0 was computed. netcoreapp2.1 was computed. netcoreapp2.2 was computed. netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.0 is compatible. netstandard2.1 was computed. |
| .NET Framework | net461 was computed. net462 was computed. net463 was computed. net47 was computed. net471 was computed. net472 was computed. net48 was computed. net481 was computed. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen40 was computed. tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
-
.NETStandard 2.0
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.45)
- System.Buffers (>= 4.6.1)
-
net10.0
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.45)
- Tmds.DBus (>= 0.95.2)
-
net10.0-android36.0
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.45)
-
net10.0-ios26.0
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.45)
-
net10.0-maccatalyst26.0
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.45)
-
net10.0-windows10.0.19041
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.45)
-
net11.0
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.45)
- Tmds.DBus (>= 0.95.2)
-
net11.0-android37.0
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.45)
-
net11.0-ios27.2
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.45)
-
net11.0-maccatalyst27.2
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.45)
-
net11.0-windows10.0.19041
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.45)
-
net8.0
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.43)
- Tmds.DBus (>= 0.95.2)
-
net8.0-windows10.0.19041
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.43)
-
net9.0
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.45)
- Tmds.DBus (>= 0.95.2)
-
net9.0-windows10.0.19041
- CSoft.TopPortLib (>= 11.0.3)
- InTheHand.BluetoothLE (>= 4.0.45)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.
依赖 CSoft.TopPortLib 11.0.3,安装时自动还原新版 Pigeon 和核心通信组件。
继续提供 .NET 8 至 11 的通用及 Windows GATT 资产、.NET 10 和 11 的 Android、iOS 和 Mac Catalyst 资产,以及 .NET Standard 2.0 兼容资产。