[英]How to pair and search Bluetooth Low Energy services on phone in windows desktop application?
[英]Windows Phone 8.1 - Bluetooth Low Energy and Authentication
我在使用C#的蓝牙低功耗设备时遇到身份验证问题。
我可以连接到BLE,从其服务中读取内容,但是当我尝试使用此功能进行编写时,出现以下错误:
“该属性需要身份验证才能读取或写入。(HRESULT的异常:0x80650005)
我已经将设备配对,并且正如我所说,我可以从中读取信息。 唯一的问题是什么时候我需要写。 如果我不配对设备,则当我自动将其配对时,它会提示错误。
public async static Task<bool> WriteByte(string paramDeviceID, Guid paramService, byte paramValue)
{
string debug;
var Services = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid([UUID HERE]), null);
GattDeviceService Service = await GattDeviceService.FromIdAsync(paramDeviceID);
debug = "Using service: " + Services[0].Name; // Service name is correct
GattCharacteristic gattCharacteristic = Service.GetCharacteristics(paramService)[0];
var writer = new Windows.Storage.Streams.DataWriter();
writer.WriteByte(paramValue);
// Error happens here
GattCommunicationStatus status = await gattCharacteristic.WriteValueAsync(writer.DetachBuffer());
// This code is never executed, error occurs before
if (GattCommunicationStatus.Unreachable == status)
{
debug = "Write failed";
return false;
}
return true;
}
有人遇到过同样的问题吗? 我该如何解决? 谢谢!
更新-当我从手机中删除设备,重置蓝牙设备并将手机与BLE设备进行新配对时,此代码将完全正常工作。 然后,每当我断开与设备的连接并重新连接时,当我调用WriteValueAsync函数时,它将返回错误。 即使不使用设备就连接和断开连接...
但是,使用相同设备的Android应用程序可以正常使用该设备。 即使以前使用过。
Windows Phone 8.1重新连接到设备似乎存在一些问题...
当我有另一个连接到设备的应用程序时出现错误。 像这样尝试:
public async static Task<GattCommunicationStatus> WriteByte(string paramDeviceID, Guid paramService, byte paramValue)
{
string debug;
var Services = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromUuid([UUID HERE]), null);
GattDeviceService Service = await GattDeviceService.FromIdAsync(paramDeviceID);
debug = "Using service: " + Services[0].Name; // Service name is correct
GattCharacteristic gattCharacteristic = Service.GetCharacteristics(paramService)[0];
var writer = new Windows.Storage.Streams.DataWriter();
writer.WriteByte(paramValue);
try{
return GattCommunicationStatus status = await gattCharacteristic.WriteValueAsync(writer.DetachBuffer());
}
catch()
{
debug = "Write failed";
return GattCommunicationStatus.Unreachable;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.