ndk icon indicating copy to clipboard operation
ndk copied to clipboard

More practical examples: Accelerometer for example

Open bit-garden opened this issue 3 years ago • 1 comments

I'm currently trying to figure out how to read the accelerometer but am not sure how some of this is supposed to fit.

Java examples show implementing SensorEventListener and then registering it to the SensorManager, but I'm not sure how that's supposed to work.

I have this so far

  let ctx = ndk_context::android_context();
  let vm = unsafe { jni::JavaVM::from_raw(ctx.vm().cast()) }.unwrap();
  let env = vm.attach_current_thread().unwrap();

  let class_ctxt = env.find_class("android/content/Context").unwrap();
  let sensor_service = env
    .get_static_field(class_ctxt, "SENSOR_SERVICE", "Ljava/lang/String;")
    .unwrap();

  let sensor_manager = env
    .call_method(
      ctx.context().cast(),
      "getSystemService",
      "(Ljava/lang/String;)Ljava/lang/Object;",
      &[sensor_service],
    )
    .unwrap()
    .l()
    .unwrap();

bit-garden avatar Jun 08 '22 18:06 bit-garden

It is probably easier / recommended to go through the NDK sensor bindings instead: https://developer.android.com/ndk/reference/group/sensor

The types are available in ndk-sys (ffi crate) but don't yet have safe(r) wrappers in the ndk crate.

MarijnS95 avatar Jun 09 '22 15:06 MarijnS95