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

时间:2014-11-19 12:31:53

标签: c# bluetooth windows-phone-8.1 bluetooth-lowenergy

我的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重新连接到设备有一些问题...

1 个答案:

答案 0 :(得分: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;
    }
}