C#怎么实现蓝牙设备搜索_C#如何开发Bluetooth应用【指南】

BluetoothClient仅支持已配对的传统蓝牙设备发现,无法扫描未配对或BLE设备;搜不到设备需检查系统可见性、驱动状态及组策略限制。Windows 上用 BluetoothClient 搜不到设备?先确认平台限制在 .net framework 或 .net 5+ 的 windows 平台下,bluetoothclient 默认只支持 rfcomm 协议的配对设备发现,且严重依赖系统蓝牙协议栈(bth)是否启用"可被发现"模式。它根本不会扫描未配对、不可见或使用 ble(bluetooth low energy)广播的设备。常见错误现象:BluetoothClient.DiscoverDevices() 返回空数组,或抛出 SocketException 错误码 10013(权限拒绝)真实使用场景:仅适用于已手动配对、且对方设备处于"可见"状态的传统蓝牙(BR/EDR)设备,比如老式蓝牙打印机、串口模块关键参数差异:DiscoverDevices(int) 的参数是最大返回数量,不是超时毫秒数;超时由底层驱动控制,无法通过 API 调整兼容性影响:.NET Core / .NET 5+ 在 Windows 上仍可用该类,但 Linux/macOS 完全不支持 ------ PlatformNotSupportedException 会直接抛出想扫 BLE 设备?必须换 Windows.Devices.Bluetooth.AdvertisementWindows 10 1809+ 系统原生支持 BLE 广播监听,但需要 UWP 运行时能力(即使你写的是 WinForms/WPF,也得走 WindowsRuntimeComponent 或启用 C++/CX 互操作),而且必须声明 bluetooth 功能权限。常见错误现象:调用 BluetoothLEAdvertisementWatcher.Start() 后无回调,或触发 Stopped 事件并带 BluetoothLEAdvertisementWatcherStatus.DisabledByUser必须做的三件事:-- 在 Package.appxmanifest(或项目属性 > 功能)中勾选 bluetooth-- 运行时检查 BluetoothAdapter.GetDefaultAsync() 是否返回非 null-- 用户需在系统设置中打开"蓝牙"和"允许设备通过蓝牙查找此设备"(后者影响接收广播)性能注意点:ScanningMode 设为 Active 才能收到扫描响应(Scan Response),但耗电更高;Passive 模式只收广播包(Advertising Data),适合低功耗监听WinForms/WPF 项目里怎么安全接入 UWP 蓝牙 API?不能直接引用 Windows.Devices.Bluetooth 命名空间到传统桌面项目,必须通过 .NET 5+ 的"Windows Runtime Support"桥接,或者用 Microsoft.Windows.SDK.Contracts NuGet 包(仅限 Windows 10 1809+)。 文心快码 文心快码(Comate)是百度推出的一款AI辅助编程工具

相关推荐
Warson_L5 小时前
Python `Annotated` 与 LangGraph Reducer 学习笔记
python
韩师傅5 小时前
海天线算法的前世今生
python·计算机视觉
韩师傅5 小时前
当你的甲方设备过烂,要如何快速出效果?
python·计算机视觉
Warson_L5 小时前
LangGraph的MessageState and HumanMessage
python
韩师傅6 小时前
当你的甲方吐槽天空不够蓝,你应该如何应对
python·计算机视觉
Warson_L6 小时前
python的类&继承
python
Warson_L6 小时前
类型标注/type annotation
python
ThreeS9 小时前
手搓MiniVLA全实战教程-一步一步用pytorch解释原理与思路
人工智能·python
金銀銅鐵10 小时前
[Python] 模 n 乘法的逆元计算器
python·数学·游戏