Secure connection API
使用集合让一切井井有条
根据您的偏好保存内容并对其进行分类。
发现远程设备后,系统会调用 handleIntent
函数,并且
是时候开始在客户端之间传递数据了。本部分介绍了
保持安全连接的四个必要步骤:
打开连接
要打开连接以从远程设备接收数据,请使用
之前收到的参与者对象,并指定 CHANNEL_NAME
:
Kotlin
participant
.openConnection(CHANNEL_HELLO)
.onFailure { /* handle failure */}
.getOrNull()
?.let { connection ->
connection.send("Hello, world".toByteArray(UTF_8)).onFailure { /* handle failure */}
}
Java
public void openConnection(Participant participant) {
Futures.addCallback(
participant.openConnectionFuture(CHANNEL_HELLO),
new FutureCallback<RemoteConnection>() {
@Override
public void onSuccess(RemoteConnection remoteConnection) {
// use remoteConnection object to pass data, e.g.:
sendDataToRemoteConnection(remoteConnection);
}
@Override
public void onFailure(Throwable t) {
// handle error opening a remote connection
}
},
mainExecutor);
}
private void sendDataToRemoteConnection(RemoteConnection remoteConnection) {
Futures.addCallback(
remoteConnection.sendFuture("Hello, world".getBytes()),
new FutureCallback<Void>() {
@Override
public void onSuccess(Void result) {
// data sent successfully
}
@Override
public void onFailure(Throwable t) {
// handle error
}
},
mainExecutor);
}
接受、发送/接收以及关闭连接
安全连接要求接收方设备接受传入连接
然后再接收数据如需接受远程连接,请使用以下
snippet:
Kotlin
suspend fun acceptIncomingConnection(participant: Participant) {
val connection = participant.acceptConnection(CHANNEL_HELLO).getOrThrow()
connection.registerReceiver(
object : ConnectionReceiver {
override fun onMessageReceived(remoteConnection: RemoteConnection, payload: ByteArray) {
displayMessage(payload.toString(UTF_8))
}
override fun onConnectionClosed(
remoteConnection: RemoteConnection,
error: Throwable?,
reason: String?
) {
// handle connection closure
}
}
)
}
Java
public void acceptIncomingConnection(Participant participant) {
// Registers call back to accept incoming remote connection
Futures.addCallback(
participant.acceptConnectionFuture(CHANNEL_HELLO),
new FutureCallback<>() {
@Override
public void onSuccess(RemoteConnection result) {
receiveData(result);
}
@Override
public void onFailure(Throwable t) {
// handle connection error
}
},
mainExecutor);
}
private void receiveData(RemoteConnection remoteConnection) {
remoteConnection.registerReceiver(
new ConnectionReceiver() {
@Override
public void onMessageReceived(RemoteConnection remoteConnection, byte[] payload) {
displayMessage(new String(payload, UTF_8));
}
@Override
public void onConnectionClosed(
RemoteConnection remoteConnection,
@Nullable Throwable error,
@Nullable String reason) {
// handle connection closure
}
});
}
本页面上的内容和代码示例受内容许可部分所述许可的限制。Java 和 OpenJDK 是 Oracle 和/或其关联公司的注册商标。
最后更新时间 (UTC):2025-07-27。
[[["易于理解","easyToUnderstand","thumb-up"],["解决了我的问题","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["没有我需要的信息","missingTheInformationINeed","thumb-down"],["太复杂/步骤太多","tooComplicatedTooManySteps","thumb-down"],["内容需要更新","outOfDate","thumb-down"],["翻译问题","translationIssue","thumb-down"],["示例/代码问题","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["最后更新时间 (UTC):2025-07-27。"],[],[],null,["# Secure connection API\n\nAfter discovering a remote device, the `handleIntent` function is called, and\nit's time to start passing data between clients. This section covers the\nfour essential steps of maintaining a secure connection:\n\n- Opening a connection\n- Accepting the connection\n- Sending and receiving data\n- Closing the connection\n\nOpen a connection\n-----------------\n\nTo open a connection to receive data from a remote device, use the\npreviously received participant object and specify `CHANNEL_NAME`: \n\n### Kotlin\n\n```kotlin\nparticipant\n .openConnection(CHANNEL_HELLO)\n .onFailure { /* handle failure */}\n .getOrNull()\n ?.let { connection -\u003e\n connection.send(\"Hello, world\".toByteArray(UTF_8)).onFailure { /* handle failure */}\n }\n```\n\n### Java\n\n```java\npublic void openConnection(Participant participant) {\n Futures.addCallback(\n participant.openConnectionFuture(CHANNEL_HELLO),\n new FutureCallback\u003cRemoteConnection\u003e() {\n @Override\n public void onSuccess(RemoteConnection remoteConnection) {\n // use remoteConnection object to pass data, e.g.:\n sendDataToRemoteConnection(remoteConnection);\n }\n\n @Override\n public void onFailure(Throwable t) {\n // handle error opening a remote connection\n }\n },\n mainExecutor);\n}\n\nprivate void sendDataToRemoteConnection(RemoteConnection remoteConnection) {\n Futures.addCallback(\n remoteConnection.sendFuture(\"Hello, world\".getBytes()),\n new FutureCallback\u003cVoid\u003e() {\n @Override\n public void onSuccess(Void result) {\n // data sent successfully\n }\n\n @Override\n public void onFailure(Throwable t) {\n // handle error\n }\n },\n mainExecutor);\n}\n```\n\nAccept, send/receive, and close a connection\n--------------------------------------------\n\nSecure connections require the receiving device to accept incoming connections\nbefore receiving the data. To accept a remote connection, use the following\nsnippet: \n\n### Kotlin\n\n```kotlin\nsuspend fun acceptIncomingConnection(participant: Participant) {\n val connection = participant.acceptConnection(CHANNEL_HELLO).getOrThrow()\n connection.registerReceiver(\n object : ConnectionReceiver {\n override fun onMessageReceived(remoteConnection: RemoteConnection, payload: ByteArray) {\n displayMessage(payload.toString(UTF_8))\n }\n\n override fun onConnectionClosed(\n remoteConnection: RemoteConnection,\n error: Throwable?,\n reason: String?\n ) {\n // handle connection closure\n }\n }\n )\n}\n```\n\n### Java\n\n```java\npublic void acceptIncomingConnection(Participant participant) {\n // Registers call back to accept incoming remote connection\n Futures.addCallback(\n participant.acceptConnectionFuture(CHANNEL_HELLO),\n new FutureCallback\u003c\u003e() {\n @Override\n public void onSuccess(RemoteConnection result) {\n receiveData(result);\n }\n\n @Override\n public void onFailure(Throwable t) {\n // handle connection error\n }\n },\n mainExecutor);\n}\n\nprivate void receiveData(RemoteConnection remoteConnection) {\n remoteConnection.registerReceiver(\n new ConnectionReceiver() {\n @Override\n public void onMessageReceived(RemoteConnection remoteConnection, byte[] payload) {\n displayMessage(new String(payload, UTF_8));\n }\n\n @Override\n public void onConnectionClosed(\n RemoteConnection remoteConnection,\n @Nullable Throwable error,\n @Nullable String reason) {\n // handle connection closure\n }\n });\n}\n```"]]