4

I'm having an issue with the BluetoothLEAdvertisementDataSection.BluetoothLEAdvertisementDataSection class from the Windows 10 Bluetooth Low-energy (BLE) API.

If I check the length of the IBuffer member Data using this code:

 var myDataSection = new BluetoothLEAdvertisementDataSection();
 Debug.WriteLine($"Data Capacity: {myDataSection.Data.Capacity}"); //Looks like SO needs to update for C# 6.0

I get the expected output:

Data Capacity: 29

For more info on BLE packets, I recommend visiting this great blog post.

Now let' say I've declared a byte[] called myPayload which is 29 bytes long. The following code throws an exception:

 DataWriter writer = new DataWriter();
 writer.WriteBytes(myPayload);
 myDataSection.Data = writer.DetachBuffer(); //throws ArgumentException

The ArgumentException very helpfully suggests that

"Value does not fall within the expected range."

In fact, any size for myPayload of 20+ bytes results in the same error. If I make it 19 bytes long, however, I get no errors.

Yes, I was very upset when this answer did not help.

Community
  • 1
  • 1
ZX9
  • 898
  • 2
  • 16
  • 34

1 Answers1

2

This is really sad...but I actually just miscounted the number of bytes I was adding. 29 bytes does work. 30 does not. This is not an API issue.

 static readonly byte[] myPayload =  
{0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF, //8 bytes
 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF, //8 bytes
 0x01,0x23,0x45,0x67,0x89,0xAB,0xCD,0xEF, //8 bytes
 0x00,0x00,0x00,0x00,0x00}; //5 bytes Total:29 bytes            

...will work just fine.

ZX9
  • 898
  • 2
  • 16
  • 34
  • 1
    In the future in a similar situation you may benefit from specifying the length of the array in the declaration `static readonly byte[29] myPayload = ...` – Daniel Oct 27 '16 at 18:32
  • @Daniel I like the idea, but your syntax needs a slight modification: `static readonly byte[] myPayload = new byte[29]{/* 29 bytes of data here */}` – ZX9 Nov 02 '16 at 14:37
  • Not surprising that I got the syntax off I didn't test it, but you see the idea. For more options you can view [All possible C# array initialization syntaxes](http://stackoverflow.com/questions/5678216/all-possible-c-sharp-array-initialization-syntaxes) – Daniel Nov 02 '16 at 14:48