Connection API

To determine if your app is running on Android Auto or Android Automotive OS, use the CarConnection API to retrieve connection information at runtime. For example:

  1. In your car app's Session, initialize a CarConnection and subscribe to LiveData updates:

CarConnection(carContext).type.observe(this, ::onConnectionStateUpdated)

  1. In the observer, react to changes in the connection state:

fun onConnectionStateUpdated(connectionState: Int) {
    val message = when (connectionState) {
        CarConnection.CONNECTION_TYPE_NOT_CONNECTED -> "Not connected to a head unit"
        CarConnection.CONNECTION_TYPE_NATIVE -> "Connected to Android Automotive OS"
        CarConnection.CONNECTION_TYPE_PROJECTION -> "Connected to Android Auto"
        else -> "Unknown car connection type"
    }
    CarToast.makeText(carContext, message, CarToast.LENGTH_SHORT).show()
}