蓝牙外设ADVERTISE_FAILED_DATA_TOO_LARGE

时间:2015-07-18 11:22:46

标签: android bluetooth bluetooth-lowenergy cbperipheral peripherals

我正在尝试在NEXUS 9中做广告并获得ADVERTISE_FAILED_DATA_TOO_LARGE的错误。在成功通告后添加服务时,它工作得非常好,但如果我通过“广告数据”构建器添加服务,以便其他设备可以在扫描时过滤,则会收到错误代码1,即ADVERTISE_FAILED_DATA_TOO_LARGE

a)工作守则

     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";

    }
};

并添加服务:

 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)最初添加服务时无法正常工作,以便中央通过过滤器发现:

在调用BLEBroadcast()之前调用startAdvertisingService()函数并添加

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

广告失败,错误代码为1。

3 个答案:

答案 0 :(得分:28)

我怀疑这是引起麻烦的代码行:

advertiseData.setIncludeDeviceName(true);

广告没有足够的空间用于设备名称和16字节服务UUID。因此,如果您包含上述内容,请添加:

advertiseData.addServiceUuid(new ParcelUuid(serviceUUID)); 

您将收到您描述的错误。尝试删除第一行。

答案 1 :(得分:3)

基本上,您的数据超过31个字节,因此您需要将其削减。

将此更改为false,然后它将起作用:

advertiseData.setIncludeDeviceName(false);

答案 2 :(得分:1)

您可以将设备名称更改为更短的名称。 例如:XYZ_Name_pic-> XYZ

转到蓝牙->设置->重命名设备名称

或者您可以通过false代替true

advertiseData.setIncludeDeviceName(false); //传递false不正确