shelll
shelll

Reputation: 3383

How to detect if phone is connected to Android Auto

I am making an app that should change its behavior when the phone is connected to an Android Auto. It does not have any Auto capabilities and will not be marketed/submitted as an Android Auto app.

Is there a way to detect if the phone is connected to an Android Auto? I know it is possible for Auto media apps to detect their connection status via a BroadcastReceiver registered for com.google.android.gms.car.media.STATUS action. The documentation is not 100% clear, so will this reliably work also for all other non-Auto apps?

Upvotes: 3

Views: 6278

Answers (6)

Panther
Panther

Reputation: 445

Based on the other replies, here is the solution if you only want to get the current Android Auto connection state once and not listen for it:

public class AutoConnectionDetector {
private final Context context;
private static String TAG = "AutoConnectionDetector";

// columnName for provider to query on connection status
private static final String CAR_CONNECTION_STATE = "CarConnectionState";

// phone is not connected to car
private static final int CONNECTION_TYPE_NOT_CONNECTED = 0;

// phone is connected to Automotive OS
private final int CONNECTION_TYPE_NATIVE = 1;

// phone is connected to Android Auto
private final int CONNECTION_TYPE_PROJECTION = 2;

private final String CAR_CONNECTION_AUTHORITY = "androidx.car.app.connection";

private final Uri PROJECTION_HOST_URI = new Uri.Builder().scheme("content").authority(CAR_CONNECTION_AUTHORITY).build();


public AutoConnectionDetector(Context context) {
    this.context = context;
}

public boolean queryForState() {
    ContentResolver resolver = context.getContentResolver();
    Cursor response = resolver.query(PROJECTION_HOST_URI, new String[]{CAR_CONNECTION_STATE}, null, null, null);

    if (response == null || response.getCount() < 1) {
        //Log.w(TAG, "Null response from content provider when checking connection to the car, treating as disconnected");
        return false;
    }

    if (response.moveToFirst()) {
        int carConnectionTypeColumn = response.getColumnIndex(CAR_CONNECTION_STATE);
        if (carConnectionTypeColumn < 0) {
            //Log.w(TAG, "Connection to car response is missing the connection type, treating as disconnected");
            response.close();
            return false;
        } else {
            int connectionState = response.getInt(carConnectionTypeColumn);
            if (connectionState == CONNECTION_TYPE_NOT_CONNECTED) {
                //Log.i(TAG, "Android Auto disconnected");
                response.close();
                return false;
            } else {
                Log.i(TAG, "Android Auto connected");
                response.close();
                return true;
            }
        }
    } else {
        //Log.w(TAG, "Connection to car response is empty, treating as disconnected");
        response.close();
        return false;
    }
}

}

Upvotes: 0

Sinan Ceylan
Sinan Ceylan

Reputation: 1073

@G.Zxuan 's Kotlin solution works great. I've transferred it into Java and added a listener interface. Tested on Android 12

public class AutoConnectionDetector {
    private final Context context;
    private static String TAG = "AutoConnectionDetector";
    private final CarConnectionBroadcastReceiver carConnectionReceiver = new CarConnectionBroadcastReceiver();
    private final CarConnectionQueryHandler carConnectionQueryHandler;
    // columnName for provider to query on connection status
    private static final String CAR_CONNECTION_STATE = "CarConnectionState";

    // auto app on your phone will send broadcast with this action when connection state changes
    private final String ACTION_CAR_CONNECTION_UPDATED = "androidx.car.app.connection.action.CAR_CONNECTION_UPDATED";

    // phone is not connected to car
    private static final int CONNECTION_TYPE_NOT_CONNECTED = 0;

    // phone is connected to Automotive OS
    private final int CONNECTION_TYPE_NATIVE = 1;

    // phone is connected to Android Auto
    private final int CONNECTION_TYPE_PROJECTION = 2;

    private final int QUERY_TOKEN = 42;

    private final String CAR_CONNECTION_AUTHORITY = "androidx.car.app.connection";

