【问题标题】:Android Eddystone broadcast data too largeAndroid Eddystone 广播数据太大
【发布时间】:2016-01-22 13:03:40
【问题描述】:

我正在尝试开发基于 Eddystone 的应用程序。 我拿了谷歌示例代码并尝试修改它。 android 规范说服务数据的最大长度是 31 个字节

我尝试在以下代码中更改服务数据长度 构建服务数据()

这里最多只接受 20 个字节。 不止于此(例如 21 个字节)我得到了 ADVERTISE_FAILED_DATA_TOO_LARGE 错误

//错误
类:AdvertiseCallback
错误:ADVERTISE_FAILED_DATA_TOO_LARGE
由于要广播的广告数据大于31字节,无法启动广告。

我正在使用 UID 框架并在棒棒糖设备上进行测试。

请让我知道我做错了什么?

byte[] serviceData = null;
       

    //  1+1+10+6+1+1+1   = 21 bytes
     private byte[] buildServiceData() throws IOException {
          
     byte txPower = txPowerLevelToByteValue();
         
     byte[] namespaceBytes = toByteArray(namespace.getText().toString());
             
     byte[] instanceBytes = toByteArray(instance.getText().toString());
            
     ByteArrayOutputStream os = new ByteArrayOutputStream();
     
     os.write(new byte[]{FRAME_TYPE_UID, txPower});
      

     os.write(namespaceBytes);
            
     os.write(instanceBytes);
        
            

//for testing only
         
 //  os.write(new byte[]{txPower});
         
  // os.write(new byte[]{txPower});
      
  //    os.write(new byte[]{txPower});
        
     
       return os.toByteArray();
      

 }

 //advertise the data
 AdvertiseData advertiseData = new AdvertiseData.Builder()
            .addServiceData(SERVICE_UUID, serviceData)
            .addServiceUuid(SERVICE_UUID)
            
.setIncludeTxPowerLevel(false)
           
 .setIncludeDeviceName(false)
            
.build();
        
        

namespace.setError(null);
        
instance.setError(null);
        

setEnabledViews(false, namespace, instance, rndNamespace, rndInstance, txPower, txMode);
        
adv.startAdvertising(advertiseSettings, advertiseData, advertiseCallback);

【问题讨论】:

  • 在一部具有最低规格和 Android 8.1.0 (Oreo Go) 的手机中,我只能附加 .addServiceData(SERVICE_UUID, serviceData) 和 addServiceUuid(SERVICE_UUID) 中的一个(添加 .setIncludeDeviceName(真的) )。只有 .addServiceData(SERVICE_UUID, serviceData) 我可以发送 serviceData 最多 10 个字节而不会失败。我有另一部具有中等规格和 Android 7.1.2 (Nougat) 的手机,我可以在其中接收信号,但 serviceRecord.getServiceData() 返回一个空白地图。但是从 Nougat 中,我最多可以发送 24 个字节的数据。在 Oreo Go 中我可以检索数据,地图不为空

标签: java android maxlength bluetooth-lowenergy eddystone


【解决方案1】:

我怀疑serviceData 的格式不正确,以至于它的长度变得太大。很难说具体原因,因为没有显示所有代码。

我建议您检查 serviceData 的长度,看看格式错误是否使其过长。如果不清楚是什么导致其格式错误,将字节数组打印为十六进制字符串并将其粘贴到您的问题中也可能会有所帮助。

您可以使用如下代码打印出您的 serviceData:

// Put this line near the "advertise the data" comment
Log.d(TAG, "Service data bytes: "+byteArayToHexString(serviceData));


public static String byteArrayToHexString(byte[] bytes) {
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < bytes.length; i++) {
        sb.append(String.format("%02x", bytes[i]));
    }
    return sb.toString();
}

【讨论】:

  • 服务数据字节:00f040e6b49dc957ce78f5f972734c56f0ab - 无错误服务数据字节:00f040e6b49dc957ce78f5f972734c56f0abf0f0f0 - 错误
  • 正如我提到的,它最多可以工作 20 个字节,超过 20 个字节时会出错
  • 嗯,对于一个标准的 Eddystone-UID 帧,你只需要 18 个字节的数据,是吗?这样您就可以成功传输一个。您要做的是发送更大的非标准 Eddystone-UID 帧。没有错 - 我只是想澄清你想要做什么。而且我不相信你可以发送 31 字节的服务数据是真的。您在哪里找到的文档?
  • android 开发者文档 Class: AdvertiseCallback ADVERTISE_FAILED_DATA_TOO_LARGE 由于要广播的广告数据大于 31 字节,无法开始广告。我在描述中提到过。该文档清楚地提到了 31 个字节。我想在 UID 框架本身中发送一些额外的数据,而不使用其他框架(TLM 和 URL)。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-03-27
  • 1970-01-01
  • 2019-10-03
  • 2011-09-30
  • 1970-01-01
相关资源
最近更新 更多