简体   繁体   English

Windows Phone 8.1-低功耗蓝牙和身份验证

[英]Windows Phone 8.1 - Bluetooth Low Energy and Authentication

I'm having an authentication issue with Bluetooth Low Energy devices with C#. 我在使用C#的蓝牙低功耗设备时遇到身份验证问题。

I can connect to the BLE, read from its services, but when I try to write with this function, I get the following error: 我可以连接到BLE,从其服务中读取内容,但是当我尝试使用此功能进行编写时,出现以下错误:

"The attribute requires authentication before it can be read or written. (Exception from HRESULT: 0x80650005)" “该属性需要身份验证才能读取或写入。(HRESULT的异常:0x80650005)

I've paired the device and, as I said, I can read from it. 我已经将设备配对,并且正如我所说,我可以从中读取信息。 Only problem is when I need to write. 唯一的问题是什么时候我需要写。 If I don't pair the device, when I write it automatically pairs, then it gives the error. 如果我不配对设备,则当我自动将其配对时,它会提示错误。

    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;

    }

Anyone has had the same issue? 有人遇到过同样的问题吗? How can I solve it? 我该如何解决? Thank you! 谢谢!

UPDATE - This code works PERFECTLY when I remove the device from my phone, reset my Bluetooth device and do a new pairing of the phone with the BLE device. 更新-当我从手机中删除设备,重置蓝牙设备并将手机与BLE设备进行新配对时,此代码将完全正常工作。 Then, whenever I disconnect from the device and reconnect, it returns an error when I call the WriteValueAsync function. 然后,每当我断开与设备的连接并重新连接时,当我调用WriteValueAsync函数时,它将返回错误。 Even if I connect and disconnect without having used the device... 即使不使用设备就连接和断开连接...

However, an Android app that uses the same device, has no problem to use the device. 但是,使用相同设备的Android应用程序可以正常使用该设备。 Even when I've used it before. 即使以前使用过。

Seems like there's some problem with the reconnection of Windows Phone 8.1 to the device... Windows Phone 8.1重新连接到设备似乎存在一些问题...

I get an error when I have another app that is connected to the device. 当我有另一个连接到设备的应用程序时出现错误。 Try it like this: 像这样尝试:

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.

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