Nearby Connection API

Nearby connections is a peer-to-peer networking API that allows apps to easily connect, discover and exchange data with the nearby device in real-time, regardless of network connectivity. In other words, we can say that Nearby connections enable advertising, discovery and connection between nearby devices in a fully offline peer-to-peer manner. The connection between devices is high-bandwidth, low-latency and fully encrypted to enable fast, secure data transfers. Nearby connections API uses a combination of Bluetooth, BLE and wifi hotspots.

Nearby Connection API implementations

We implement nearby connections API in two phases:

  1. Pre-Connection
  2. Post-Connection

In the pre-connection phase, the Advertiser device advertises itself, while discovers devices search the nearby advertiser devices and send connection requests. A connection request from a Discoverer to an Advertiser initiates a symmetric authentication flow that results in both sides independently accepting (or rejecting) the connection request.

After a connection request is accepted by both sides, the connection is considered to be established and the devices enter the post-connection phase, during which both sides can exchange data.

How to advertise the device?

Advertisers begin by invoking startAdvertising(), passing in a ConnectionLifecycleCallback which will be notified whenever a Discoverer wants to connect via the onConnectionInitiated() callback.

How to discover devices?

Discoverers begin by invoking startDiscovery(), passing in an EndpointDiscoveryCallback which will be notified whenever a nearby Advertiser is found via the onEndpointFound() callback.

Steps for implementing Nearby Connection API:

There are following steps for implementing nearby connection API as follows:

1. Add dependency for nearby connections

2. Add permission in Manifest.xml

3. Advertise the advertiser devices

4. Discover the advertiser devices

5. Create a connectionLifecycleCallback class

6. Create a EndPointDiscoveryCallback

7. Requesting to create a connection

8. Accepting a connection request

9. For rejecting the connection request

10. For sending the data to the endpoint

11. For receiving the data

12. To stop all endpoints

13. To disconnect from the endpoint

14. For stop advertising

15. To stop discovering

 

Leave a Reply