1

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

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

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

//error
Class: AdvertiseCallback
Error : 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);
4

1 回答 1

0

我怀疑它的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();
}
于 2016-01-22T13:46:10.240 回答