Bluetooth Peripheral ADVERTISE_FAILED_DATA_TOO_LARGE

旧时模样 提交于 2019-12-12 07:14:32

问题


I am trying to advertise in NEXUS 9 and getting the error of ADVERTISE_FAILED_DATA_TOO_LARGE. It was working perfectly fine when I was adding the service after successfully advertising but if I add the service through Advertise Data builder so that other devices can filter while scanning, I get error code 1 i.e ADVERTISE_FAILED_DATA_TOO_LARGE

a) Working 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";

    }
};

and also adding the service:

 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) Not working when adding service initially so that can be discovered by central through filter:

calling BLEBroadcast() function before calling startAdvertisingService() and also adding

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

gives advertising failure with error code 1.


回答1:


I suspect that this is the line of code causing the trouble:

advertiseData.setIncludeDeviceName(true);

The advertisement will not have enough space for both the device name and a 16 byte service UUID. So if you include the above then add:

advertiseData.addServiceUuid(new ParcelUuid(serviceUUID)); 

You will get the error you describe. Try removing the first line.




回答2:


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

Change this to false, then it will work:

advertiseData.setIncludeDeviceName(false);



回答3:


You can either change your device's name to something shorter. Example: XYZ_Name_pic - > XYZ

Go to bluetooth -> settings - > rename device name

or you can pass false instead of true

advertiseData.setIncludeDeviceName(false); // pass false not true



来源:https://stackoverflow.com/questions/31490649/bluetooth-peripheral-advertise-failed-data-too-large

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!