# RemoteConnection

# RemoteConnection

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/com/google/ambient/crossdevice/connections/RemoteConnection "View this page in Java")

<br />

```
@RequiresApi(value = 26) interface RemoteConnection
```

<br />

*** ** * ** ***

A connection to a remote device, used for sending and receiving payloads. Multiple connections can exist between the same pair of devices, and they will share the underlying physical connection. Developers can use this connection to send and receive payloads between devices.

Implementations are not required to be thread safe.

## Summary

|                                                                                                                                                                                                                                                                                                         ### Public functions                                                                                                                                                                                                                                                                                                          ||
|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `suspend `[Unit](https://developer.android.com/reference/kotlin/kotlin/Unit)                                                                                                                             | [close](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#close(kotlin.String))`(reason: `[String](https://developer.android.com/reference/kotlin/kotlin/String)`?)` Closes the connection, cleaning up any resources allocated for it.                                                                                                                            |
| [ListenableFuture](https://developer.android.com/reference/kotlin/com/google/common/util/concurrent/ListenableFuture)`<`[Void](https://developer.android.com/reference/kotlin/java/lang/Void)`?>`        | [closeFuture](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#closeFuture(kotlin.String))`(reason: `[String](https://developer.android.com/reference/kotlin/kotlin/String)`?)` Java-compatible version of[close](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#close(kotlin.String)).               |
| `open `[ListenableFuture](https://developer.android.com/reference/kotlin/com/google/common/util/concurrent/ListenableFuture)`<`[Void](https://developer.android.com/reference/kotlin/java/lang/Void)`?>` | [closeFuture](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#closeFuture())`()` Java-compatible version of[close](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#close(kotlin.String)).                                                                                                             |
| [Unit](https://developer.android.com/reference/kotlin/kotlin/Unit)                                                                                                                                       | [registerReceiver](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#registerReceiver(com.google.ambient.crossdevice.connections.ConnectionReceiver))`(receiver: `[ConnectionReceiver](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/ConnectionReceiver)`)` Registers the given connection receiver with this channel. |
| `suspend `[Result](https://developer.android.com/reference/kotlin/kotlin/Result)`<`[Unit](https://developer.android.com/reference/kotlin/kotlin/Unit)`>`                                                 | [send](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#send(kotlin.ByteArray))`(bytes: `[ByteArray](https://developer.android.com/reference/kotlin/kotlin/ByteArray)`)` Enqueues bytes to be sent across the channel.                                                                                                                                            |
| [ListenableFuture](https://developer.android.com/reference/kotlin/com/google/common/util/concurrent/ListenableFuture)`<`[Void](https://developer.android.com/reference/kotlin/java/lang/Void)`?>`        | [sendFuture](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#sendFuture(kotlin.ByteArray))`(bytes: `[ByteArray](https://developer.android.com/reference/kotlin/kotlin/ByteArray)`)` Java-compatible version of[send](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#send(kotlin.ByteArray)).         |
| [Unit](https://developer.android.com/reference/kotlin/kotlin/Unit)                                                                                                                                       | [unregisterReceiver](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#unregisterReceiver(com.google.ambient.crossdevice.connections.ConnectionReceiver))`(receiver: `[ConnectionReceiver](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/ConnectionReceiver)`)` Unregisters a given receiver from the remote device.   |

|                                                                                                                                                    ### Public properties                                                                                                                                                    ||
|----------------------------------------------------------------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [ConnectionType](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/ConnectionType) | [connectionType](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#connectionType()) The type of the connection.                       |
| [Boolean](https://developer.android.com/reference/kotlin/kotlin/Boolean)                                                   | [isClosed](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#isClosed()) `true`if this connection is closed.                           |
| [Participant](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/Participant)                   | [participant](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#participant()) The participant this remote connection is connected to. |

## Public functions

### close

```
suspend fun close(reason: String? = null): Unit
```

Closes the connection, cleaning up any resources allocated for it.

If called when the connection is already closed, this method will be a no-op.  

|                                                                                    Parameters                                                                                     ||
|--------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------|
| `reason: `[String](https://developer.android.com/reference/kotlin/kotlin/String)`? = null` | An optional reason field telling the remote participant why the connection is closed. |

|                                                                                                                       See also                                                                                                                       ||
|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---|
| [onConnectionClosed](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/ConnectionReceiver#onConnectionClosed(com.google.ambient.crossdevice.connections.RemoteConnection,kotlin.Throwable,kotlin.String)) |   |

### closeFuture

```
fun closeFuture(reason: String?): ListenableFuture<Void?>
```

Java-compatible version of[close](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#close(kotlin.String)).  

### closeFuture

```
open fun closeFuture(): ListenableFuture<Void?>
```

Java-compatible version of[close](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#close(kotlin.String)).  

### registerReceiver

```
fun registerReceiver(receiver: ConnectionReceiver): Unit
```

Registers the given connection receiver with this channel.  

|                                                                                                                                       Throws                                                                                                                                       ||
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------|
| `com.google.ambient.crossdevice.connections.ConnectionsException: `[com.google.ambient.crossdevice.connections.ConnectionsException](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/ConnectionsException) | if the connection is closed. |

### send

```
suspend fun send(bytes: ByteArray): Result<Unit>
```

Enqueues bytes to be sent across the channel.

Bytes are sent in the order they are enqueued, and this function completes when the payload is successfully enqueued. Note that completion of this method does not mean the remote device has received or processed the payload message.  

|                                                                                                                                                                                                                                               Returns                                                                                                                                                                                                                                               ||
|------------------------------------------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Result](https://developer.android.com/reference/kotlin/kotlin/Result)`<`[Unit](https://developer.android.com/reference/kotlin/kotlin/Unit)`>` | [Result.success](https://developer.android.com/reference/kotlin/kotlin/Result.Companion#success(kotlin.Any))if the message was sent successfully, or a failed result with[ConnectionsException](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/ConnectionsException)if the connection is already closed. |

### sendFuture

```
fun sendFuture(bytes: ByteArray): ListenableFuture<Void?>
```

Java-compatible version of[send](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection#send(kotlin.ByteArray)).  

### unregisterReceiver

```
fun unregisterReceiver(receiver: ConnectionReceiver): Unit
```

Unregisters a given receiver from the remote device.

## Public properties

### connectionType

```
val connectionType: ConnectionType
```

The type of the connection.  

### isClosed

```
val isClosed: Boolean
```

`true`if this connection is closed.  

### participant

```
val participant: Participant
```

The participant this remote connection is connected to.