启动连接
发现附近的设备后,发现器便可发起连接。以下示例在发现设备后立即请求连接。
private final EndpointDiscoveryCallback endpointDiscoveryCallback = new EndpointDiscoveryCallback() { @Override public void onEndpointFound(String endpointId, DiscoveredEndpointInfo info) { // An endpoint was found. We request a connection to it. Nearby.getConnectionsClient(context) .requestConnection(getLocalUserName(), endpointId, connectionLifecycleCallback) .addOnSuccessListener( (Void unused) -> { // We successfully requested a connection. Now both sides // must accept before the connection is established. }) .addOnFailureListener( (Exception e) -> { // Nearby Connections failed to request the connection. }); } @Override public void onEndpointLost(String endpointId) { // A previously discovered endpoint has gone away. } };
根据您的用例,您可能需要改为向用户显示已发现设备的列表,让他们可以选择要连接的设备。
接受或拒绝连接
发现者请求与广告客户建立连接后,双方都将通过 ConnectionLifecycleCallback
回调的 onConnectionInitiated()
方法将连接启动流程通知到用户。请注意,此回调在广告客户上传递到 startAdvertising()
,在发现器上传入 requestConnection()
,但从现在开始,API 是对称的。
现在,双方必须分别选择通过调用 acceptConnection()
或 rejectConnection()
来接受或拒绝连接。只有在双方都接受后,连接才会完全确定。如果一个或两个拒绝,连接将被舍弃。无论采用哪种方式,结果都会传递到 onConnectionResult()
。
以下代码段展示了如何实现此回调:
private final ConnectionLifecycleCallback connectionLifecycleCallback = new ConnectionLifecycleCallback() { @Override public void onConnectionInitiated(String endpointId, ConnectionInfo connectionInfo) { // Automatically accept the connection on both sides. Nearby.getConnectionsClient(context).acceptConnection(endpointId, payloadCallback); } @Override public void onConnectionResult(String endpointId, ConnectionResolution result) { switch (result.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(String endpointId) { // We've been disconnected from this endpoint. No more data can be // sent or received. } };
同样,此示例显示两端都自动接受了连接,但根据您的用例,您可能希望以某种方式向用户显示这种选择。
对连接进行身份验证
请求连接时,您的应用可以使用提供给 onConnectionInitiated()
的身份验证令牌对连接进行身份验证。通过这种方式,用户可以确认自己正在连接到目标设备。这两台设备都会获得相同的令牌,这是一个短随机字符串;您可以自行决定如何验证。这通常涉及在两台设备上显示令牌,并要求用户手动比较和确认,类似于蓝牙配对对话框。
此示例代码演示了在 AlertDialog
中向用户显示身份验证令牌的身份验证方式:
@Override public void onConnectionInitiated(String endpointId, ConnectionInfo info) { new AlertDialog.Builder(context) .setTitle("Accept connection to " + info.getEndpointName()) .setMessage("Confirm the code matches on both devices: " + info.getAuthenticationDigits()) .setPositiveButton( "Accept", (DialogInterface dialog, int which) -> // The user confirmed, so we can accept the connection. Nearby.getConnectionsClient(context) .acceptConnection(endpointId, payloadCallback)) .setNegativeButton( android.R.string.cancel, (DialogInterface dialog, int which) -> // The user canceled, so we should reject the connection. Nearby.getConnectionsClient(context).rejectConnection(endpointId)) .setIcon(android.R.drawable.ic_dialog_alert) .show(); }