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 */}
 
}
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:

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