蓝牙外设 ADVERTISE_FAILED_DATA_TOO_LARGE
Bluetooth Peripheral ADVERTISE_FAILED_DATA_TOO_LARGE
我正在尝试在 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 都不够 space。因此,如果您包括以上内容,请添加:
advertiseData.addServiceUuid(new ParcelUuid(serviceUUID));
您将收到您描述的错误。尝试删除第一行。
基本上你的数据超过了31个字节,所以你需要trim下来。
将其更改为 false,然后它将起作用:
advertiseData.setIncludeDeviceName(false);
您可以将设备名称更改为更短的名称。
示例:XYZ_Name_pic -> XYZ
转到蓝牙 -> 设置 -> 重命名设备名称
或者您可以传递 false 而不是 true
advertiseData.setIncludeDeviceName(假); // 传假不真
我正在尝试在 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 都不够 space。因此,如果您包括以上内容,请添加:
advertiseData.addServiceUuid(new ParcelUuid(serviceUUID));
您将收到您描述的错误。尝试删除第一行。
基本上你的数据超过了31个字节,所以你需要trim下来。
将其更改为 false,然后它将起作用:
advertiseData.setIncludeDeviceName(false);
您可以将设备名称更改为更短的名称。 示例:XYZ_Name_pic -> XYZ
转到蓝牙 -> 设置 -> 重命名设备名称
或者您可以传递 false 而不是 true
advertiseData.setIncludeDeviceName(假); // 传假不真