API Secure Connection

Après avoir découvert un appareil distant, la fonction handleIntent est appelée. il est temps de commencer à transmettre des données entre les clients. Cette section aborde les quatre étapes essentielles pour maintenir une connexion sécurisée:

  • Ouverture d'une connexion
  • Acceptation de la connexion...
  • Envoyer et recevoir des données
  • Fermer la connexion

Ouvrir une connexion

Pour ouvrir une connexion afin de recevoir des données d'un appareil distant, utilisez le l'objet du participant précédemment reçu et spécifiez CHANNEL_NAME:

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

Accepter, envoyer/recevoir et fermer une connexion

Les connexions sécurisées nécessitent que l'appareil destinataire accepte les connexions entrantes avant de recevoir les données. Pour accepter une connexion à distance, utilisez le code suivant : snippet:

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