Search code examples
androidandroid-autoandroid-automotive

Desktop Head Unit (DHU): how to mock sensors data


Am trying to listen for sensor changes of Android auto through Desktop Head Unit with the following code

    val carHardwareManager = CarHardwareManager.create(carContext, HostDispatcher())
    val sensors = carHardwareManager.carSensors
    
    sensors.addAccelerometerListener(
    CarSensors.UPDATE_RATE_NORMAL, { }
    ) {
       Log.d(TAG, "onGetTemplate: Accelerometer statues = ${it.forces.status}")
       Log.d(TAG, "onGetTemplate: Accelerometer value = ${it.forces.value}")
      }

But am getting this error

java.lang.RuntimeException: androidx.car.app.HostException: Remote subscribeCarHardwareResult call failed
    at androidx.car.app.utils.RemoteUtils.lambda$dispatchCallFromHost$0(RemoteUtils.java:155)
    at androidx.car.app.utils.RemoteUtils$$ExternalSyntheticLambda1.run(Unknown Source:6)
    at androidx.car.app.utils.ThreadUtils.runOnMain(ThreadUtils.java:39)
    at androidx.car.app.utils.RemoteUtils.dispatchCallFromHost(RemoteUtils.java:147)
    at androidx.car.app.utils.RemoteUtils.lambda$dispatchCallFromHost$1(RemoteUtils.java:186)
    at androidx.car.app.utils.RemoteUtils$$ExternalSyntheticLambda2.run(Unknown Source:8)
    at android.os.Handler.handleCallback(Handler.java:938)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:236)
    at android.app.ActivityThread.main(ActivityThread.java:8057)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:620)
    atcom.android.internal.os.ZygoteInit.main(ZygoteInit.java:1011)

As the documentation states, I enabled the sensors in the configuration file nad launched the DHU with it. But how to use their command. for example accel [x] [y] [z] where I use this command for example?


Solution

  • You type the command into the terminal window where you started the DHU.

    For example, if you started the DHU on a Mac by typing

    ./desktop-head-unit -c configName.ini
    

    you would also type the command into that same terminal after the DHU had started up and connected with the device - for example:

    accel 1 1 1
    

    and then the callback you've set on the listener should be invoked with the new data.

    As far as the logs/crashing go, I think the issue might be that you're not properly setting an Executor as the second parameter for addAccelerometerListener. You can use carContext.mainExecutor as this param.