android– 蓝牙外设ADVERTISE_FAILED_DATA_TOO_LARGE

android– 蓝牙外设ADVERTISE_FAILED_DATA_TOO_LARGE,第1张

概述我试图在NEXUS9中做广告并得到ADVERTISE_FAILED_DATA_TOO_LARGE的错误.在成功通告后添加服务时,它工作得非常好,但是如果我通过“广告数据”构建器添加服务以便其他设备可以在扫描时进行过滤,则会收到错误代码1,即ADVERTISE_FAILED_DATA_TOO_LARGEa)工作准则publicvoids

我试图在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)最初添加服务时不起作用,以便中央通过过滤器发现:

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

        AdvertiseData.Builder advertiseData = new AdvertiseData.Builder();        advertiseData.addServiceUuID(new ParcelUuID(serviceUUID)); 

出现广告失败,错误代码为1.

解决方法:

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

advertiseData.setIncludeDevicename(true);

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

advertiseData.addServiceUuID(new ParcelUuID(serviceUUID)); 

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

总结

以上是内存溢出为你收集整理的android – 蓝牙外设ADVERTISE_FAILED_DATA_TOO_LARGE全部内容,希望文章能够帮你解决android – 蓝牙外设ADVERTISE_FAILED_DATA_TOO_LARGE所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/web/1098081.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-28
下一篇 2022-05-28

发表评论

登录后才能评论

评论列表(0条)

保存