3

Since API 26 SensorDirectChannel is available to use for possible faster sample rates. It can be used with MemoryFile or HardwareBuffer as Buffer. I get IOExceptions with both while trying to create the Channel. I tried as following:

SensorManager sm = (SensorManager)getSystemService(Service.SENSOR_SERVICE);
MemoryFile memFile = null;
try {
   memFile = new MemoryFile("AccMemoryFile", 1040);
} catch (Exception e) {
   e.printStackTrace();
}

SensorDirectChannel channel = sm.createDirectChannel(memFile);

I get the following message:

02-26 15:15:46.477 10323-10323/de.makos.directchaneltest E/AndroidRuntime: FATAL EXCEPTION: main
                                                                       Process: de.makos.directchaneltest, PID: 10323
                                                                       java.lang.RuntimeException: Unable to start activity ComponentInfo{de.makos.directchaneltest/de.makos.directchaneltest.MainActivity}: java.io.UncheckedIOException: java.io.IOException: create MemoryFile direct channel failed -12
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                           at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                           at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                           at android.os.Looper.loop(Looper.java:164)
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                           at java.lang.reflect.Method.invoke(Native Method)
                                                                           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                        Caused by: java.io.UncheckedIOException: java.io.IOException: create MemoryFile direct channel failed -12
                                                                           at android.hardware.SystemSensorManager.createDirectChannelImpl(SystemSensorManager.java:587)
                                                                           at android.hardware.SensorManager.createDirectChannel(SensorManager.java:904)
                                                                           at de.makos.directchaneltest.MainActivity.onCreate(MainActivity.java:37)
                                                                           at android.app.Activity.performCreate(Activity.java:7044)
                                                                           at android.app.Activity.performCreate(Activity.java:7035)
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                                                                           at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                           at android.os.Looper.loop(Looper.java:164) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
                                                                        Caused by: java.io.IOException: create MemoryFile direct channel failed -12
                                                                           at android.hardware.SystemSensorManager.createDirectChannelImpl(SystemSensorManager.java:588)
                                                                           at android.hardware.SensorManager.createDirectChannel(SensorManager.java:904) 
                                                                           at de.makos.directchaneltest.MainActivity.onCreate(MainActivity.java:37) 
                                                                           at android.app.Activity.performCreate(Activity.java:7044) 
                                                                           at android.app.Activity.performCreate(Activity.java:7035) 
                                                                           at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
                                                                           at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 
                                                                           at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                                                                           at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                           at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                                                                           at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                           at android.os.Looper.loop(Looper.java:164) 
                                                                           at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                                           at java.lang.reflect.Method.invoke(Native Method) 
                                                                           at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440) 
                                                                           at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

With the HardwareBuffer I got a similar Exception. Code:

HardwareBuffer accHawBuff = HardwareBuffer.create(1040, 1, HardwareBuffer.BLOB, 1, HardwareBuffer.USAGE_SENSOR_DIRECT_DATA);
SensorDirectChannel channel= sm.createDirectChannel(accHawBuff);

Exception Message:

