Search code examples
androidandroid-sensors

Android SensorDirectChannel returns no sensor measurements


I'm trying to use the SensorDirectChannel added in Andriod 8. With the memory file as shared medium it is all zero. Using the hardware buffer the returned array is of size zero.

I initiate the SensorDirectChannel like this:

if (mSensor.isDirectChannelTypeSupported(SensorDirectChannel.TYPE_HARDWARE_BUFFER)) {
    try {
        hawBuff = HardwareBuffer.create(1040, 1, HardwareBuffer.BLOB, 1, HardwareBuffer.USAGE_SENSOR_DIRECT_DATA);
        mSensorDirectChannelBuff = mSensorManager.createDirectChannel(hawBuff);
        mSensorDirectChannelBuff.configure(mSensor, SensorDirectChannel.RATE_FAST);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

Then I try to read from the hardware buffer like this:

if (mSensorDirectChannelBuff.isOpen()) {
    Parcel measurement = Parcel.obtain();
    hawBuff.writeToParcel(measurement, Parcelable.PARCELABLE_WRITE_RETURN_VALUE);
    int[] measurementArray = measurement.createIntArray();
    try {
        Log.d("SensorDirectChannel", "HardwareBuffer: " + measurementArray[0]);
    } catch (Exception e) {
        Log.e("HardwareBuffer", "array is empty");
        e.printStackTrace();
    }
}

The array is always of size zero. I don't know if I missed something in the docs or what I'm doing wrong. Does someone have an idea what's wrong?


Solution

  • Now, after some software updates, the smartphone does no longer support sensor direct channels. The line mSensor.isDirectChannelTypeSupported(SensorDirectChannel.TYPE_HARDWARE_BUFFER) now returns false. The same is the case for the memory file option.

    So I guess it never worked from the start, causing my problems trying to use it.