简体   繁体   English

低功耗蓝牙连接

[英]Bluetooth Low Energy connection

I'm trying to develop an app for Win 8.1 PC to communicate with my BLE device. 我正在尝试为Win 8.1 PC开发一个与我的BLE设备通信的应用程序。 I read that in Win 8 there is no something like scanning functionality for BLE so I read some examples from MS and try to simple connect to my device with UUID: "A9DA45B4-34F8-C660-591D-F6918B5B988C" 我读到Win 8中没有类似BLE扫描功能的内容,因此我从MS阅读了一些示例,并尝试使用UUID简单连接到我的设备:“ A9DA45B4-34F8-C660-591D-F6918B5B988C”

So I write some code based on ms examples: 因此,我根据ms示例编写了一些代码:

 var devices = await DeviceInformation.FindAllAsync(
            GattDeviceService.GetDeviceSelectorFromUuid(new Guid("A9DA45B4-34F8-C660-591D-F6918B5B988C")), new string[] { "System.Devices.ContainerId" });

 if (devices.Count > 0){
    //Do something
 }

But this code always return me empty devices list. 但是这段代码总是向我返回空设备列表。 More over I am still able to connect my device by Android or iOS app. 此外,我仍然可以通过Android或iOS应用连接我的设备。

Can someone told me if it is good way to connect BLE? 有人可以告诉我连接BLE的好方法吗?

Go to Settings --> Change PC Settings --> --> PC and devices --> Bluetooth. 转到设置->更改PC设置->-> PC和设备->蓝牙。

Make sure that your device is discoverable, then establish a connection. 确保可以发现您的设备,然后建立连接。

Try again when you're done, if it still doesn't work, try with following code: 完成后再试一次,如果仍然不起作用,请尝试以下代码:

var devices = await DeviceInformation.FindAllAsync(
        GattDeviceService.GetDeviceSelectorFromUuid(GattServiceUuids.GenericAccess));

devices.Count should return at least 1. devices.Count应该至少返回1。

In my case there were 3 things wrong. 就我而言,有3处错误。

If you plan to use BLE on Windows 8.1, first make sure that you have 如果您打算在Windows 8.1上使用BLE,请首先确保您已经

Bluetooth LE Enumerator 

In your Device Manager, if not go to PC/laptop manufacturer and reinstall your driver until you find BLE Enumerator in your device manager! 在设备管理器中,如果不能访问PC /笔记本电脑制造商,请重新安装驱动程序,直到在设备管理器中找到BLE枚举器为止!

Second, as Peter Duniho said, pair your device manually before you do anything else. 第二,正如Peter Duniho所说,在执行其他任何操作之前,先手动配对设备。 Windows doesn't have something like scan functionality so before you try to connect, pair it first. Windows没有扫描功能,因此在尝试连接之前,请先将其配对。

And last, quoting Alexandru, Windows BLE is like "Wild West". 最后,引用亚历山大(Alexandru),Windows BLE就像“狂野的西部”。 Several times when I tried to pair my device with Windows, I got the message "Windows can't pair your device, make sure that it is still discoverable". 几次尝试将设备与Windows配对时,我收到消息“ Windows无法与您的设备配对,请确保仍可发现该设备”。 To omit this step I increase each manufacturer timouts on my device (I think that when Windows tried to pair my device after rang was thinking that nothing happened because of too long delays and it goes to sleep mode or reject connection) so you must be patient. 为了省略此步骤,我增加了设备上每个制造商的时间(我想当Windows在响后尝试配对我的设备时,由于延迟太长而没有任何反应,因此进入睡眠模式或拒绝连接),因此您必须耐心等待。 First, set each timeout to MAX value and turn off sleep modes and energy saving modes. 首先,将每个超时设置为MAX值,然后关闭睡眠模式和节能模式。

Also I can confirm that the following code works fine: 我也可以确认以下代码可以正常工作:

var devices = await DeviceInformation.FindAllAsync(
        GattDeviceService.GetDeviceSelectorFromUuid(new Guid("A9DA45B4-34F8-C660-591D-F6918B5B988C")), new string[] { "System.Devices.ContainerId" });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM