How to use Android Automotive on a 10+ years old Saab 9–5, Part 2/2

Suru Dissanaike
HiMinds
Published in
7 min readApr 15, 2019

A cutting edge infotainment system

This is the second part of us trying to retrofit Android Automotive into my 12-year-old Saab 9–5. In the previous article we asked ourself:

  • What would it take to give my Saab a cutting edge infotainment system? (BTW you can find the article here)

This article was written by my colleague Prakash (with an insignificant contribution from me), all credit goes to him!

Saab 9–5 from 2007 with roof-mounted cargo box (for transporting skis)

A quick recap and next step in our journey

In the previous article, we explore how to retrofit Android Automotive into a 10+ years old Saab. We have learned what Android Automotive is at a high level and looked at an ODB-II dongle as a way to extract information from a car. We looked at a bit of Python code, and we prepared our laptop for building Android Automotive image. In this article, we will look at the inner workings of Android Automotive. We are going to explore the Car API, CarService, VehicleNetworkService and Vehicle HAL by building an app that utilises the RPM value we extracted from our Saab.

The Aim of the Android Automotive POC 🚗

  • Connect a Bluetooth capable OBDII dongle to our Saab 9–5 and extract vehicle data
  • Collect RPM or Speed data through Bluetooth in Ubuntu running a Python application
  • From Ubuntu, we are going to send collected OBD-II data to the Android Emulator through sockets

Running Automotive Car Emulator on Ubuntu 18.10

In the previous article, we build the Android Automotive Image; we downloaded code Android Open Source Project. Now it is time to run the image in the Android Emulator!

The Automotive Emulator images located on the below path

cd out/target/product/generic_x86

To run the Car Emulator, you need to execute the below command to change some access permissions:

sudo chmod 777 /dev/kvmemulator -show-kernel -selinux permissive

The flag “-selinux permissive“

will disable the SELinux policy, which is convenient when developing or debugging.

Note: The emulator will become invisible when opening the new terminal. To bring the emulator command visibly, provide the below command,

source build/envsetup.shlunch aosp_car_x86-userdebug

The -show-kernel parameters will show the kernel log of emulated(goldfish) kernel. Open another terminal to see the debug messages of the automotive emulator, and this is optional but very convenient.

cd android-pie-compile/out/host/linux-x86/bin
./adb root
./adb shell
logcat

In case you are not able to connect with Android shell, please check with the command

./adb devices

If the command does not work, you know that you need to troubleshoot your adb installation.

Android Automotive PoC

This is an block diagram of our Android Automitve PoC:

Get your engines ready!

Installing the first App on Automotive Emulator

Run the automotive emulator

sudo chmod 777 /dev/kvmemulator -show-kernel -selinux permissive

Install the android studio with Pie SDK(API Level 28) on Ubuntu with this link https://developer.android.com/studio/install

Follow the below screenshots to install the basic app in the automotive emulator. Make sure that you have Pie SDK (API Level 28) installed in the SDK manager in Android Studio.

Open the sample project in Android Studio.

The project window will open like below.

Build the Android project by clicking Build -> Make Project and Run -> Run App, Make sure that you are running Automotive emulator in another terminal.

Now the sample application will run on the automotive emulator.

If you want to debug the application, open the ./adb shell in the terminal window.

Connecting Android Emulator(guest) from Ubuntu(host) through Sockets

By default, the android automotive emulator uses the qemud pipe connection to communicate with Ubuntu. To change qemud to sockets, we need to do some little changes in the android source code,

Open the below path file location and edit the code like below (android-pie-compile/hardware/interfaces/automotive/vehicle/2.0/default/impl/vhal_v2_0/VehicleEmulator.cpp)

Note: The Car API’s are not yet released in android pie SDK, So we need to do some little tricks to integrate the Car API’s.Later we will see how to integrate the Car API’s in android studio. The below app source code comes with Car API’s. So it’s not necessary to integrate the SDK again.

Clone the android app source code, which uses Car Diagnostics API to view the speed and RPM in gauges.

Clone the following repo:

git clone https://github.com/HiMinds/himinds-iot-project-android-automotive-sample.git

