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.
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())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With