# CarConnection

Artifact: [androidx.car.app:app](https://developer.android.com/jetpack/androidx/releases/car-app) [View Source](https://cs.android.com/search?q=file:androidx/car/app/connection/CarConnection.java+class:androidx.car.app.connection.CarConnection) Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.1.0)

*** ** * ** ***


```
class CarConnection
```

<br />

*** ** * ** ***

A class that allows retrieval of information about connection to a car head unit.

## Summary

| ### Constants |
|---|---|
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | `https://developer.android.com/reference/kotlin/androidx/car/app/connection/CarConnection#ACTION_CAR_CONNECTION_UPDATED() = "androidx.car.app.connection.action.CAR_CONNECTION_UPDATED"` Broadcast action that notifies that the car connection has changed and needs to be updated. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-string/index.html!` | `https://developer.android.com/reference/kotlin/androidx/car/app/connection/CarConnection#CAR_CONNECTION_STATE() = "CarConnectionState"` Defines current car connection state. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/car/app/connection/CarConnection#CONNECTION_TYPE_NATIVE() = 1` Natively running on a head unit (Android Automotive OS). |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/car/app/connection/CarConnection#CONNECTION_TYPE_NOT_CONNECTED() = 0` Not connected to any car head unit. |
| `const https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html` | `https://developer.android.com/reference/kotlin/androidx/car/app/connection/CarConnection#CONNECTION_TYPE_PROJECTION() = 2` Connected to a car head unit by projecting to it. |

| ### Public constructors |
|---|
| `@https://developer.android.com/reference/kotlin/androidx/annotation/MainThread https://developer.android.com/reference/kotlin/androidx/car/app/connection/CarConnection#CarConnection(android.content.Context)(context: https://developer.android.com/reference/android/content/Context.html)` Constructs a `https://developer.android.com/reference/kotlin/androidx/car/app/connection/CarConnection` that can be used to get connection information. |

| ### Public functions |
|---|---|
| `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData<https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-int/index.html!>` | `https://developer.android.com/reference/kotlin/androidx/car/app/connection/CarConnection#getType()()` Returns a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` that can be observed to get current connection type. |

## Constants

### ACTION_CAR_CONNECTION_UPDATED

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.1.0)

```
const val ACTION_CAR_CONNECTION_UPDATED = "androidx.car.app.connection.action.CAR_CONNECTION_UPDATED": String!
```

Broadcast action that notifies that the car connection has changed and needs to be updated.

### CAR_CONNECTION_STATE

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.1.0)

```
const val CAR_CONNECTION_STATE = "CarConnectionState": String!
```

Defines current car connection state.

This is used for communication with the car host's content provider on queries for connection type.

### CONNECTION_TYPE_NATIVE

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.1.0)

```
const val CONNECTION_TYPE_NATIVE = 1: Int
```

Natively running on a head unit (Android Automotive OS).

### CONNECTION_TYPE_NOT_CONNECTED

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.1.0)

```
const val CONNECTION_TYPE_NOT_CONNECTED = 0: Int
```

Not connected to any car head unit.

### CONNECTION_TYPE_PROJECTION

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.1.0)

```
const val CONNECTION_TYPE_PROJECTION = 2: Int
```

Connected to a car head unit by projecting to it.

## Public constructors

### CarConnection

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.1.0)

```
@MainThread
CarConnection(context: Context)
```

Constructs a `https://developer.android.com/reference/kotlin/androidx/car/app/connection/CarConnection` that can be used to get connection information.

| Throws |
|---|---|
| `https://developer.android.com/reference/java/lang/NullPointerException.html` | if `context` is `null` |

## Public functions

### getType

Added in [1.1.0](https://developer.android.com/jetpack/androidx/releases/car-app#1.1.0)

```
fun getType(): LiveData<Int!>
```

Returns a `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` that can be observed to get current connection type.

The recommended pattern is to observe the `https://developer.android.com/reference/kotlin/androidx/lifecycle/LiveData` with the activity's lifecycle in order to get updates on the state change throughout the activity's lifetime.

Connection types are:

1. `https://developer.android.com/reference/kotlin/androidx/car/app/connection/CarConnection#CONNECTION_TYPE_NOT_CONNECTED()`
2. `https://developer.android.com/reference/kotlin/androidx/car/app/connection/CarConnection#CONNECTION_TYPE_NATIVE()`
3. `https://developer.android.com/reference/kotlin/androidx/car/app/connection/CarConnection#CONNECTION_TYPE_PROJECTION()`