由于API 26 SensorDirectChannel可用于更快的采样率。
它可以与MemoryFile或HardwareBuffer一起用作缓冲区。
我在尝试创建Channel时都遇到了IOExceptions。我尝试如下:
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);
我收到以下消息:
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)
使用HardwareBuffer,我得到了类似的异常。
码:
HardwareBuffer accHawBuff = HardwareBuffer.create(1040, 1, HardwareBuffer.BLOB, 1, HardwareBuffer.USAGE_SENSOR_DIRECT_DATA);
SensorDirectChannel channel= sm.createDirectChannel(accHawBuff);
异常消息:
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)
由于没有使用直接通道的示例或其他文档,因此我不确定是否可能错过了某些内容。我在运行Oreo 8.1(API 27)的真实和虚拟设备上进行了尝试。
我稍微看了一下本机函数,但找不到原因,也弄不清楚-12是什么意思。
我希望有人可以澄清这一点。
提前致谢
最佳答案
兄弟,我认为导致此问题的原因是您的设备不支持SensorDirectChannel。
我最近遇到了一个类似的问题,我无法通过传递与您所获得的Logcat信息几乎相同的MemoryFile实例来创建SensorDirectChannel,但是我可以通过传递一个HardwareBuffer实例来做到这一点,很奇怪。
在检查了一些文档之后,我从this site中找到了原因。
7.3.9项说:
块引用
如果设备实现包括直接传感器支持,则它们:
[C-3-1]必须通过isDirectChannelTypeSupported和getHighestDirectReportRateLevel API正确声明对直接渠道类型和直接报告费率级别的支持。
[C-3-2]对于声明支持传感器直接通道的所有传感器,必须至少支持两种传感器直接通道类型中的一种
TYPE_HARDWARE_BUFFER
TYPE_MEMORY_FILE
通过以下类型的主传感器(非唤醒变体)的传感器直接通道应支持事件报告:
TYPE_ACCELEROMETER
TYPE_ACCELEROMETER_UNCALIBRATED
TYPE_GYROSCOPE
TYPE_GYROSCOPE_UNCALIBRATED
TYPE_MAGNETIC_FIELD
TYPE_MAGNETIC_FIELD_UNCALIBRATED
因此,显然,传感器供应商必须使其传感器支持TYPE_HARDWARE_BUFFER SensorDirectChannel或TYPE_MEMORY_FILE SensorDirectChannel或两者都支持,否则您将无法使用它们。
最后,我通过isDirectChannelTypeSupported检查了设备(Pixel2)的加速度传感器,并得到了预期的结果,如下所示:
resultscreenshot.
希望它能对您有所帮助。
顺便说一句,我的英语不太好。抱歉,这会让您感到困惑。
关于android - 无法创建SensorDirectChanel,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/48991333/