Shu*_*ham 11 peripherals android bluetooth bluetooth-lowenergy cbperipheral
我试图在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";
}
};
Run Code Online (Sandbox Code Playgroud)
并且还添加了服务:
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);
}
Run Code Online (Sandbox Code Playgroud)
b)最初添加服务时不起作用,以便中央通过过滤器发现:
在调用BLEBroadcast()之前调用函数startAdvertisingService()并添加
AdvertiseData.Builder advertiseData = new AdvertiseData.Builder();
advertiseData.addServiceUuid(new ParcelUuid(serviceUUID));
Run Code Online (Sandbox Code Playgroud)
出现广告失败,错误代码为1.
dav*_*ung 29
我怀疑这是引起麻烦的代码行:
advertiseData.setIncludeDeviceName(true);
Run Code Online (Sandbox Code Playgroud)
广告将没有足够的空间用于设备名称和16字节服务UUID.因此,如果您包含上述内容,请添加:
advertiseData.addServiceUuid(new ParcelUuid(serviceUUID));
Run Code Online (Sandbox Code Playgroud)
您将收到您描述的错误.尝试删除第一行.
基本上你的数据超过了 31 个字节,所以你需要修剪它。
将其更改为 false,然后它就可以工作:
advertiseData.setIncludeDeviceName(false);
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6216 次 |
| 最近记录: |