    private final Uri PROJECTION_HOST_URI = new Uri.Builder().scheme("content").authority(CAR_CONNECTION_AUTHORITY).build();

    public interface OnCarConnectionStateListener {
        void onCarConnected();

        void onCarDisconnected();
    }

    private static OnCarConnectionStateListener listener;

    public void setListener(OnCarConnectionStateListener listener) {
        AutoConnectionDetector.listener = listener;
    }

    public AutoConnectionDetector(Context context) {
        this.context = context;
        carConnectionQueryHandler = new CarConnectionQueryHandler(context.getContentResolver());
    }

    public void registerCarConnectionReceiver() {
        context.registerReceiver(carConnectionReceiver, new IntentFilter(ACTION_CAR_CONNECTION_UPDATED));
        queryForState();
        Log.i(TAG, "registerCarConnectionReceiver: ");
    }

    public void unRegisterCarConnectionReceiver() {
        context.unregisterReceiver(carConnectionReceiver);
        Log.i(TAG, "unRegisterCarConnectionReceiver: ");
    }

    private void queryForState() {
        String[] projection = {CAR_CONNECTION_STATE};
        carConnectionQueryHandler.startQuery(
                QUERY_TOKEN,
                null,
                PROJECTION_HOST_URI,
                projection,
                null,
                null,
                null
        );
    }

    private static void notifyCarConnected() {
        listener.onCarConnected();
    }

    private static void notifyCarDisconnected() {
        listener.onCarDisconnected();
    }

    class CarConnectionBroadcastReceiver extends BroadcastReceiver {
        // query for connection state every time the receiver receives the broadcast
        @Override
        public void onReceive(Context context, Intent intent) {
            queryForState();
        }
    }

    private static class CarConnectionQueryHandler extends AsyncQueryHandler {
        public CarConnectionQueryHandler(ContentResolver contentResolver) {
            super(contentResolver);
        }

        @Override
        protected void onQueryComplete(int token, Object cookie, Cursor response) {
            if (response == null) {
                Log.w(TAG, "Null response from content provider when checking connection to the car, treating as disconnected");
                notifyCarDisconnected();
                return;
            }
            int carConnectionTypeColumn = response.getColumnIndex(CAR_CONNECTION_STATE);
            if (carConnectionTypeColumn < 0) {
                Log.w(TAG, "Connection to car response is missing the connection type, treating as disconnected");
                notifyCarDisconnected();
                return;
            }
            if (!response.moveToNext()) {
                Log.w(TAG, "Connection to car response is empty, treating as disconnected");
                notifyCarDisconnected();
                return;
            }
            int connectionState = response.getInt(carConnectionTypeColumn);

            if (connectionState == CONNECTION_TYPE_NOT_CONNECTED) {
                Log.i(TAG, "Android Auto disconnected");
                notifyCarDisconnected();
            } else {
                Log.i(TAG, "Android Auto connected");
                Log.i(TAG, "onQueryComplete: " + connectionState);
                notifyCarConnected();
            }
        }
    }
}

Upvotes: 1

arekolek
arekolek

Reputation: 9621

See https://developer.android.com/training/cars/apps#car-connection

val Context.isAndroidAutoConnected: LiveData<Boolean>
    get() = CarConnection(this).type
        .map { it == CarConnection.CONNECTION_TYPE_PROJECTION }

app/build.gradle:

dependencies {
    implementation 'androidx.car.app:app:1.1.0'
    implementation "androidx.lifecycle:lifecycle-livedata-ktx:2.5.1"
}

Upvotes: 1

Pavel Braynin
Pavel Braynin

Reputation: 1

Recomendation G.Zxuan work perfect, but we must add in dependecies "androidx.car.app:app-projected:1.1.0" in build.gradle

Upvotes: 0

G.Zxuan
G.Zxuan

Reputation: 86

Configuration.UI_MODE_TYPE_CAR is not working on Anroid 12. You can use CarConnection API in the androidx.car.app:app library. But that is too heavy to import entire library only for car connections if you don't need other features.

So I write a piece of code base on the CarConnection to detect Android Auto connection, as below:

class AutoConnectionDetector(val context: Context) {

