API Koneksi

Untuk menentukan apakah aplikasi Anda berjalan di Android Auto atau Android Automotive OS, gunakan CarConnection API untuk mengambil informasi koneksi saat runtime. Contoh:

  1. Di Session aplikasi mobil Anda, lakukan inisialisasi CarConnection dan berlangganan update LiveData:

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

  1. Di observer, bereaksi terhadap perubahan status koneksi:

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