{"id":4925,"date":"2018-07-31T12:33:27","date_gmt":"2018-07-31T07:03:27","guid":{"rendered":"https:\/\/www.innovationm.com\/blog\/?p=4925"},"modified":"2023-01-20T18:55:36","modified_gmt":"2023-01-20T13:25:36","slug":"nearby-api-post-connection-phase","status":"publish","type":"post","link":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/","title":{"rendered":"Nearby API Post-Connection Phase"},"content":{"rendered":"<p>We have already discussed near pre-connection phase in our previous blog.<br \/>\nCheck this reference :\u00a0<a href=\"https:\/\/www.innovationm.com\/blog\/nearby-api-pre-connection-phase\/\">https:\/\/www.innovationm.com\/blog\/nearby-api-pre-connection-phase\/<\/a><\/p>\n<p>Now, in this blog, we will have a look at the post-connection phase.<\/p>\n<p>After the connection between advertiser and discoverer is made successfully, post-connection phase starts.<br \/>\nIn the post-connection phase, there is no distinction between advertiser and discoverer.<br \/>\nBoth can send data to each other.<br \/>\nSharing of data happens in the form of Payload objects.<\/p>\n<p><strong>Types of Payload :<\/strong><\/p>\n<p><strong>1. Bytes<br \/>\n<\/strong>These are the simplest types of payloads.<br \/>\nIf you want to send simple data like messages or metadata(data about data) then it must be your choice.<br \/>\nFor 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.<\/p>\n<pre class=\"\">Payload payload = Payload.fromBytes(bytesArray);<\/pre>\n<p>To retrieve BYTE payload on the receiving side, you need to call payload.asBytes() which will return the byte array.<\/p>\n<p><strong>2. File<\/strong><br \/>\nIf 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.<br \/>\nFor creating a FILE payload, you need to call Payload.fromFile() by passing either a java.io.File or a ParcelFileDescriptor as the parameter.<\/p>\n<pre class=\"\">Payload payload = Payload.fromFile(file);<\/pre>\n<p>To retrieve FILE payload on the receiving side, you need to call payload.asFile().asJavaFile() or payload.asFile().asParcelFileDescriptor().<\/p>\n<p><strong>3. Stream<\/strong><br \/>\nIn case you want to send a large amount of data such as an audio stream then Stream payloads would be the right choice.<br \/>\nFor creating a FILE payload, you need to call Payload.fromStream() by passing in either an InputStream or a ParcelFileDescriptor as the parameter.<\/p>\n<pre class=\"\">Payload payload = Payload.fromStream(is);<\/pre>\n<p>To retrieve FILE payload on the receiving side, you need to call payload.asStream().asInputStream() or payload.asStream().asParcelFileDescriptor().<\/p>\n<p><strong>Sending and Receiving<\/strong><\/p>\n<p>In order to send payloads to a conneted endpoint, you need to call sendPayload() method.<\/p>\n<pre class=\"\">Nearby.getConnectionsClient(context).sendPayload(endPointId, payload)\r\n        .addOnSuccessListener(new OnSuccessListener() {\r\n            @Override\r\n            public void onSuccess(Void aVoid) {\r\n                \/\/Called if the sendPayload() method is called successfully.\r\n            }\r\n        })\r\n        .addOnFailureListener(new OnFailureListener() {\r\n            @Override\r\n            public void onFailure(@NonNull Exception e) {\r\n                \/\/Called if there is a failure in calling sendPayload() method.\r\n            }\r\n        });<\/pre>\n<p>In order to receive payloads, you need to implement onPayloadReceived() method.<br \/>\nonPayloadReceived() method is one of the two methods of PayloadCallback class.<br \/>\nAn instance of this class is passed on both sides, while accepting connection, in acceptConnection() method.<br \/>\nAs 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.<\/p>\n<pre class=\"\">@Override\r\npublic void onPayloadReceived(String endPointId, Payload payload)\r\n{\r\n    if (payload.getType() == Payload.Type.BYTES)\r\n    {\r\n        \/\/triggered when the first byte of BYTE type payload is received.\r\n    }\r\n    else if (payload.getType() == Payload.Type.FILE)\r\n    {\r\n        \/\/triggered when the first byte of FILE type payload is received.\r\n    }\r\n    else if (payload.getType() == Payload.Type.STREAM)\r\n    {\r\n        \/\/triggered when the first byte of STREAM type payload is received.\r\n    }\r\n}<\/pre>\n<p><strong>Payload Progress Update<\/strong><\/p>\n<p>The second method of PayloadCallback class is onPayloadTransferUpdate() which updates us about the progress of both incoming and outgoing payloads.<br \/>\nThat means, this method receives a callback on both sending and receiving sides.<\/p>\n<p>When the payload is transferred completely, onPayloadTransferUpdate() is called with a status of PayloadTransferUpdate.Status.SUCCESS.<\/p>\n<pre class=\"\">@Override\r\npublic void onPayloadTransferUpdate(String endpointId, PayloadTransferUpdate payloadTransferUpdate)\r\n{\r\n    if (payloadTransferUpdate.getStatus() == PayloadTransferUpdate.Status.SUCCESS)\r\n    {\r\n        \/\/triggered when the payload is transferred completely\r\n    }\r\n    else if(payloadTransferUpdate.getStatus() == PayloadTransferUpdate.Status.FAILURE)\r\n    {\r\n        \/\/do something here if there is a failure\r\n    }\r\n}<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>We have already discussed near pre-connection phase in our previous blog. Check this reference :\u00a0https:\/\/www.innovationm.com\/blog\/nearby-api-pre-connection-phase\/ Now, in this blog, we will have a look at the post-connection phase. After the connection between advertiser and discoverer is made successfully, post-connection phase starts. In the post-connection phase, there is no distinction between advertiser and discoverer. Both can [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":4946,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2,71],"tags":[159,294,295,296],"class_list":["post-4925","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-android","category-mobile","tag-android","tag-google-api","tag-nearby-api","tag-post-connection"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.5 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Nearby API Post-Connection Phase - InnovationM - Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Nearby API Post-Connection Phase - InnovationM - Blog\" \/>\n<meta property=\"og:description\" content=\"We have already discussed near pre-connection phase in our previous blog. Check this reference :\u00a0https:\/\/www.innovationm.com\/blog\/nearby-api-pre-connection-phase\/ Now, in this blog, we will have a look at the post-connection phase. After the connection between advertiser and discoverer is made successfully, post-connection phase starts. In the post-connection phase, there is no distinction between advertiser and discoverer. Both can [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/\" \/>\n<meta property=\"og:site_name\" content=\"InnovationM - Blog\" \/>\n<meta property=\"article:published_time\" content=\"2018-07-31T07:03:27+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-20T13:25:36+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/Nearby-post-Connection.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1140\" \/>\n\t<meta property=\"og:image:height\" content=\"633\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"InnovationM Admin\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"InnovationM Admin\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/nearby-api-post-connection-phase\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/nearby-api-post-connection-phase\\\/\"},\"author\":{\"name\":\"InnovationM Admin\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"headline\":\"Nearby API Post-Connection Phase\",\"datePublished\":\"2018-07-31T07:03:27+00:00\",\"dateModified\":\"2023-01-20T13:25:36+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/nearby-api-post-connection-phase\\\/\"},\"wordCount\":433,\"commentCount\":0,\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/nearby-api-post-connection-phase\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/Nearby-post-Connection.jpg\",\"keywords\":[\"Android\",\"google-api\",\"nearby-api\",\"post-connection\"],\"articleSection\":[\"Android\",\"Mobile\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/nearby-api-post-connection-phase\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/nearby-api-post-connection-phase\\\/\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/nearby-api-post-connection-phase\\\/\",\"name\":\"Nearby API Post-Connection Phase - InnovationM - Blog\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/nearby-api-post-connection-phase\\\/#primaryimage\"},\"image\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/nearby-api-post-connection-phase\\\/#primaryimage\"},\"thumbnailUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/Nearby-post-Connection.jpg\",\"datePublished\":\"2018-07-31T07:03:27+00:00\",\"dateModified\":\"2023-01-20T13:25:36+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/nearby-api-post-connection-phase\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/nearby-api-post-connection-phase\\\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/nearby-api-post-connection-phase\\\/#primaryimage\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/Nearby-post-Connection.jpg\",\"contentUrl\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/wp-content\\\/uploads\\\/2018\\\/07\\\/Nearby-post-Connection.jpg\",\"width\":1140,\"height\":633},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/nearby-api-post-connection-phase\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Nearby API Post-Connection Phase\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#website\",\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/\",\"name\":\"InnovationM - Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/#\\\/schema\\\/person\\\/a831bf4602d69d1fa452e3de0c8862ed\",\"name\":\"InnovationM Admin\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g\",\"caption\":\"InnovationM Admin\"},\"sameAs\":[\"http:\\\/\\\/www.innovationm.com\\\/\"],\"url\":\"https:\\\/\\\/www.innovationm.com\\\/blog\\\/author\\\/innovationmadmin\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Nearby API Post-Connection Phase - InnovationM - Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/","og_locale":"en_US","og_type":"article","og_title":"Nearby API Post-Connection Phase - InnovationM - Blog","og_description":"We have already discussed near pre-connection phase in our previous blog. Check this reference :\u00a0https:\/\/www.innovationm.com\/blog\/nearby-api-pre-connection-phase\/ Now, in this blog, we will have a look at the post-connection phase. After the connection between advertiser and discoverer is made successfully, post-connection phase starts. In the post-connection phase, there is no distinction between advertiser and discoverer. Both can [&hellip;]","og_url":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/","og_site_name":"InnovationM - Blog","article_published_time":"2018-07-31T07:03:27+00:00","article_modified_time":"2023-01-20T13:25:36+00:00","og_image":[{"width":1140,"height":633,"url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/Nearby-post-Connection.jpg","type":"image\/jpeg"}],"author":"InnovationM Admin","twitter_misc":{"Written by":"InnovationM Admin","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/#article","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/"},"author":{"name":"InnovationM Admin","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"headline":"Nearby API Post-Connection Phase","datePublished":"2018-07-31T07:03:27+00:00","dateModified":"2023-01-20T13:25:36+00:00","mainEntityOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/"},"wordCount":433,"commentCount":0,"image":{"@id":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/Nearby-post-Connection.jpg","keywords":["Android","google-api","nearby-api","post-connection"],"articleSection":["Android","Mobile"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/","url":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/","name":"Nearby API Post-Connection Phase - InnovationM - Blog","isPartOf":{"@id":"https:\/\/www.innovationm.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/#primaryimage"},"image":{"@id":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/#primaryimage"},"thumbnailUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/Nearby-post-Connection.jpg","datePublished":"2018-07-31T07:03:27+00:00","dateModified":"2023-01-20T13:25:36+00:00","author":{"@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed"},"breadcrumb":{"@id":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/#primaryimage","url":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/Nearby-post-Connection.jpg","contentUrl":"https:\/\/www.innovationm.com\/blog\/wp-content\/uploads\/2018\/07\/Nearby-post-Connection.jpg","width":1140,"height":633},{"@type":"BreadcrumbList","@id":"https:\/\/www.innovationm.com\/blog\/nearby-api-post-connection-phase\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.innovationm.com\/blog\/"},{"@type":"ListItem","position":2,"name":"Nearby API Post-Connection Phase"}]},{"@type":"WebSite","@id":"https:\/\/www.innovationm.com\/blog\/#website","url":"https:\/\/www.innovationm.com\/blog\/","name":"InnovationM - Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.innovationm.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.innovationm.com\/blog\/#\/schema\/person\/a831bf4602d69d1fa452e3de0c8862ed","name":"InnovationM Admin","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/5c99d9eece9dfbc82297cf34ddd58e9fe05bb52fe66c8f6bf6c0a45bfb6d7629?s=96&r=g","caption":"InnovationM Admin"},"sameAs":["http:\/\/www.innovationm.com\/"],"url":"https:\/\/www.innovationm.com\/blog\/author\/innovationmadmin\/"}]}},"_links":{"self":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4925","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/comments?post=4925"}],"version-history":[{"count":0,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/posts\/4925\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media\/4946"}],"wp:attachment":[{"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/media?parent=4925"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/categories?post=4925"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.innovationm.com\/blog\/wp-json\/wp\/v2\/tags?post=4925"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}