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:
- 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.
- 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.
When you start discovery you should pass EndpointDiscoveryCallback, Something like this:
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.