Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android BLE Advertisement fails with error code 1

I have a sample app that is using BLE to advertise some data. However, my Advertisement fails with error code 1. Error code 1 basically means that the payload is bigger than the allowed 31 bytes for the advertisement packet. But from my code, I can see that the payload is less than 31 bytes. Where is the issue?

Some suggested turning off device name advertisement as a long name will take space. I have done that as well.

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 )
            .setTimeout(0)
            .setConnectable( false )
            .build();
    ParcelUuid pUuid = new ParcelUuid( UUID.fromString( getString( R.string.ble_uuid ) ) );
    //ParcelUuid pUuid = new ParcelUuid( UUID.randomUUID() );


    AdvertiseData data = new AdvertiseData.Builder()
            .setIncludeDeviceName(false)
            .setIncludeTxPowerLevel(false)

            .addServiceUuid( pUuid )
            .addServiceData( pUuid, "D".getBytes() )
            .build();
    advertiser.startAdvertising( settings, data, advertisingCallback );
}

I expect this to advertise data "D", not fail with error code 1.

like image 831
user3656651 Avatar asked Aug 02 '19 15:08

user3656651


1 Answers

It looks to me like you are adding pUuid to the advertisement data twice. Once by itself and a second time with the data "D". BLE advertisements only have room for 1 UUID. Try eliminating that first call to:

.addServiceUuid(pUuid)

and instead only use:

.addServiceData(pUuid, "D".getBytes())
like image 197
Greg Moens Avatar answered Nov 11 '22 21:11

Greg Moens