Cannot write large byte array to a BLE device using UWP APIs - e.g. Write value as Async

0

I am using Windows 10 build 14393.2156. Bluetooth adapter LMP version is 6.X (Bluetooth version 4.0). I cannot write byte array data with length 350. However, I can write byte array data with length around 60 and get the expected data from the BLE device. When i write byte array of large length e.g. 350, I get windows exception: "Exception: The specified server cannot perform the requested operation. (Exception from HRESULT: 0x8007003A)". Following is the code:

private async Task CoreWrite(byte[] data)
    {
        var writeBuffer = CryptographicBuffer.CreateFromByteArray(data);
        var result = await _txCharacteristic.WriteValueAsync(writeBuffer);
        if (result != GattCommunicationStatus.Success)
        {
            throw new IOException($"Failed to write to bluetooth device. Status: {nameof(result)}");
        }
    }

Please note that the device is already paired. Is there any payload limit which can possibly affect limiting of payload length in Bluetooth 4.0 versus 4.2 specification. Or you suggest a higher Windows 10 builds with more recent Bluetooth LMP 8.X should help fix the issue. Appreciate any advice or help.

Many thanks.

c#
arrays
uwp
bluetooth-lowenergy
asked on Stack Overflow Nov 15, 2018 by smondal • edited Nov 15, 2018 by Jazb

1 Answer

0

Surprisingly, we found that Attribute Data Length of the characteristic was limited at 244 bytes. Hence, I was not able to write any data more than 244 bytes. However, performing multiple writes with 244 bytes at a time does resolve this issue. I could see the expected response from the BLE device.

Example:

int offset = 0;
int AttributeDataLen = 244;
while (offset < data.Length)
{
   int length = data.Length - offset;
   if (length > AttributeDataLen)
   {
      length = AttributeDataLen;
   }

   byte[] subset = new byte[length];

   Array.Copy(data, offset, subset, 0, length);
   offset += length;

   var writeBuffer = CryptographicBuffer.CreateFromByteArray(subset);
   var result = await _txCharacteristic.WriteValueAsync(writeBuffer);
   if (result != GattCommunicationStatus.Success)
   {
      throw new IOException(
        $"Failed to write to bluetooth device. Status: {nameof(result)}");
   }
}
answered on Stack Overflow Dec 2, 2018 by smondal • edited Dec 5, 2018 by CoCaIceDew

User contributions licensed under CC BY-SA 3.0