1

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?

General Grievance
  • 4,555
  • 31
  • 31
  • 45
Hussien Fahmy
  • 1,119
  • 6
  • 24

1 Answers1

2

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.

Ben Sagmoe
  • 440
  • 2
  • 9
  • I used `carContext.mainExecutor` but still getting the crashe. – Hussien Fahmy Dec 22 '22 at 17:35
  • The error points to the lone with `sensors.addAccelerometerListener` – Hussien Fahmy Dec 22 '22 at 17:38
  • 1
    Ahh, sorry I was focusing on the second half of the code. I'm pretty sure the issue is really in your first line of code where you're creating a `CarHardwareManager` using a new `HostDispatcher` instead of just getting the `CarHardwareManager` from the `carContext`. You should use something like `val carSensors = carContext.getCarService(CarHardwareManager::class.java).carSensors` instead of the first two lines in your snippet. – Ben Sagmoe Dec 27 '22 at 15:16