蓝牙外设ADVERTISE_FAILED_DATA_TOO_LARGE [英] Bluetooth Peripheral ADVERTISE_FAILED_DATA_TOO_LARGE

查看:3070
本文介绍了蓝牙外设ADVERTISE_FAILED_DATA_TOO_LARGE的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在NEXUS 9做广告并获得ADVERTISE_FAILED_DATA_TOO_LARGE的错误。这是工作,当我加入该服务后,成功的广告,但如果我添加的服务,通过广告数据生成器完全没有这样的其他设备可以过滤在扫描时,收到错误code 1即ADVERTISE_FAILED_DATA_TOO_LARGE

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

一)工作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";

    }
};

和还添加服务:

 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)开始添加服务时,这样可以通过中央通滤波器被发现不工作:

b) Not working when adding service initially so that can be discovered by central through filter:

调用 BLEBroadcast()之前调用函数 startAdvertisingService(),并且还加入

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

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

给广告失败,错误code 1。

gives advertising failure with error code 1.

推荐答案

我怀疑这是code的造成麻烦的行:

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

advertiseData.setIncludeDeviceName(true);

该广告将不会有两个设备名称和一个16字节的服务UUID足够的空间。所以,如果你有上面再加入:

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.

这篇关于蓝牙外设ADVERTISE_FAILED_DATA_TOO_LARGE的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