1
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
            .Builder()
            .addServiceData(pUuid,tek.toByteArray()).build()


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

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

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.

Hally
  • 81
  • 8
  • If you can't use extended advertisements as M. Kotzjan suggested, then another solution would be to divide the advert into two and add sequence numbers to. In other words, you would advertise the above data as follows:- (12QUNf4ScRXQ9mJDVI7k5T1THGr7l7F) and (22vpquk4ASMySRn8=). The first number (1) is the sequence of the data and the second number (2) is the total number of packets in that data. Then the scanning device can piece the information together when it reads both adverts. – Youssif Saeed Jun 28 '21 at 06:28
  • How should I do that? Can I divide the data then repeat the broadcasting? But if I do that, it gives onStartFailure: 3, which means the advertising is already started. – Hally Jun 28 '21 at 15:06
  • You have to stop ongoing advertisement then start a new one with the updated data. – Youssif Saeed Jun 28 '21 at 16:50
  • As my data is around 44bytes, I am trying to divide into 5 strings. Should I set time for advertising for each string data and stop advertisement? – Hally Jun 28 '21 at 17:23
  • Why 5 strings? You can accomplish what you want with 2 strings only. In any case even if you divide into 5 strings, then yes you need to stop and start advertising for each string - maybe have a 1 or 2 second timer that sends a new string when the timer expires. If you find that this is not working for you then maybe sending the data via advert is not the most suitable option and you have to use GATT exchange instead. Have a look at this answer for more information on that:- https://stackoverflow.com/a/65645550/2215147 – Youssif Saeed Jun 28 '21 at 17:34
  • It is 5 strings because the advertisement gives errorcode 1, which means the data is too long when the string is longer than 10 characters. I think this is because pUuid and other things are added to the data for advertisement. – Hally Jun 28 '21 at 17:38

1 Answers1

1

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.

Michael Kotzjan
  • 2,093
  • 2
  • 14
  • 23