Open the android app in the Android Studio, Build the android project,

  • File → Open (Choose the cloned path of app source)
  • Build -> Make Project
  • Build -> Build Bundles/APK -> Build APK

Note: These Car apps can’t be run directly from the android studio like shown above, because they are into platform applications.So we need special permissions to run the app on emualtor.

Create a directory in android project location like below,

mkdir -p /home/himinds/Android/android-pie-compile/packages/services/Car/tests/himinds-basic

Create the file Android.mk in the below-mentioned path,

/home/himinds/Android/android-pie-compile/packages/services/Car/tests/himinds-basic/Android.mk

Copy the below content in Android.mk file,

LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)
LOCAL_SRC_FILES := himinds-demo.apk
LOCAL_RESOURCE_DIR := $(LOCAL_PATH)/res
LOCAL_MODULE := Himinds
LOCAL_PRIVATE_PLATFORM_APIS := true
LOCAL_CERTIFICATE := platform
LOCAL_MODULE_TAGS := optional
LOCAL_MODULE_CLASS := APPS
LOCAL_PRIVILEGED_MODULE := true
LOCAL_USE_AAPT2 := true
LOCAL_PROGUARD_ENABLED := disabled
LOCAL_DEX_PREOPT := false
include $(BUILD_PREBUILT)

Go to android app source code location,

cd /home/himinds/Android-automotive-sample/app/build/outputs/apk/release

Copy the .apk file to the mentioned below path,

cp app-release-unsigned.apk /home/himinds/Android/android-pie-compile/packages/services/Car/tests/himinds-basic/himinds-demo.apk

NOTE: if suppose , you are not getting release folder after building the sources.Then change the build variant to release by clicking Build -> select build varirant

Open the below file and add the app name in android car makefile,

vi /home/himinds/Android/android-pie-compile/device/generic/car/common/car.mkPRODUCT_PACKAGES += \android.hardware.broadcastradio@2.0-service \android.hardware.automotive.vehicle@2.0-service \+ Himinds

Now build the Android source code with above modifications,

cd /home/himinds/Android/android-pie-compile/make -j$(nproc)

Run the emulator,

sudo chmod 777 /dev/kvmemulator -show-kernel -selinux permissive

Open the MyApplication app in the emulator, you will see two gauges, the left one is for speed and the right one is for RPM.

Now the automotive app setup is done, let’s see how to send sensor data to the automotive emulator.

Install protobuf in ubuntu using the below command,

sudo pip install protobuf

Clone the source code, this simulates some random sensor data of OBDII instead of real OBD-II dongle.

git clone https://github.com/HiMinds/himinds-iot-project-android-automotive-OBD-simulator.git

Open the file vhal_emulator.py from simulator source code, change the adbCmd variable with the corresponding android path of adb binary.

adbCmd = ‘/home/himinds/Android/android-pie-compile/out/host/linux-x86/bin/adb %s forward tcp:0 tcp:%d’ % (extraArgs, remotePortNumber)

Change /home/himinds/Android/android-pie-compile with your android source code path.

Now run the python script interface-obdii-to-android.py, it starts to send some random sensor data to automotive emulator through sockets.

python interface-obdii-to-android.py

Now see the MyApplication app the needles in the gauge get rotate based on our random sensor value.

Manual Integration of Car API in Android studio:

Open the sample project in android app studio, Copy the classes.jar file from the location:

/home/himinds/Android/android-pie-compile/out/target/common/obj/JAVA_LIBRARIES/android.car_intermediates

Paste it into the Android project libs folder,

Add the below line in build.gradle under the app section.

implementation fileTree(dir: ‘libs’, include: [‘*.jar’])

Build the Android project, and now the project will have access to the Android Automotive car API’s

Wrap up

In this article, we build our own Android Automotive App that displayed RPM and vehicle speed that was extracted from an actual Saab 9–5 via the ODB-II interface. We explored the Android Automotive Stack and learned the basic of the architecture. The code is available on GitHub and we hope you enjoyed the article!

Thank you for reading! Take care and hope to see you soon. 🙏🏽

--

--