4

How can I advertise via BLE 100 bytes?
SDK >= 26
I am able to advertise 20 bytes, but when I advertise more than 20 bytes, I get an Exception.

I have already read these articles:
Android: Sending data >20 bytes by BLE
How to send more than 20 bytes data over ble in android?

As I right understand, the mentioned links are no solution for advertising but for P2P connections, am I right?

My code:

     private fun startAdvertising() {
        goForeground()

        Log.d(tag, "Service: Starting Advertising")

        if (mAdvertiseCallback == null) {
            val settings = buildAdvertiseSettings()
            mAdvertiseCallback = SampleAdvertiseCallback()

            if (mBluetoothLeAdvertiser != null) {
                mBluetoothLeAdvertiser!!.startAdvertising(settings, data, mAdvertiseCallback)
            }
        }
    } 

private fun buildAdvertiseData(): AdvertiseData {
        val advertisingData = AdvertiseData.Builder()
        val uuid = BeaconWiliot.manufactureUuid
        advertisingData.addServiceUuid(uuid)
        advertisingData.setIncludeDeviceName(false)
        advertisingData.addServiceData(uuid, ByteArray(20))
        return advertisingData.build()
    }
4
  • probably because the advertisement packet does not support more than 20 bytes of custom data. Feb 5, 2019 at 11:29
  • Do you need to advertise 100 bytes in one package, or possible advertise 100 bytes at once but in several packages?
    – Valentin
    Feb 5, 2019 at 11:41
  • @Valentin Advertising in several packages is possible, do you know the way? Feb 5, 2019 at 12:36
  • You are confusing Advertising (a GAP-level procedure) with GATT packets (a GATT-level procedure). Advertising/Scanning packets are always 31 bytes long (Bluetooth Specification 4.2, GAP, "ADVERTISING AND SCAN RESPONSE DATA"). GATT data packets can be larger, by changing their MTU. FORMAT").
    – bavaza
    Feb 6, 2019 at 9:15

1 Answer 1

1

You cannot.

The maximum length of an advertising packet is 31 byte. Additionally you can implement a scan response.
The MTU size of BLE packets is negotiated after connection establishment and has nothing to do with the advertisement size.

This scan response is like an extension of the advertisement data:
Your device broadcasts the advertisement. In case a scanning device is interested, it can request the scan response. The scan response can contain more or less the same data at the advertisement and can also have 31 byte.
In general, putting data to the scan response is a bit slower since a new request must be made. From user point of view, this nearly makes no difference.

In total this makes 62 bytes. Usually this 62 bytes contain data such as the name and some flags like "BLE_FLAGS_GENERAL_DISCOVERABLE_MODE" (name dependent on the implementation) plus some overhead to mark the type and length of the data.

(BTW: I don't know why 31 and usual packets 20 byte payload...)

Of cause, you could change the advertising data on runtime and transmit new data in every advertisement. The scanning device will miss most of the advertisements, thus you could implement regularly retransmit etc. etc... But this is not what advertisement is meant for.

As I right understand, the mentioned links are no solution for advertising but for P2P connections, am I right?

Yes. In a P2P connection, you can fragment data to multiple packets or better increase the MTU size to send larger data blocks.

2
  • "...you could change the advertising data on runtime and transmit new data in every advertisement. The scanning device will miss most of the advertisements, thus you could implement regularly retransmit etc. etc" - can you give some example? Because I need to send 100 empty bytes and it is doesn't matter whether devices get it. Another solution can be like this: Start Advertising 20 bytes .. timeout in 1 ms ... Start Advertising 20 bytes...timeout in 1 ms... Do it 5 times and then wait threshold 20 seconds. But I don't know how to implement this. Feb 7, 2019 at 11:56
  • Sorry, I could tell you for CSR controllers... How do you register the advertising data? In the same way you could register the new data from time to time. Note that advertising is done on multiple channels one by one. And the phone is listening only on one channel at a time. Both will hit the same channel from time to time. That is why much of the advertising data will be lost.
    – maze
    Feb 8, 2019 at 9:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.