我正在使用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。我绝对可以连接到安卓和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)
我不知道为什么。
发布于 2016-08-19 13:53:10
杰拉德·威尔金森的答案是正确的。为了让生活变得更简单,我使用反应性扩展()将其转化为一种可使用的方法。欢迎任何意见。
因此,一旦您找到使用BluetoothLEAdvertisementWatcher的设备并与其配对,您就可以使用它来启用GATTServices。
private async Task<GattDeviceService> GetGATTServiceAsync(string deviceName)
{
//devicewatcher is abused to trigger connection
var deviceWatcher = DeviceInformation.CreateWatcher(); //trick to enable GATT
var addedSource = Observable.FromEventPattern(deviceWatcher, nameof(deviceWatcher.Added))
.Select(pattern => ((DeviceInformation)pattern.EventArgs));
var updatedSource = Observable.FromEventPattern(deviceWatcher, nameof(deviceWatcher.Updated))
.Select(pattern =>
{
var update = ((DeviceInformationUpdate)pattern.EventArgs);
return Observable.FromAsync(() => DeviceInformation.CreateFromIdAsync(update.Id).AsTask());
}).Concat();
var source = addedSource.Merge(updatedSource);
source.Publish().Connect(); //make sure the event handlers are attached before starting the device watcher
deviceWatcher.Start();
var result = await source.Where(di => di.Name == deviceName) //find the relevant device
.Select(di => Observable.FromAsync(() => GattDeviceService.FromIdAsync(di.Id).AsTask())) //get all services from the device
.Concat() //necessary because of the async method in the previous statement
.Where(service => service.Uuid == SERVICE_UUID) //get the service with the right UUID
.Retry() //GattDeviceService.FromIdAsync can throw exceptions
.FirstAsync();
deviceWatcher.Stop();
return result;
}
https://stackoverflow.com/questions/35420940
复制相似问题