ndk
ndk copied to clipboard
More practical examples: Accelerometer for example
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();
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.