Skip to content

Instantly share code, notes, and snippets.

@Suchiq
Last active November 9, 2019 18:15
Show Gist options
  • Save Suchiq/789d8abc79f34a4f574b46e8623d5b0c to your computer and use it in GitHub Desktop.
Save Suchiq/789d8abc79f34a4f574b46e8623d5b0c to your computer and use it in GitHub Desktop.
Code for Device B (Discoverer)
private void startDiscovery() {
DiscoveryOptions discoveryOptions = new DiscoveryOptions.Builder().setStrategy(STRATEGY).build();
Nearby.getConnectionsClient(BaseApplication.getInstance()
.getActivity()).
startDiscovery(SERVICE_ID, new EndpointDiscoveryCallback() {
@Override
public void onEndpointFound(@NonNull String endpointId, @NonNull DiscoveredEndpointInfo discoveredEndpointInfo) {
Nearby.getConnectionsClient(BaseApplication.getInstance()
.getActivity()).
requestConnection("Device B", endpointId, new ConnectionLifecycleCallback() {
@Override
public void onConnectionInitiated(@NonNull String endpointId, @NonNull ConnectionInfo connectionInfo) {
Nearby.getConnectionsClient(BaseApplication.getInstance().getActivity()).acceptConnection(endpointId, mPayloadCallback);
}
@Override
public void onConnectionResult(@NonNull String s, @NonNull ConnectionResolution connectionResolution) {
switch (connectionResolution.getStatus().getStatusCode()) {
case ConnectionsStatusCodes.STATUS_OK:
// We're connected! Can now start sending and receiving data.
break;
case ConnectionsStatusCodes.STATUS_CONNECTION_REJECTED:
// The connection was rejected by one or both sides.
break;
case ConnectionsStatusCodes.STATUS_ERROR:
// The connection broke before it was able to be accepted.
break;
default:
// Unknown status code
}
}
@Override
public void onDisconnected(@NonNull String s) {
}
});
}
@Override
public void onEndpointLost(@NonNull String s) {
// disconnected
}
}, discoveryOptions);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment