Android BLE - How to calculate ad size (overall and by component)?

I am promoting an Android device via Bluetooth Low Energy with the below code:

private void advertise() {
    BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();

    AdvertiseSettings settings = new AdvertiseSettings.Builder()
            .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY)
            .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH)
            .setConnectable(false)
            .build();

    ParcelUuid pUuid = new ParcelUuid(UUID.fromString(getString(R.string.ble_uuid)));

    byte[] messageToSend = "Data".getBytes(Charset.forName("UTF-8"));

    AdvertiseData data = new AdvertiseData.Builder()
            .addServiceUuid(pUuid)
            .addServiceData(pUuid, messageToSend)
            .build();

    AdvertiseCallback advertisingCallback = new AdvertiseCallback() {
        @Override
        public void onStartSuccess(AdvertiseSettings settingsInEffect) {
            Log.d("Successful", "start");
            super.onStartSuccess(settingsInEffect);
        }

        @Override
        public void onStartFailure(int errorCode) {
            Log.e("BLE", "Advertising onStartFailure: " + errorCode);
            super.onStartFailure(errorCode);
        }
    };

    advertiser.startAdvertising(settings, data, advertisingCallback);
}

      

The plan is this: send a message (in this case "Data") to each BLE compatible device next to the Bluetooth enabled with ads. I tested it and another device successfully received the "Data" string in the object AdvertiseData

. But as I know there are limitations to this approach (I may be wrong on any of the following points, please feel free to correct me;)):

  • The total size of advertising data (object AdvertiseData

    ) can be no more than 32 bytes.
  • From this structure, 20 bytes can be used to send custom advertising data.

If I understand the last point correctly, it means that I can send 20 UTF-8 encoded ASCII characters with one object AdvertiseData

(I would like to approach this situation, similar to the answer to this question for sending more than 20 bytes). However, I can currently send up to 9 ASCII characters - if I want to send another one, the callback method onStartFailure()

returns with errorCode

1 ( ADVERTISE_FAILED_DATA_TOO_LARGE

as per the documentation of the AdvertisingCallback class ). Of course, this "limit" is even smaller when I use non-ASCII characters (eg accented characters - é

, ű

etc.) ...

It is possible that my approach is not the best because I found different examples on the internet for sending ad data ... so maybe I am creating an object AdvertiseData

with some unnecessary object that takes up some free space in the data. So basically I would like to know:

  • How many bytes is the size of an object AdvertiseData

    ?
  • How is this object broken? What are the parts I should be thinking about while calculating, and how big are they?

Could you help me experiment?: D


My AdvertisingData object looks like this:

AdvertiseData 
  [mServiceUuids=
    [bdc7950d-731f-4d4d-8e47-c090502dbd63], 
  mManufacturerSpecificData={}, 
  mServiceData=
    {bdc7950d-731f-4d4d-8e47-c090502dbd63=[68, 97, 116, 97]}, 
  mIncludeTxPowerLevel=false,
  mIncludeDeviceName=false]

      

+3


source to share





All Articles