02-26 15:27:07.592 10774-10774/de.makos.brakelightdatacollector E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: de.makos.brakelightdatacollector, PID: 10774
                                                                              java.lang.RuntimeException: Unable to start activity ComponentInfo{de.makos.brakelightdatacollector/de.makos.brakelightdatacollector.MainActivity}: java.io.UncheckedIOException: java.io.IOException: create HardwareBuffer direct channel failed -12
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2778)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856)
                                                                                  at android.app.ActivityThread.-wrap11(Unknown Source:0)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:106)
                                                                                  at android.os.Looper.loop(Looper.java:164)
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6494)
                                                                                  at java.lang.reflect.Method.invoke(Native Method)
                                                                                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440)
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
                                                                               Caused by: java.io.UncheckedIOException: java.io.IOException: create HardwareBuffer direct channel failed -12
                                                                                  at android.hardware.SystemSensorManager.createDirectChannelImpl(SystemSensorManager.java:612)
                                                                                  at android.hardware.SensorManager.createDirectChannel(SensorManager.java:927)
                                                                                  at de.makos.brakelightdatacollector.MesseinheitDirectChanel.registerListener(MesseinheitDirectChanel.java:63)
                                                                                  at de.makos.brakelightdatacollector.MainActivity.onCreate(MainActivity.java:64)
                                                                                  at android.app.Activity.performCreate(Activity.java:7044)
                                                                                  at android.app.Activity.performCreate(Activity.java:7035)
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731)
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                                                                                  at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                  at android.os.Looper.loop(Looper.java:164) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 
                                                                               Caused by: java.io.IOException: create HardwareBuffer direct channel failed -12
                                                                                  at android.hardware.SystemSensorManager.createDirectChannelImpl(SystemSensorManager.java:613)
                                                                                  at android.hardware.SensorManager.createDirectChannel(SensorManager.java:927) 
                                                                                  at de.makos.brakelightdatacollector.MesseinheitDirectChanel.registerListener(MesseinheitDirectChanel.java:63) 
                                                                                  at de.makos.brakelightdatacollector.MainActivity.onCreate(MainActivity.java:64) 
                                                                                  at android.app.Activity.performCreate(Activity.java:7044) 
                                                                                  at android.app.Activity.performCreate(Activity.java:7035) 
                                                                                  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 
                                                                                  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2731) 
                                                                                  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2856) 
                                                                                  at android.app.ActivityThread.-wrap11(Unknown Source:0) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1589) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:106) 
                                                                                  at android.os.Looper.loop(Looper.java:164) 
                                                                                  at android.app.ActivityThread.main(ActivityThread.java:6494) 
                                                                                  at java.lang.reflect.Method.invoke(Native Method) 
                                                                                  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:440) 
                                                                                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807) 

As there are no examples or other documentation for the usage of Direct Channels I am not sure if I maybe missed something. I tried it on a real and a virtual device, both running Oreo 8.1 (API 27). I looked a little bit into the native function but i could not find a reason.I also could not figure out what the -12 means.

I hope someone can clarify this. Thanks in advance

heavySea
  • 31
  • 4
  • can you please confirm on which device you are running this piece of code! – Raja Singla May 11 '18 at 19:28
  • It was tested on a Xiaomi Mi4c with an unofficial Android 8 build. The driver probably does not support that feature. Due to the progress of the project, this issue was not further investigated. – heavySea May 15 '18 at 08:46

1 Answers1

0

bro, I think the reason that cause this issue is that your device doesn't support SensorDirectChannel.

I recently encountered a similar problem that I can't create a SensorDirectChannel by passing a MemoryFile instance with almost same Logcat information as what you got, but I CAN have it done by passing a HardwareBuffer instance, weird. After checking some docs I found the reason from this site.

And the item 7.3.9 say:

Blockquote

If device implementations include direct sensor support, they:

[C-3-1] MUST correctly declare support of direct channel types and direct report rates level through the isDirectChannelTypeSupported and getHighestDirectReportRateLevel API.

[C-3-2] MUST support at least one of the two sensor direct channel types for all sensors that declare support for sensor direct channel

  • TYPE_HARDWARE_BUFFER
  • TYPE_MEMORY_FILE
  • SHOULD support event reporting through sensor direct channel for primary sensor (non-wakeup variant) of the following types:
  • TYPE_ACCELEROMETER
  • TYPE_ACCELEROMETER_UNCALIBRATED
  • TYPE_GYROSCOPE
  • TYPE_GYROSCOPE_UNCALIBRATED
  • TYPE_MAGNETIC_FIELD
  • TYPE_MAGNETIC_FIELD_UNCALIBRATED

So, obviously the sensor vendor must make their sensor support the TYPE_HARDWARE_BUFFER SensorDirectChannel or TYPE_MEMORY_FILE SensorDirectChannel or both of them, or you cannot make use of them.

At last, I check the acceleration sensor of my device (Pixel2) via isDirectChannelTypeSupported and got an expected result as follows: resultscreenshot. Hope it can help you.

Btw, my English is not so good. Apologize if it makes you confused.

Huang Li
  • 11
  • 1
  • Yes, this was also the conclusion I came to. I used and unofficial Android build, not supported by the vendor. – heavySea Jan 15 '19 at 11:40