Develop application for non-AGA distribution


The Automotive Grade Android can be set up and run with a single factory call. However due to Android not allowing network traffic on the same thread as the main activity thread we must create a new thread. This is accomplished by using the AsyncTask.

Pre-requisits of tools and environment


There are four important programs that needs to be downloaded depending on what you want to do.

  • Java
  • Git
  • SSH key generated and sent to the AGA Development team
  • Android Studio

Signal - How to get an instance of Automotive API and how to listen to vehicle's signals


  1. In your Android Studio project go to the <module_name>/src/main/res folder and open the AndroidManifest.xml file.
  2. Side the manifest tag but outside the application tag add the following two lines.
    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
    <uses-permission android:name="android.permission.INTERNET"/>
    
  3. Open upp your Activity java file that is located in the <module_name>/src/main/java/<your_package_name>/<your_activity_name>.java
  4. The Activity only needs the code below in order to receive driver distraction level and vehicle speed.
    public class MainActivity extends Activity {
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            new AsyncTask() {      //Network operation must be run on separate thread than main thread 
                @Override
                protected Object doInBackground(Object... objects) {
                    AutomotiveFactory.createAutomotiveManagerInstance(
                        new AutomotiveCertificate(new byte[0]), //Provided certificate
                        new AutomotiveListener() {
                            @Override
                            public void receive(AutomotiveSignal automotiveSignal) {
                                Log.v("Demo", "Speed is: " + ((SCSFloat)automotiveSignal.getData()).getFloatValue());
                            }
    
                            @Override
                            public void timeout(int i) {}
    
                            @Override
                            public void notAllowed(int i) {}
                        },
                        new DriverDistractionListener() {       //Listener for driver distraction level
                            @Override
                            public void levelChanged(DriverDistractionLevel driverDistractionLevel) {
                                Log.v("Demo", "Driver distraction level is: " + driverDistractionLevel.getLevel());
                            }
                        }
                    ).register(AutomotiveSignalId.FMS_WHEEL_BASED_SPEED);
                    return null;
                }
            }.execute();
        }
    }
    
    Since android does not allow the main activity thread to call network operations we create an annonymous class of the type AsyncTask that will run our Automotive instance in another thread. The AutomotiveManager is created from a factory and requires three things: 
    1. An AutomotiveCertificate; right now this is created by sending in an empty byte array.
    2. A Signal listener; the AutomotiveListener will listen to any signals that the AutomotiveManager has registered or requested.
    3. A DriverDistractionListener; the driver distraction level has its own listener and you should take notice of when this is changed and make the appropriate changes in the program when a new level arrives.
    After the AutomotiveInstance from the factory is received, register the AutomotiveSignalId.VEHICLE_SPEED with ID number 320. For a complete list of all signal IDs look here. Once the annomanous AsyncTask class is complete, call its execute method that will start the thread and our automotive listener.
 

Android Device - how to use an Android device or emulator together with simulator


Make sure that your device is connected and, if you are using your own phone/tablet, do not forget to turn on developer mode. If the device is not connected use the following commands:

adb kill-server
adb start-server

Next, start the application on either a phone, tablet or emulator.  If you are connected to device using USB use the following commands in a terminal or windows cmd (the tool is located in the android sdk platform-tools folder):

adb forward tcp:8251 tcp:8251
adb forward tcp:9898 tcp:9898
adb forward tcp:9899 tcp:9899

It will forward any packages sent to these ports on localhost via ADB to the device and your app.
Start the simulator-gui. For more information on the simulator, see simulator-gui wiki-page.
The application will now respond via logcat debug messages both the vehicle speed and the driver distraction level whenever it is changed in the simulator.