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.

jazb
  • 5,498
  • 6
  • 37
  • 44
smondal
  • 1
  • 3
  • see how handling a large byte array can be handled here: https://stackoverflow.com/questions/583970/need-loop-to-copy-chunks-from-byte-array – jazb Nov 15 '18 at 05:50
  • Does the peripheral support writes of Long values? – Emil Nov 15 '18 at 08:17
  • Thanks Emil. Yes the peripheral does support writing of large values. We can write the same value from Android system using Bluetooth 5.0 specification. – smondal Nov 16 '18 at 01:23
  • Your os version is old, and the new version of windows has support a lot feature about bluetooth 5.0, please try this in lastest version. – CoCaIceDew Nov 22 '18 at 02:44

1 Answers1

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)}");
   }
}
CoCaIceDew
  • 228
  • 1
  • 7
smondal
  • 1
  • 3