3

I'm trying to write to the GAP service (0x180) characteristics and 0x2A04

Whenever I try writing to 0x2A04 (connection parameters) or 0x0200 (device name),

    var devices = await Windows.Devices.Enumeration.DeviceInformation.FindAllAsync(GattDeviceService.GetDeviceSelectorFromShortId(0x1800));
    var service = await GattDeviceService.FromIdAsync(devices[0].Id);

    var gapData = service.GetCharacteristics(new Guid("00002A04-0000-1000-8000-00805f9b34fb"))[0];
    var raw = await gapData.ReadValueAsync();

    byte[] conParas = new byte[raw.Value.Length];
    DataReader.FromBuffer(raw.Value).ReadBytes(conParas);
    //I can breakpoint and verify that the read works fine

    var status = await gapData.WriteValueAsync(conParas.AsBuffer());

and call the WriteValueAsync(), the program breaks on that line and my exception is

An exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll but was not handled in user code

Additional information: Access is denied. (Exception from HRESULT: 0x80070005 (E_ACCESSDENIED))

I'm not quite sure what level of the stack the parameters are being refused - I don't even know if the parameters are even reaching the BLE device. However, as I can write to other GATT services and read from the GAP characteristics, I believe it is the device.

Can anybody see a fool proof method to discover where this problem is coming from?

Thanks Thomas

friartuck
  • 2,954
  • 4
  • 33
  • 67

1 Answers1

2

I had this problem before. It seems Microsoft doesn't like you writing to the GAP.

I recommend you structure your Gatt.XML so that it only contains "Name" and "Appearance" services in the GAP (only these 2 services) and then put the remaining characteristics underneath a Custom Service or a standard one.

If you try writing to a characteristic that is write enabled beneath the custom service it should work.

Summary: You can't write to characteristics under GAP.

Reference: Restructuring the Gatt.XML of my device fixed this same problem for me.

Additional Details: For these "Access Denied" errors you want to check that: 1) You have your package manifest capabilities manually edited to include the Services your Gatt.XML has. You have to specify custom services and standard services alike. 2) Your Gatt.XML is correctly structured. 3) The caracteristic you are trying to write to exists/the device is currently paired to.

Cheers, Togepi

Togepi
  • 21
  • 1