Search code examples
androidbluetooth-lowenergyuideddystonegoogle-nearby

nearby messages api: how to retrieve pictures and videos?


I'm actually working on an android mobile application for proximity marketing, my app is supposed to receive cards and coupons from marketers and display the received items.

The following code is from google developers and it demonstrates using a message listener to parse messages received from a BLE beacon:

mMessageListener = new MessageListener() {
    @Override
    public void onFound(Message message) {
        // Do something with the message here.
        Log.i(TAG, "Message found: " + message);
        Log.i(TAG, "Message string: " + new String(message.getContent()));
        Log.i(TAG, "Message namespaced type: " + message.getNamespace() +
                "/" + message.getType());
    }

    ...
};

My question is:

in the code above the message parsed is a string so it's simple to handle that I think, in my case I have to receive cards and coupons so pictures .. how could I handle and parse the received pictures?


Solution

  • Beacon attachments with the Nearby APIs are limited to 1024 bytes of arbitrary data, and are typically Base64 encoded for storage as strings. While in theory you could take binary images, Base64 encode them, and store them as attachments, the images would have to be 1024 bytes or smaller (not counting Base64 encoding overhead.) Bottom line: attachments can't transport large images.

    A typical alternative is to put your image on a web server at a public URL and store the URL in the attachment. You then write code inside the onFound() method above to kick off a background thread to fetch the image with an HTTP client. Once this returns, you can display the image. You can see an example of fetching an image here.

    Of course, this requires network access for everything to work, but the Nearby APIs really require that, anyway, otherwise it would be impossible to fetch the attachments from Google's cloud servers.