    companion object {
        const val TAG = "AutoConnectionDetector"

        // columnName for provider to query on connection status
        const val CAR_CONNECTION_STATE = "CarConnectionState"

        // auto app on your phone will send broadcast with this action when connection state changes
        const val ACTION_CAR_CONNECTION_UPDATED = "androidx.car.app.connection.action.CAR_CONNECTION_UPDATED"

        // phone is not connected to car
        const val CONNECTION_TYPE_NOT_CONNECTED = 0

        // phone is connected to Automotive OS
        const val CONNECTION_TYPE_NATIVE = 1

        // phone is connected to Android Auto
        const val CONNECTION_TYPE_PROJECTION = 2

        private const val QUERY_TOKEN = 42

        private const val CAR_CONNECTION_AUTHORITY = "androidx.car.app.connection"

        private val PROJECTION_HOST_URI = Uri.Builder().scheme("content").authority(CAR_CONNECTION_AUTHORITY).build()
    }

    private val carConnectionReceiver = CarConnectionBroadcastReceiver()
    private val carConnectionQueryHandler = CarConnectionQueryHandler(context.contentResolver)

    fun registerCarConnectionReceiver() {
        context.registerReceiver(carConnectionReceiver, IntentFilter(ACTION_CAR_CONNECTION_UPDATED))
        queryForState()
    }

    fun unRegisterCarConnectionReceiver() {
        context.unregisterReceiver(carConnectionReceiver)
    }

    private fun queryForState() {
        carConnectionQueryHandler.startQuery(
            QUERY_TOKEN,
            null,
            PROJECTION_HOST_URI,
            arrayOf(CAR_CONNECTION_STATE),
            null,
            null,
            null
        )
    }

    inner class CarConnectionBroadcastReceiver : BroadcastReceiver() {
      // query for connection state every time the receiver receives the broadcast
        override fun onReceive(context: Context?, intent: Intent?) {
            queryForState()
        }
    }

    internal class CarConnectionQueryHandler(resolver: ContentResolver?) : AsyncQueryHandler(resolver) {
        // notify new queryed connection status when query complete
        override fun onQueryComplete(token: Int, cookie: Any?, response: Cursor?) {
            if (response == null) {
                Log.w(TAG, "Null response from content provider when checking connection to the car, treating as disconnected")
                notifyCarDisconnected()
                return
            }
            val carConnectionTypeColumn = response.getColumnIndex(CAR_CONNECTION_STATE)
            if (carConnectionTypeColumn < 0) {
                Log.w(TAG, "Connection to car response is missing the connection type, treating as disconnected")
                notifyCarDisconnected()
                return
            }
            if (!response.moveToNext()) {
                Log.w(TAG, "Connection to car response is empty, treating as disconnected")
                notifyCarDisconnected()
                return
            }
            val connectionState = response.getInt(carConnectionTypeColumn)
            if (connectionState == CONNECTION_TYPE_NOT_CONNECTED) {
                Log.i(TAG, "Android Auto disconnected")
                notifyCarDisconnected()
            } else {
                Log.i(TAG, "Android Auto connected")
                notifyCarConnected()
            }
        }
    }
}

This solution works on android 6~12. If you need to detect car connection status on android 5, use the Configuration.UI_MODE_TYPE_CAR solution.

Upvotes: 4

Pierre-Olivier Dybman
Pierre-Olivier Dybman

Reputation: 973

Edit: with Android 12, the solution doesn't work anymore and instead, it's better to use CarConnection API documented here

I know this is an old thread but since it comes first in Google, here is the answer from another question

public static boolean isCarUiMode(Context c) {
UiModeManager uiModeManager = (UiModeManager) c.getSystemService(Context.UI_MODE_SERVICE);
if (uiModeManager.getCurrentModeType() == Configuration.UI_MODE_TYPE_CAR) {
    LogHelper.d(TAG, "Running in Car mode");
    return true;
} else {
    LogHelper.d(TAG, "Running on a non-Car mode");
    return false;
}

}

Upvotes: 8

Related Questions