BluetoothLEAdvertisementDataSection (ArgumentException)

377 Views Asked by At

I'm trying to advertise an Eddystone beacon but my code fails at advertisementData.Data with an ArgumentException:

Value does not fall within the expected range.

Any ideas of what's happening?

// ...
using (var memoryStream = new MemoryStream())
{
    byte messageLengthByte = Convert.ToByte(message.Length);
    memoryStream.WriteByte(messageLengthByte);

    memoryStream.Write(message, 0, message.Length);

    fullMessage = memoryStream.ToArray();
}

while (fullMessage.Length < 32)
{
    byte[] newArray = new byte[fullMessage.Length + 1];
    fullMessage.CopyTo(newArray, 0);
    newArray[fullMessage.Length] = 0x00;
    fullMessage = newArray;
}

var writer = new DataWriter();
writer.WriteBytes(fullMessage);

var advertisementData = new BluetoothLEAdvertisementDataSection();
advertisementData.Data = writer.DetachBuffer(); // Error!
publisher.Advertisement.DataSections.Add(advertisementData);
publisher.Start();
1

There are 1 best solutions below

0
On BEST ANSWER

Most likely you're trying to fit in more bytes than the BLE packet allows. The max size is 32 bytes, but that's including:

  • 3 bytes for the "flags" data section, which I believe is mandatory and might be automatically set by the Windows 10 BLE API
  • for each additional section, 1 byte for the length of the section, and 1 bytes for the type of the section

If you only broadcast a single section, that leaves you with 27 bytes for that section's actual payload.