Secure connection API

リモート デバイスが検出されると handleIntent 関数が呼び出され、クライアント間でデータの受け渡しを開始します。このセクションでは、安全な接続を維持するための 4 つの重要なステップについて説明します。

  • 接続を開く
  • 接続を受け入れる
  • データの送受信
  • 接続の終了

接続を開く

リモート デバイスからデータを受信する接続を開くには、以前に受信した参加者オブジェクトを使用して 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);
}

接続の受け入れ、送受信、終了

セキュア接続では、データを受信する前に受信側デバイスが受信接続を受け入れる必要があります。リモート接続を受け入れるには、次のスニペットを使用します。

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
        }
      });
}