Closed Ardelean-Calin closed 1 year ago
I've had one more finding: Even if running the example ble_bas_peripheral
unmodified, and just deleting one byte from the advertisment data gives the same invalid length error. So I cannot have any advertisment length other than 18 bytes? Why? Every example uses the same advertisment payload.
Nevermind, I figured it out! I will post a comment with the solution to the problem and close the issue.
So, it had nothing to do with Extended Advertising. That actually works and is as simple as using ConnectableAdvertisement::ExtendedNonscannableUndirected
.
The problem was with my advertisement payload. Apparently it needs to be formatted in a very specific way. It needs to be formatted into Advertising Data (AD) elements.
So the way we need to change the payload above is as follows:
let adv_data = &mut[
0x02, 0x01, raw::BLE_GAP_ADV_FLAGS_LE_ONLY_GENERAL_DISC_MODE as u8, // Flags
0x19, 0x16, 0xD2, 0xFC, 0x40, // The BTHome AD element. Has a length of 25 bytes.
// My actual data. placeholder for now. To be filled later
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF,
0x0A, 0x09, b'D', b'u', b's', b't', b'y', b'B', b'u', b't', b't',
];
Here we have 3 AD elements, indicated by each line (the indented lines are just payload data of an AD element). The first byte in each line is the length of the AD element. The second byte is the AD element identifier as given by this document. While the rest of the bytes are payload.
For example, the last line in the code above is a 10-byte long AD element with identifier 0x09 - Complete Local Name. If I delete a byte from the end of the name, I would have to change the length 0x0A to 0x09, otherwise I would get InvalidLength errors.
So this is why I got the errors, I failed to correctly structure the advertisement payload into AD elements.
Used hardware: nRF52832 with 512kB flash and 64kB RAM. Used softdevice: S132 v7.3.0 Used example code: Slightly modified version of
ble_bas_peripheral.rs
, I will attach the code at the end of this issue.As far as I could see in the code, it should be supported to configure and enable extended advertising extensions to have advertise data longer than 31 bytes. However, whenever I try it, it doesn't seem to work and I can't figure out why. I looked at the official advertising sequence chart on Nordic's website and the program flow seems to be all right... Why do I always get _NRF_ERROR_INVALIDLENGTH errors, then? What am I missing?