【问题标题】:Connect to BLE peripheral on Windows 10连接到 Windows 10 上的 BLE 外围设备
【发布时间】:2026-02-16 05:45:01
【问题描述】:

我尝试连接到 BLE 外围设备。首先,我看广告:

watcher = new BluetoothLEAdvertisementWatcher { ScanningMode = BluetoothLEScanningMode.Active };
watcher.Received += WatcherOnReceived;
watcher.Start();

在 WatcherOnReceived 回调中我尝试创建 BluetoothLEDevice

public async void WatcherOnReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs btAdv)
{
    BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(btAdv.BluetoothAddress);
}

但是,我总是在 WatcherOnReceived 回调中得到 bleDevice == null。为什么以及如何解决它?在 UWP 应用程序中创建 BLE 设备的正确方法是什么?然后我需要连接到该设备,发现它的 GATT 服务和特性,在其中一些上启用通知并读/写其中一些。

【问题讨论】:

    标签: windows bluetooth


    【解决方案1】:

    这个问题的答案很简单——不要在 Windows 10 上使用 BLE。API 不起作用或行为随机,并且完全没有文档记录。我喜欢每个人都在谈论物联网是下一次工业革命,而微软在 BLE 存在 6 年后没有工作的 BLE API。

    【讨论】:

    • 哈哈我当然同意一切都是随机的并且有点无证。最烦人的是,他们没有一个紧凑而完整的 BLE API,而是尝试将其与其他 Windows 设备(在设备管理器中)集成,并将每个 GATT 服务公开为一个设备 + 需要使用 5 种不同的 API得到你想要的一切。
    • 情况还一样吗(两年后)?
    • 同样的问题。情况还一样吗(3 年后)?
    • 从 2017 年 3 月开始,创作者更新对 API 进行了显着改进。docs.microsoft.com/en-us/windows/uwp/devices-sensors/…
    【解决方案2】:

    如果您希望能够连接到以前未配对的 BLE 设备,例如使用带有蓝牙 LE 选择器的 DeviceWatcher,请参阅 https://github.com/Microsoft/Windows-universal-samples/tree/master/Samples/DeviceEnumerationAndPairing 中的示例 8 和 9。

    否则,您需要先在系统的蓝牙配对设置中进行配对,然后才能从 FromBluetoothAddressAsync 检索 BluetoothLEDevice。

    【讨论】:

    • 谢谢。我在该示例的场景 9 中配对了我的 BLE 设备。现在,IsPaired 标志在该设备上显示为 true。但我的应用程序中仍然得到 bleDevice == null。一旦我在 .FromBluetoothAddressAsync 调用上遇到异常 0x80070490。
    • 广告观察器上似乎没有过滤器 - 您确定您尝试连接的设备是您感兴趣的 BLE 设备吗?某些 BLE 设备无法连接,您只能从它们接收广告。
    • 所以蓝牙连接是由BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(btAdv.BluetoothAddress); ?是的,我确定这是我要连接的设备,因为它是我附近唯一的 BLE 设备。
    【解决方案3】:

    您可以查看WatcherOnReceived() 中的设备信息,以确保该设备是您要连接的设备。这样做:

    public async void WatcherOnReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs btAdv)
    {
        if (btAdv.Advertisement.LocalName == "SensorTag")
        {
            BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromBluetoothAddressAsync(btAdv.BluetoothAddress);
        }    
    }
    

    使用您自己的 BLE 设备名称而不是“SensorTag”。

    注意:您需要事先配对您的 BLE 设备(以编程方式,如 DeviceEnumerationAndPairing 示例或 PC 的设置应用程序,如下图所示。)。

    【讨论】:

    • 我可以看到,即使 PC 正在搜索设备,我的 BLE 设备也不会显示在“设置”->“设备”->“蓝牙”中。但是当我运行 Windows 平台示例、场景 8 或 9 时,我可以看到设备。设备已配对。我还可以在 WatcherOnReceived 回调中看到设备。但 FromBluetoothAddressAsync 总是为设备返回 null。
    • @MartinDusek 在PairButton_Click()Scenario8_PairDevice 中的UpdatePairingButtons(); 之前添加以下代码。 if(dpr.Status == DevicePairingResultStatus.Paired) { BluetoothLEDevice bleDevice = await BluetoothLEDevice.FromIdAsync(deviceInfoDisp.DeviceInformation.Id); } 然后运行 ​​Scenario8,配对选定的设备并检查连接状态。注意:与设备连接需要一些时间。如果您在配对后立即查看bleDevice ,则可能是null
    • 感谢您的建议。在 FromIdAsync() 调用之后,bleDevice 仍然评估为 null。但是我注意到,在我单击配对按钮后,场景 8(没有或什至有您的修改)可能会以某种方式连接到我的 BLE 设备,因为我的 BLE 设备随后停止了广告。当我单击取消配对按钮时,它会重新启动广告。这是否意味着与设备的连接/断开是通过配对/取消配对过程执行的?有没有关于这种行为的文档?在我看来,Windows 上的 BLE 是一个真正的魔法......
    • @MartinDusek 您可以通过发现其 GATT 服务和特征来检查连接是否有效。示例是here
    • 好的。我创建了温度计 BLE 设备。我配对了。然后我使用来自link 的代码 sn-p 来获取所有温度计服务,但我没有得到温度计服务。