Secure Connection API

找到遠端裝置後,系統會呼叫 handleIntent 函式, 即可開始在用戶端之間傳遞資料。本節將 保持安全連線的四個重要步驟

  • 開啟連結
  • 接受連線
  • 傳送和接收資料
  • 關閉連線

開啟連結

如要建立連線以接收遠端裝置的資料,請使用 先前接收的參與者物件,並指定 CHANNEL_NAME

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

接受、傳送/接收及關閉連線

安全連線會要求接收裝置接受傳入連線 才能接收資料如要接受遠端連線,請使用下列方式 程式碼片段:

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