1
votes

I'm having an authentication issue with Bluetooth Low Energy devices with 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:

"The attribute requires authentication before it can be read or written. (Exception from 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. Then, whenever I disconnect from the device and reconnect, it returns an error when I call the WriteValueAsync function. 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. Even when I've used it before.

Seems like there's some problem with the reconnection of Windows Phone 8.1 to the device...

1
Was the issue solved? I'm in the same situation and i really cannot understand the reason of this behaviour. Seems like first time is paired it's connected and authenticated, other times...no.Gnegno
Hello Gnegno, Unfortunately, not. Never solved. However, seems like Microsoft Support knows this problem and it's being worked on... "I am working on a number of issues dealing with reconnection to BT devices which match this problem. I don't have a workaround at this time, but my BT dev team is aware of this and we are digging into it." Source: social.msdn.microsoft.com/Forums/windowsapps/en-US/…Ravenheart
I have received this error on a Windows Phone 8.1, but the same code working perfect on Windows 10 Mobile. So it was fixed in Win 10, but unfortunately a 90% of WP market is not reachable for our application :(Viacheslav Smityukh

1 Answers

1
votes

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;
    }
}