android – Can I advertise string that is longer than 31 bytes through BLE? – Code Utility


private fun advertise(tek:String){bluetoothManager.adapter.bluetoothLeAdvertiser
        val advertiser: BluetoothLeAdvertiser=BluetoothAdapter.getDefaultAdapter().bluetoothLeAdvertiser
        val settings = AdvertiseSettings.Builder().setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_BALANCED).setConnectable(true).setTimeout(0).setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_MEDIUM).build()
        val uuid = UUID.randomUUID()
        val pUuid = ParcelUuid(UUID.fromString(uuid.toString())) 
        val data: AdvertiseData = AdvertiseData

        val advertiseCallback = object: AdvertiseCallback(){
            override fun onStartSuccess(settingsInEffect: AdvertiseSettings){
                Log.d(TAG,"BLE Advertising start")

            override fun onStartFailure(errorCode: Int){
                Log.d(TAG,"BLE Advertising onStartFailure: $errorCode")
        advertiser.startAdvertising(settings, data, advertiseCallback)

This is code that I wrote to advertise through BLE. The data which is tek here is a string that is longer than 31 bytes (eg.QUNf4ScRXQ9mJDVI7k5T1THGr7l7Fvpquk4ASMySRn8=). Is there any way that I can advertise this through BLE? When I try to advertise this on the code above, it gives onStartFailure code :1, which means the data is too long.


This could be done using extended Advertisements, a feature added in BLE 5.0:

Extended advertising: Extended Advertisements are a way to advertise more (offloaded) data than what’s allowed with Legacy Advertisements. Offloading is accomplished by first advertising on the primary channel that points to an auxiliary packet on the secondary channel.

Note: Since non-Bluetooth 5 devices will not be able to discover extended advertisements, it is recommended that advertisers also use an advertising set with legacy advertising PDUs for older scanning devices to be able to discover the end device. Advertising sets are used to send out different types of advertising events simultaneously. Each advertisement set will have different advertisement parameters such as advertising PDU type, advertising interval, and PHY.

The Bluetooth Core specification (Version 5.2 | Vol 6, Part B | 4.6.12 LE Extended Advertising) specify that this would increase the possible payload to 255 bytes.

As stated in the quote above: If you need to support devices using a BLE version below 5 this won’t work and you have to decrease the size of your advertisement payload.