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()
}
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.