【发布时间】:2016-05-27 01:06:42
【问题描述】:
我正在使用BluetoothLEAdvertisementWatcher 查找附近的 BLE 设备,它运行良好。找到它们后,我想通过 GATT 连接和读/写数据。但是拿到BluetoothLEAdvertisement(https://msdn.microsoft.com/de-de/library/windows/apps/windows.devices.bluetooth.genericattributeprofile)后,我不知道如何使用API。
public class Adapter
{
private readonly BluetoothLEAdvertisementWatcher _bleWatcher = new BluetoothLEAdvertisementWatcher();
public Adapter()
{
_bleWatcher.Received += BleWatcherOnReceived;
}
private void BleWatcherOnReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
// how to connect?
// I know, it's the wrong place to to this, but this is just an example
}
public void StartScanningForDevices(Guid[] serviceUuids)
{
_blewatcher.advertisementfilter.advertisement.serviceuuids.clear();
foreach (var uuid in serviceuuids)
{
_blewatcher.advertisementfilter.advertisement.serviceuuids.add(uuid);
}
_blewatcher.start();
}
}
我找到了使用 DeviceInformation.FindAllAsync 而不是 BluetoothLEAdvertisementWatcher 的示例,但这些示例无法正常工作/找不到任何设备。
更新
经过一段时间的挖掘,我找到了以下方法。但不幸的是,配对失败。该设备只是一个带有 BLE 屏蔽的 Arduino。我绝对可以连接 Android 和 iOS。所以它必须以某种方式使用 UWP 成为可能。 :/
private void BleWatcherOnReceived(BluetoothLEAdvertisementWatcher sender, BluetoothLEAdvertisementReceivedEventArgs args)
{
var dev = await BluetoothLEDevice.FromBluetoothAddressAsync(args.BluetoothAddress);
// dev.DeviceInformation.Pairing.CanPair is true
// dpr.Status is Failed
DevicePairingResult dpr = await dev.DeviceInformation.Pairing.PairAsync(DevicePairingProtectionLevel.None);
var service = await GattDeviceService.FromIdAsync(dev.DeviceInformation.Id);
}
更新 #2
我现在能够发现和配对(不稳定,但目前还可以),但是
var service = await GattDeviceService.FromIdAsync(args.Id);
抛出以下异常
System.IO.FileNotFoundException: 该系统找不到指定的文件。 (HRESULT 异常:0x80070002)
我不知道为什么。
【问题讨论】:
-
也一直在努力解决这个问题。我在 Raspberry Pi 2 上运行 Windows 10 IOT。我的第一个问题是我在 appxmanifest 中缺少 DeviceCapability 标签。那不会让我得到一个BluetoothLEDevice。在我修复了我被困在同一个地方之后(上面的更新#2)。我尝试使用代码配对设备,但总是失败。几乎要放弃自己了。
-
答案很简单:你不能!!!!不知何故,谁实现了 API 认为首先手动配对设备然后在您的代码中使用它会更好......互联网上有多个关于这个问题的示例和帖子......不幸的是,我刚刚写完在 Raspberry PI 3 的 raspbian OS 上使用 node.js 的示例应用程序,我只是想掌握我的主要开发技能:C# 和 Windows,但似乎我必须回到 Linux :( 配对和 GATT 发现是在在 Raspbian 上使用 node.js 的 3 行代码...... UWP 10 SDK 的一大耻辱。对不起!
-
我在 Windows 10 Creators Update 上遇到了 GattDeviceService.FromIdAsync 的问题 - 当与经典桌面应用程序一起使用时,它永远不会返回。所以也要小心那个
标签: c# bluetooth-lowenergy win-universal-app windows-10-universal gatt