Reputation: 820
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.
Upvotes: 5
Views: 3480
Reputation: 3884
I tried both above solution of @Greg Moens and @S. Gysin, and what I found is
You just have to change the ble_uuid
value CDB7950D-73F1-4D4D-8E47-C090502DBD63
with 00001101-0000-1000-8000-00805F9B34FB
.
It works for me.
Also you can check the complete blogpost for the same here: https://code.tutsplus.com/tutorials/how-to-advertise-android-as-a-bluetooth-le-peripheral--cms-25426
Upvotes: 1
Reputation: 11
The "serviceDataUuid" is 16-bit only. The method .addServiceData extracts silently a 16-bit UUID from a given 128-bit UUID, if the UUID is from the Bluetooth SIG. From your custom UUID = CDB7950D-73F1-4D4D-8E47-C090502DBD63, you must create a 16-bit UUID which is within the address range of the Bluetooth SIG.
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() );
ParcelUuid pUuid = new ParcelUuid( UUID.fromString("cdb7950d-73f1-4d4d-8e47-c090502dbd63"));
ParcelUuid pServiceDataUuid = new ParcelUuid(UUID.fromString("0000950d-0000-1000-8000-00805f9b34fb"));
AdvertiseData data = new AdvertiseData.Builder()
.setIncludeDeviceName(false)
.setIncludeTxPowerLevel(false)
.addServiceUuid( pUuid )
.addServiceData( pServiceDataUuid, "D".getBytes() )
.build();
advertiser.startAdvertising( settings, data, advertisingCallback );
}
Upvotes: 1
Reputation: 1825
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())
Upvotes: 8