Bluetooth Peripheral ADVERTISE_FAILED_DATA_TOO_LARGE

I am trying to advertise in NEXUS 9 and get the error ADVERTISE_FAILED_DATA_TOO_LARGE. It worked fine when I added the service after successful advertising, but if I add the service through the Builder advertising agent so that other devices can filter during the scan, I get error code 1, i.e. ADVERTISE_FAILED_DATA_TOO_LARGE

a) Work code

public void startAdvertisingService() { AdvertiseSettings settings = new AdvertiseSettings.Builder() .setTxPowerLevel(AdvertiseSettings.ADVERTISE_TX_POWER_HIGH) .setTimeout(0) .setAdvertiseMode(AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY) .build(); AdvertiseData.Builder advertiseData = new AdvertiseData.Builder(); advertiseData.setIncludeDeviceName(true); BluetoothLeAdvertiser myBluetoothLeAdvertiser = btAdapter.getBluetoothLeAdvertiser(); myBluetoothLeAdvertiser.stopAdvertising(mAdvertiseCallback); myBluetoothLeAdvertiser.startAdvertising(settings, advertiseData.build(),mAdvertiseCallback); } private AdvertiseCallback mAdvertiseCallback = new AdvertiseCallback() { @Override public void onStartSuccess(AdvertiseSettings settingsInEffect) { super.onStartSuccess(settingsInEffect); BLEBroadcast(); } @Override public void onStartFailure(int errorCode) { String description = ""; if (errorCode == AdvertiseCallback.ADVERTISE_FAILED_FEATURE_UNSUPPORTED) description = "ADVERTISE_FAILED_FEATURE_UNSUPPORTED"; else if (errorCode == AdvertiseCallback.ADVERTISE_FAILED_TOO_MANY_ADVERTISERS) description = "ADVERTISE_FAILED_TOO_MANY_ADVERTISERS"; else if (errorCode == AdvertiseCallback.ADVERTISE_FAILED_ALREADY_STARTED) description = "ADVERTISE_FAILED_ALREADY_STARTED"; else if (errorCode == AdvertiseCallback.ADVERTISE_FAILED_DATA_TOO_LARGE) description = "ADVERTISE_FAILED_DATA_TOO_LARGE"; else if (errorCode == AdvertiseCallback.ADVERTISE_FAILED_INTERNAL_ERROR) description = "ADVERTISE_FAILED_INTERNAL_ERROR"; else description = "unknown"; } }; 

as well as adding services:

  void BLEBroadcast() { BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(characteristicUUID, BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_INDICATE | BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE, BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE); BluetoothGattDescriptor desc = new BluetoothGattDescriptor(descriptorUUID, BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE); desc.setValue("".getBytes()); characteristic.addDescriptor(desc); BluetoothGattService service = new BluetoothGattService(serviceUUID, BluetoothGattService.SERVICE_TYPE_PRIMARY); service.addCharacteristic(characteristic); mGattServer.addService(service); } BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_INDICATE | BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE, BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE);  void BLEBroadcast() { BluetoothGattCharacteristic characteristic = new BluetoothGattCharacteristic(characteristicUUID, BluetoothGattCharacteristic.PROPERTY_NOTIFY | BluetoothGattCharacteristic.PROPERTY_INDICATE | BluetoothGattCharacteristic.PROPERTY_READ | BluetoothGattCharacteristic.PROPERTY_WRITE, BluetoothGattCharacteristic.PERMISSION_READ | BluetoothGattCharacteristic.PERMISSION_WRITE); BluetoothGattDescriptor desc = new BluetoothGattDescriptor(descriptorUUID, BluetoothGattDescriptor.PERMISSION_READ | BluetoothGattDescriptor.PERMISSION_WRITE); desc.setValue("".getBytes()); characteristic.addDescriptor(desc); BluetoothGattService service = new BluetoothGattService(serviceUUID, BluetoothGattService.SERVICE_TYPE_PRIMARY); service.addCharacteristic(characteristic); mGattServer.addService(service); } 

b) Does not work when adding a service initially, so that it can be detected using the central filter:

calling the BLEBroadcast() function before calling startAdvertisingService() , as well as adding

  AdvertiseData.Builder advertiseData = new AdvertiseData.Builder(); advertiseData.addServiceUuid(new ParcelUuid(serviceUUID)); 

gives an advertising failure with error code 1.

+7
android bluetooth bluetooth-lowenergy cbperipheral
source share
2 answers

I suspect this is a line of code causing problems:

 advertiseData.setIncludeDeviceName(true); 

The declaration will not take place for both the device name and the 16-byte UUID of the service. Therefore, if you included above, add:

 advertiseData.addServiceUuid(new ParcelUuid(serviceUUID)); 

You will receive an error message. Try to delete the first line.

+16
source share

Basically your data exceeds 31 bytes, so you need to trim it.

change it to fasle, it will work.

advertiseData.setIncludeDeviceName (false);

+2
source share

All Articles