Search code examples
google-beacon-platformproximityapi

Invalid AdvertisedId id bytes length


I am trying to register my beacon on OAuth2.0 Playground. When I am trying to register my beacon, it gives me following error:

{   "error": {
"status": "INVALID_ARGUMENT", 
"message": "Invalid AdvertisedId id bytes length", 
"code": 400   } 
}

I am sending a POST request to https://proximitybeacon.googleapis.com/v1beta1/beacons:register with following application/json body:

 {
  "advertisedId": {
    "type": "EDDYSTONE",
    "id": "ZWRkMWViXWFjMDRlNWRlZmEwNTdkMGU3ZDAwMmQ4YmM="
  },
  "status": "ACTIVE",
}

I am calculating advertiseID of beacon by this:

InstanceID: e61bdd5c2a9a

Namespace: edd1ebfac04e5defa017

I am creating the advertiseId by this method:

[1] Concatenate Namespace+Instance. => edd1ebfac04e5defa017e61bdd5c2a9a

[2] Convert it to byte stream using following code:

byte[] message = "edd1ebfac04e5defa017e61bdd5c2a9a".getBytes(StandardCharsets.UTF_8);

[3] Then convert it to Base64 using following code:

String encoded = Base64.getEncoder().encodeToString(message);

Now encoded is our advertisedId which is ZWRkMWViXWFjMDRlNWRlZmEwNTdkMGU3ZDAwMmQ4YmM=

Can anyone help me?


Solution

  • This is a reasonably common problem with converting between the hex values for the beacon ID and the actual underlying binary values for these.

    The base64 string "ZWRkMWViXWFjMDRlNWRlZmEwNTdkMGU3ZDAwMmQ4YmM=" is actually the base64 encoding of the text hex string "edd1ebfac04e5defa017e61bdd5c2a9a". But what you really need to do is base64 encode the binary values underlying this hex string.

    In Node.jS, for example:

    var b = Buffer("edd1ebfac04e5defa017e61bdd5c2a9a", "hex");
    b.length;
    > 16 bytes    // The 32 char string above represents 16 bytes!
    
    b.toString("base64");
    > '7dHr+sBOXe+gF+Yb3Vwqmg=='
    b.toString("base64").length;
    > 24
    

    So, as you can see, you should be getting a base64 string that's roughly 24 bytes in length.

    So, your conversion function should be something along the following lines:

    String convertHexBeaconIDToAdvertisementID(String hex) {
        byte[] bytes = ByteString.decodeHex(hex).toByteArray();
        return Base64.getEncoder().encodeToString(bytes);
    }