Nearby show available connections list

95 Views Asked by At

I want to use nearby connections in my app. Now I am using only the pre-connection state. The only thing that I supposed to know is if one device is in line of sight of another. But the requirements changed so now I need to connect two devices before I start other processes. So now I have few problems:

  1. The only thing that I suppose to pass to the discoverer is a string. I don't need a real stream or something. But if I pass string only once I receive disconnection in less than a minute. But as I mentioned before I need to have an open connection. So now I decided to pass the same string every 40 seconds. It seems useless but I can't find another way to keep the connection open.
  2. Google documentation says:

Depending on your use case, you may wish to instead display a list of discovered devices to the user, allowing them to choose which devices to connect to.

But I can't find an example of how to show avalible connections to the user. Current solution:

private val connectionLifecycleCallback: ConnectionLifecycleCallback =
        object : ConnectionLifecycleCallback() {
            override fun onConnectionInitiated(endpointId: String, connectionInfo: ConnectionInfo) {
                Timber.e("onConnectionInitiated $endpointId")
                Nearby.getConnectionsClient(context).acceptConnection(endpointId, receiveBytesPayloadListener)
                //automatic connection from both sides
            }

            override fun onConnectionResult(endpointId: String, result: ConnectionResolution) {
                Timber.e("onConnectionResult $endpointId")
                when (result.status.statusCode) {
                    ConnectionsStatusCodes.STATUS_OK -> {
                        Timber.e("onConnectionResult.STATUS_OK $endpointId ")
                        val bytesPayload = Payload.fromBytes("${UserRepo.rideId} ".toByteArray())
                        myTimer = Timer()
                        myTimer?.schedule(object : TimerTask() {
                            override fun run() {
                                Nearby.getConnectionsClient(context).sendPayload(endpointId, bytesPayload)
                            }
                        }, 0, 40_000)
                    }
                    ConnectionsStatusCodes.STATUS_CONNECTION_REJECTED -> {
                        Timber.e("onConnectionResult.STATUS_CONNECTION_REJECTED ")
                    }
                    ConnectionsStatusCodes.STATUS_ERROR -> {
                        Timber.e("onConnectionResult.STATUS_ERROR ")
                    }
                    else -> {
                    }
                }
            }

            override fun onDisconnected(endpointId: String) {
                Timber.e("onDisconnected... $endpointId")
            }
        }

Thanks in advance.

1

There are 1 best solutions below

0
On

When you start discovery you should pass EndpointDiscoveryCallback, Something like this:

public void startDiscovering(EndpointDiscoveryCallback endpointDiscoveryCallback, Strategy strategy){
        DiscoveryOptions discoveryOptions = new DiscoveryOptions.Builder().setStrategy(strategy).build();
        connectionsClient.startDiscovery(
                SERVICE_ID,
                endpointDiscoveryCallback,
                discoveryOptions)
                .addOnSuccessListener(
                (Void unused) -> {
                    Log.e("Discovery Start", "Discovery started successfully");
                }).addOnFailureListener(
                (Exception e) -> {
                    Log.e("Discovery Start", e.getLocalizedMessage());
                });
    }

This EndpointDiscoveryCallback will provide you with the endpoint info every time a new device is found near you. This will allow you to make a list of discovered devices.

public class MyEndpointDiscoveryCallback extends EndpointDiscoveryCallback {

    @Override
    public void onEndpointFound(@NonNull String endpointId, @NonNull DiscoveredEndpointInfo discoveredEndpointInfo) {
        // new device discovered
    }

    @Override
    public void onEndpointLost(@NonNull String endpointId) {
        // discovered device lost
    }
}