Over a million developers have joined DZone.

Nearby API Post-Connection Phase

DZone's Guide to

Nearby API Post-Connection Phase

Let's take a look at the post-connection phase of an API. Explore the code as well as the types of payload, such as bytes and file.

· Integration Zone ·
Free Resource

How to Transform Your Business in the Digital Age: Learn how organizations are re-architecting their integration strategy with data-driven app integration for true digital transformation.

We have already discussed near the pre-connection phase in our previous article. Now, in this article, we will have a look at the post-connection phase.

After the connection between the advertiser and discoverer is made successfully, the post-connection phase starts. In the post-connection phase, there is no distinction between advertiser and discoverer. Both can send data to each other. Sharing of data happens in the form of Payload objects.

Types of Payload

1. Bytes

These are the simplest types of payloads. If you want to send simple data like messages or metadata (data about data), then it must be your choice.
For creating a BYTE payload, you need to call Payload.fromBytes(byte[] bytes), where bytes is a byte array with a maximum size of Connections.MAX_BYTES_DATA_SIZE.

Payload payload = Payload.fromBytes(bytesArray);

To retrieve BYTE payload on the receiving side, you need to call payload.asBytes(), which will return the byte array.

2. File

If you want to share a file stored on your local device such as a photo or video, then you should send them as a File payload.
For creating a FILE payload, you need to call Payload.fromFile() by passing either a java.io.File or a ParcelFileDescriptor as the parameter.

Payload payload = Payload.fromFile(file);

To retrieve FILE payload on the receiving side, you need to call payload.asFile().asJavaFile() or payload.asFile().asParcelFileDescriptor().

3. Stream

In case you want to send a large amount of data such as an audio stream then Stream payloads would be the right choice.
For creating a FILE payload, you need to call Payload.fromStream() by passing in either an InputStream or a ParcelFileDescriptor as the parameter.

Payload payload = Payload.fromStream(is);

To retrieve FILE payload on the receiving side, you need to call payload.asStream().asInputStream() or payload.asStream().asParcelFileDescriptor().

Sending and Receiving

In order to send payloads to a connected endpoint, you need to call sendPayload() method.

Nearby.getConnectionsClient(context).sendPayload(endPointId, payload)
        .addOnSuccessListener(new OnSuccessListener() {
            public void onSuccess(Void aVoid) {
                //Called if the sendPayload() method is called successfully.
        .addOnFailureListener(new OnFailureListener() {
            public void onFailure(@NonNull Exception e) {
                //Called if there is a failure in calling sendPayload() method.

In order to receive payloads, you need to implement onPayloadReceived() method.
onPayloadReceived() method is one of the two methods of PayloadCallback class.
An instance of this class is passed on both sides, while accepting the connection, in acceptConnection() method.
As soon as any one of the two(advertiser and discoverer) receives the very first byte of the incoming payload, a callback is triggered in onPayloadReceived() method.

public void onPayloadReceived(String endPointId, Payload payload)
    if (payload.getType() == Payload.Type.BYTES)
        //triggered when the first byte of BYTE type payload is received.
    else if (payload.getType() == Payload.Type.FILE)
        //triggered when the first byte of FILE type payload is received.
    else if (payload.getType() == Payload.Type.STREAM)
        //triggered when the first byte of STREAM type payload is received.

Payload Progress Update

The second method of PayloadCallback class is onPayloadTransferUpdate() which updates us on the progress of both incoming and outgoing payloads.
That means this method receives a callback on both sending and receiving sides.

When the payload is transferred completely, onPayloadTransferUpdate() is called with a status of PayloadTransferUpdate.Status.SUCCESS.

public void onPayloadTransferUpdate(String endpointId, PayloadTransferUpdate payloadTransferUpdate)
    if (payloadTransferUpdate.getStatus() == PayloadTransferUpdate.Status.SUCCESS)
        //triggered when the payload is transferred completely
    else if(payloadTransferUpdate.getStatus() == PayloadTransferUpdate.Status.FAILURE)
        //do something here if there is a failure

Make your mark on the industry’s leading annual report. Fill out the State of API Integration 2019 Survey and receive $25 to the Cloud Elements store.

android ,google api

Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}