Windows Phone 8.1 - Bluetooth Low Energy and Authentication

1

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...

c#
bluetooth
windows-phone-8.1
bluetooth-lowenergy
asked on Stack Overflow Nov 19, 2014 by Ravenheart • edited Jan 13, 2015 by Ravenheart

1 Answer

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;
    }
}
answered on Stack Overflow Jan 12, 2015 by Ruben Middel

User contributions licensed under CC BY-SA 3.0