Android BLE播发失败,错误代码为1

Android BLE播发失败,错误代码为1,android,bluetooth,bluetooth-lowenergy,Android,Bluetooth,Bluetooth Lowenergy,我有一个示例应用程序,它使用BLE发布一些数据。但是,我的广告失败,错误代码为1。错误代码1基本上意味着有效负载大于广告数据包允许的31字节。但是从我的代码中,我可以看到有效负载小于31字节。问题在哪里 一些人建议关闭设备名称广告,因为长名称会占用空间。我也这样做过 private void advertise(){ BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().ge

我有一个示例应用程序,它使用BLE发布一些数据。但是,我的广告失败,错误代码为1。错误代码1基本上意味着有效负载大于广告数据包允许的31字节。但是从我的代码中,我可以看到有效负载小于31字节。问题在哪里

一些人建议关闭设备名称广告,因为长名称会占用空间。我也这样做过

private void advertise(){
        BluetoothLeAdvertiser advertiser =         BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
    AdvertiseSettings settings = new AdvertiseSettings.Builder()
            .setAdvertiseMode( AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY )
            .setTxPowerLevel( AdvertiseSettings.ADVERTISE_TX_POWER_HIGH )
            .setTimeout(0)
            .setConnectable( false )
            .build();
    ParcelUuid pUuid = new ParcelUuid( UUID.fromString( getString( R.string.ble_uuid ) ) );
    //ParcelUuid pUuid = new ParcelUuid( UUID.randomUUID() );


    AdvertiseData data = new AdvertiseData.Builder()
            .setIncludeDeviceName(false)
            .setIncludeTxPowerLevel(false)

            .addServiceUuid( pUuid )
            .addServiceData( pUuid, "D".getBytes() )
            .build();
    advertiser.startAdvertising( settings, data, advertisingCallback );
}

我希望这将播发数据“D”,而不是失败,错误代码为1。

在我看来,您将向播发数据添加pUuid两次。一次自动,第二次使用数据“D”。BLE广告只能容纳1个UUID。尝试取消第一次呼叫:

.addServiceUuid(pUuid)
而只能使用:

.addServiceData(pUuid, "D".getBytes())
“serviceDataUuid”仅为16位。如果UUID来自蓝牙SIG,则addServiceData方法会从给定的128位UUID中静默提取16位UUID。从自定义UUID=CDB7950D-73F1-4D4D-8E47-C090502DBD63,必须创建一个位于蓝牙SIG地址范围内的16位UUID

private void advertise(){
    BluetoothLeAdvertiser advertiser = BluetoothAdapter.getDefaultAdapter().getBluetoothLeAdvertiser();
    AdvertiseSettings settings = new AdvertiseSettings.Builder()
            .setAdvertiseMode( AdvertiseSettings.ADVERTISE_MODE_LOW_LATENCY )
            .setTxPowerLevel( AdvertiseSettings.ADVERTISE_TX_POWER_HIGH )
            .setTimeout(0)
            .setConnectable( false )
            .build();
    //ParcelUuid pUuid = new ParcelUuid( UUID.fromString( getString( R.string.ble_uuid ) ) );
    //ParcelUuid pUuid = new ParcelUuid( UUID.randomUUID() );
    ParcelUuid pUuid = new ParcelUuid( UUID.fromString("cdb7950d-73f1-4d4d-8e47-c090502dbd63"));
    ParcelUuid pServiceDataUuid = new ParcelUuid(UUID.fromString("0000950d-0000-1000-8000-00805f9b34fb"));


    AdvertiseData data = new AdvertiseData.Builder()
            .setIncludeDeviceName(false)
            .setIncludeTxPowerLevel(false)

            .addServiceUuid( pUuid )
            .addServiceData( pServiceDataUuid, "D".getBytes() )
            .build();
    advertiser.startAdvertising( settings, data, advertisingCallback );
}

R.string.ble_uuid包含什么?CDB7950D-73F1-4D4D-8E47-C090502DBD63C您能告诉我您是如何在另一端接收数据的吗?我在获取正确编码的数据时遇到了问题看起来你是对的!我永远也想不到!