Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to detect app(or mobile) is connected to Android Auto

I searched, but could not get any answer Is there any broadcast do detect when our phone is connected to Android Auto?

I have this code but that need to be run by some event.

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;
}
like image 236
Parth Avatar asked Jan 23 '18 12:01

Parth


People also ask

Will my phone automatically connect to Android Auto?

If you're planning a long trip or your phone needs a charge, you can plug it in. Otherwise, Android Auto Wireless automatically connects your phone to your car radio when you get in your vehicle (after the initial USB cable connection).


1 Answers

Looking into the documentation on the UiModeManager, I found ACTION_ENTER_CAR_MODE, as well as ACTION_EXIT_CAR_MODE.

Using these, you can create and register a receiver in the manifest like so:

<receiver
  android:name=".CarModeReceiver"
  android:enabled="true"
  android:exported="true">
  <intent-filter>
    <action android:name="android.app.action.ENTER_CAR_MODE"/>
    <action android:name="android.app.action.EXIT_CAR_MODE"/>
  </intent-filter>
</receiver>

Then in the implementation of the receiver, you can do something like this

public class CarModeReceiver extends BroadcastReceiver {

  @Override
  public void onReceive(Context context, Intent intent) {
      String action = intent.getAction();
      if (UiModeManager.ACTION_ENTER_CAR_MODE.equals(action)) {
        Log.d("CarModeReceiver", "Car Mode");
      } else if (UiModeManager.ACTION_EXIT_CAR_MODE.equals(action)) {
        Log.d("CarModeReceiver", "Non-Car Mode");
      }
  }
}
like image 77
salminnella Avatar answered Sep 28 '22 04:09

salminnella