API Connection

Pour déterminer si votre application s'exécute sur Android Auto ou Android Automotive OS, utilisez l'API CarConnection pour récupérer les informations de connexion au moment de l'exécution. Exemple :

  1. Dans la Session de votre application pour voiture, initialisez une CarConnection et abonnez-vous aux mises à jour LiveData :

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

  1. Dans l'observateur, réagissez aux changements d'état de la connexion :

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