0

I'm attempting to get an estimated range to a BLE device, given the devices RSSI and calibrated transmission power.

The calibrated txPower is supposedly emitted as the last byte in the peripherals iBeacon advertisement packet. These packets, according to documentation i've found, should be 30 bytes in length.

However, the returned byte[] for the packet is 62 bytes in length. Because of this, the format of the advertisement data is unknown.

Why might this be happening, and is there any way to decipher the format of the 62 byte packet?

Orbit
  • 2,985
  • 9
  • 49
  • 106

1 Answers1

0

When scanning for BLE devices, Android APIs return not just the bytes for the raw advertisement PDUs but also the scan response PDUs. The latter are tacked on to the end of the former in the byte array returned by the scanning APIs.

For this reason, you cannot reliably use a negative offset from the end if the byte array to access beacon fields. Using a positive offset from the beginning is more reliable, but even this can fail if unusual PDUs are inserted before the manufacturer advertisement PDU, which is the one you care about.

For 100% reliability you must parse out all the PDUs, find the manufacturer advertisement one, and look at the bytes in that. I learned this the hard way when writing the Android Beacon Library. It is open source, so even if you want to roll your own scanning, it is a good idea to see how it does it.

davidgyoung
  • 63,876
  • 14
  • 121
  • 204
  • That's odd. Is there a simpler way to get the calibrated txPower of the peripheral? The calibrated constant is really all I need from the advertisement. – Orbit Nov 29 '16 at 21:40
  • You must get the constant from the bytes of the advertisement. A simpler solution than parsing the PDUs is to search for the byte pattern of the static iBeacon prefix, and then find the calibration constant based on an offset from there. The disadvantage of this approach is that byte sequence may (by chance) also appear elsewhere in the advertisement, causing the technique to fail. – davidgyoung Nov 29 '16 at 22:42