1

Eddystone ベースのアプリを開発しようとしています。Google のサンプル コードを取得して、変更してみました。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 の長さをチェックして、フォーマットの誤りによって長すぎるかどうかを確認することをお勧めします。また、バイト配列を 16 進文字列として出力し、それを質問に貼り付けることも役立つ場合があります。

次のようなコードで 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 に答える