واجهة برمجة تطبيقات الاتصالات الآمنة
تنظيم صفحاتك في مجموعات
يمكنك حفظ المحتوى وتصنيفه حسب إعداداتك المفضّلة.
بعد رصد جهاز بعيد، يتم استدعاء الوظيفة 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 و/أو الشركات التابعة لها.
تاريخ التعديل الأخير: 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"]],["تاريخ التعديل الأخير: 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```"]]