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);
}
AdvertiseData.Builder advertiseData = new AdvertiseData.Builder();
advertiseData.addServiceUuid(new ParcelUuid(serviceUUID));
我怀疑是这行代码引起了麻烦:
advertiseData.setIncludeDeviceName(true);
广告将没有足够的空间容纳设备名和16字节服务UUID。因此,如果您包括上述内容,则添加:
advertiseData.addServiceUuid(new ParcelUuid(serviceUUID));
您将得到您描述的错误。试着删除第一行。
deviceName String deviceId String
deviceName String 设备名称 deviceId String 设备Id BluetoothDevice Object deviceName String deviceId String
deviceName String 设备名称 deviceId String 设备Id
连接设备 接口说明 用于连接 扫描出来的蓝牙设备。 参数说明 字段 类型 必须? 说明 device RKBLEDevice 是 蓝牙设备 示例代码 Swift: RokidMobileSDK.binder.connect(device: RKBLEDevice) Objc: [RokidMobileSDK.binder connect:device]; 断开设备 接口说明 用于断开已经连接的
开启蓝牙扫描 参数说明 字段 类型 必须? 说明 blePrefix String 是 设备名称类型前缀 示例代码 Swift: RokidMobileSDK.binder.startBLEScan(blePrefix: String) ->Bool Objc: [RokidMobileSDK.binder startBLEScanWithBlePrefix:@"Rokid-"]; 手机蓝牙未打
释放蓝牙资源 接口说明 建议在配网结束后调用 举个大栗子 RokidMobileSDK.binder.releaseBT()