Android BLE 广告失败,错误代码为 1

use*_*651 3 android bluetooth bluetooth-lowenergy

我有一个示例应用程序,它使用 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 );
}
Run Code Online (Sandbox Code Playgroud)

我希望这会宣传数据“D”,而不是因错误代码 1 而失败。

Gre*_*ens 6

在我看来,您将 pUuid 添加到广告数据中两次。一次单独使用,第二次使用数据“D”。BLE 广告只有 1 个 UUID 的空间。尝试消除第一个调用:

.addServiceUuid(pUuid)
Run Code Online (Sandbox Code Playgroud)

而是只使用:

.addServiceData(pUuid, "D".getBytes())
Run Code Online (Sandbox Code Playgroud)