Skip to content

More practical examples: Accelerometer for example #284

Closed

Description

@bit-garden

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();

Activity

  1. MarijnS95 commented on Jun 9, 2022

    @MarijnS95
    Member

    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.

  2. locked and limited conversation to collaborators on Dec 22, 2022
  3. converted this issue into a discussion #381 on Dec 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions