# RemoteConnection

# RemoteConnection

*** ** * ** ***

[Kotlin](https://developer.android.com/reference/kotlin/com/google/ambient/crossdevice/connections/RemoteConnection "View this page in Kotlin")\|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 fields                                                                                                                                                                                              ||
|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| `abstract @`[NonNull](https://developer.android.com/reference/androidx/annotation/NonNull)` `[ConnectionType](https://developer.android.com/reference/com/google/ambient/crossdevice/connections/ConnectionType) | [connectionType](https://developer.android.com/reference/com/google/ambient/crossdevice/connections/RemoteConnection#connectionType()) The type of the connection.                       |
| `abstract boolean`                                                                                                                                                                                               | [isClosed](https://developer.android.com/reference/com/google/ambient/crossdevice/connections/RemoteConnection#isClosed()) `true`if this connection is closed.                           |
| `abstract @`[NonNull](https://developer.android.com/reference/androidx/annotation/NonNull)` `[Participant](https://developer.android.com/reference/com/google/ambient/crossdevice/Participant)                   | [participant](https://developer.android.com/reference/com/google/ambient/crossdevice/connections/RemoteConnection#participant()) The participant this remote connection is connected to. |

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

## Public fields

### connectionType

```
abstract @NonNull ConnectionType connectionType
```

The type of the connection.  

### isClosed

```
abstract boolean isClosed
```

`true`if this connection is closed.  

### participant

```
abstract @NonNull Participant participant
```

The participant this remote connection is connected to.

## Public methods

### close

```
abstract void close(String reason)
```

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                                                                             ||
|-----------------------------------------------------------------------------|---------------------------------------------------------------------------------------|
| [String](https://developer.android.com/reference/java/lang/String)` reason` | An optional reason field telling the remote participant why the connection is closed. |

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

### closeFuture

```
abstract @NonNull ListenableFuture<Void> closeFuture(String reason)
```

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

### closeFuture

```
default @NonNull ListenableFuture<Void> closeFuture()
```

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

### registerReceiver

```
abstract void registerReceiver(@NonNull ConnectionReceiver receiver)
```

Registers the given connection receiver with this channel.  

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

### send

```
abstract @NonNull Result<Unit> send(@NonNull byte[] bytes)
```

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

### sendFuture

```
abstract @NonNull ListenableFuture<Void> sendFuture(@NonNull byte[] bytes)
```

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

### unregisterReceiver

```
abstract void unregisterReceiver(@NonNull ConnectionReceiver receiver)
```

Unregisters a given receiver from the remote device.