C# Windows Phone 8.1-蓝牙低能耗和身份验证

C# Windows Phone 8.1-蓝牙低能耗和身份验证,c#,bluetooth,windows-phone-8.1,bluetooth-lowenergy,C#,Bluetooth,Windows Phone 8.1,Bluetooth Lowenergy,我在使用C#的蓝牙低能设备时遇到身份验证问题 我可以连接到BLE,从其服务读取数据,但当我尝试使用此函数写入数据时,出现以下错误: “该属性在读取或写入之前需要身份验证。(HRESULT的异常:0x80650005)” 我已经配对了设备,正如我所说,我可以从中读取数据。唯一的问题是我什么时候需要写作。 如果我没有配对设备,当我写它自动配对时,它会给出错误 public async static Task<bool> WriteByte(string paramDeviceID

我在使用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;

    }
公共异步静态任务WriteByte(字符串paramDeviceID、Guid paramService、字节paramValue)
{
字符串调试;
var Services=wait Windows.Devices.Enumeration.DeviceInformation.findalsync(GattDeviceService.GetDeviceSelectorFromUuid([UUID HERE]),null);
GattDeviceService服务=等待GattDeviceService.FromIdAsync(paramDeviceID);
debug=“使用服务:”+Services[0]。Name;//服务名称正确
GattCharacteristic GattCharacteristic=Service.getcharacteristic(paramService)[0];
var writer=new Windows.Storage.Streams.DataWriter();
writer.WriteByte(参数值);
//这里发生了错误
GattCommunicationStatus=等待gattCharacteristic.WriteValueAsync(writer.DetachBuffer());
//此代码从未执行过,之前发生过错误
if(GattCommunicationStatus.Unreachable==状态)
{
debug=“写入失败”;
返回false;
}
返回true;
}
有人有过同样的问题吗?我怎样才能解决它? 谢谢大家!

更新-当我从手机中卸下设备,重置蓝牙设备,并将手机与蓝牙设备重新配对时,此代码工作正常。 然后,每当我断开与设备的连接并重新连接时,调用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;
    }
}
公共异步静态任务WriteByte(字符串paramDeviceID、Guid paramService、字节paramValue)
{
字符串调试;
var Services=wait Windows.Devices.Enumeration.DeviceInformation.findalsync(GattDeviceService.GetDeviceSelectorFromUuid([UUID HERE]),null);
GattDeviceService服务=等待GattDeviceService.FromIdAsync(paramDeviceID);
debug=“使用服务:”+Services[0]。Name;//服务名称正确
GattCharacteristic GattCharacteristic=Service.getcharacteristic(paramService)[0];
var writer=new Windows.Storage.Streams.DataWriter();
writer.WriteByte(参数值);
试一试{
return GattCommunicationStatus=wait gattCharacteristic.WriteValueAsync(writer.DetachBuffer());
}
捕获()
{
debug=“写入失败”;
返回gatt通信状态。无法访问;
}
}

尝试了您的代码,但什么都没有。。。gattCharacteristic.WriteValueAsync上出现相同错误。我用新的信息更新了我的问题。看起来更像是蓝牙重新连接的问题。问题解决了吗?我也处于同样的情况,我真的无法理解这种行为的原因。好像是第一次配对,它是连接和验证的,其他时间…不,你好,格尼诺,不幸的是,没有。从未解决。然而,微软的技术支持人员似乎知道这个问题,并且正在着手解决。。。“我正在处理一些与此问题相匹配的BT设备的重新连接问题。目前我没有解决方法,但我的BT开发团队知道这一点,我们正在深入研究。”来源:我在Windows Phone 8.1上收到此错误,但同样的代码在Windows 10 Mobile上运行得很好。因此在Win 10中进行了修复,但不幸的是,我们的应用无法获得90%的WP市场:(