Kotlin/Native access device sensor in Android specific part
I'm currently stuck on a task that i thought is quite basic. Im developing a library in Kotlin/Native that should query the gravity sensor in iOS and Android.
Almost all the logic is in the common part and just the communication with the devices sensor is implemented in the platform specific methods. Surprisingly all the iOS core libraries (CoreMotion in this case) have been ported so that the implementation was quite easy.
CoreMotion in this case allows me to add a listener on the gravity sensor which is calling a callback whenever the orientation of the screen changes with respect to X, Y or Z (I'm interested in Z btw)
In Android I'd do something like this
private lateinit var sensorManager: SensorManager
fun setupSensor() {
this.sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)?.let {
this.accelerometer = it
}
sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)?.let {
this.gravity = it
}
}
But the imports needed are not available in Kotlin/Native
import android.hardware.Sensor;
import android.hardware.SensorManager;
Is there a way to access this kind of hardware (the gravity sensor) in Kotlin/Native? Or better in the Android specific part of Kotlin/Native?

add a comment |
I'm currently stuck on a task that i thought is quite basic. Im developing a library in Kotlin/Native that should query the gravity sensor in iOS and Android.
Almost all the logic is in the common part and just the communication with the devices sensor is implemented in the platform specific methods. Surprisingly all the iOS core libraries (CoreMotion in this case) have been ported so that the implementation was quite easy.
CoreMotion in this case allows me to add a listener on the gravity sensor which is calling a callback whenever the orientation of the screen changes with respect to X, Y or Z (I'm interested in Z btw)
In Android I'd do something like this
private lateinit var sensorManager: SensorManager
fun setupSensor() {
this.sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)?.let {
this.accelerometer = it
}
sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)?.let {
this.gravity = it
}
}
But the imports needed are not available in Kotlin/Native
import android.hardware.Sensor;
import android.hardware.SensorManager;
Is there a way to access this kind of hardware (the gravity sensor) in Kotlin/Native? Or better in the Android specific part of Kotlin/Native?

1
Can you ask a more specific question? What sensors are you trying to use, and what are you trying to do with them?
– Gabe Sechan
Nov 19 '18 at 13:57
I'd need the gravity sensor as said above. Specifically I'd need it's Z value which tells if the screen is face up, face down or somewhere in between... I'll update the question
– Hons
Nov 19 '18 at 14:11
add a comment |
I'm currently stuck on a task that i thought is quite basic. Im developing a library in Kotlin/Native that should query the gravity sensor in iOS and Android.
Almost all the logic is in the common part and just the communication with the devices sensor is implemented in the platform specific methods. Surprisingly all the iOS core libraries (CoreMotion in this case) have been ported so that the implementation was quite easy.
CoreMotion in this case allows me to add a listener on the gravity sensor which is calling a callback whenever the orientation of the screen changes with respect to X, Y or Z (I'm interested in Z btw)
In Android I'd do something like this
private lateinit var sensorManager: SensorManager
fun setupSensor() {
this.sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)?.let {
this.accelerometer = it
}
sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)?.let {
this.gravity = it
}
}
But the imports needed are not available in Kotlin/Native
import android.hardware.Sensor;
import android.hardware.SensorManager;
Is there a way to access this kind of hardware (the gravity sensor) in Kotlin/Native? Or better in the Android specific part of Kotlin/Native?

I'm currently stuck on a task that i thought is quite basic. Im developing a library in Kotlin/Native that should query the gravity sensor in iOS and Android.
Almost all the logic is in the common part and just the communication with the devices sensor is implemented in the platform specific methods. Surprisingly all the iOS core libraries (CoreMotion in this case) have been ported so that the implementation was quite easy.
CoreMotion in this case allows me to add a listener on the gravity sensor which is calling a callback whenever the orientation of the screen changes with respect to X, Y or Z (I'm interested in Z btw)
In Android I'd do something like this
private lateinit var sensorManager: SensorManager
fun setupSensor() {
this.sensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER)?.let {
this.accelerometer = it
}
sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY)?.let {
this.gravity = it
}
}
But the imports needed are not available in Kotlin/Native
import android.hardware.Sensor;
import android.hardware.SensorManager;
Is there a way to access this kind of hardware (the gravity sensor) in Kotlin/Native? Or better in the Android specific part of Kotlin/Native?


edited Nov 19 '18 at 14:23
Hons
asked Nov 19 '18 at 13:41
HonsHons
2,31532241
2,31532241
1
Can you ask a more specific question? What sensors are you trying to use, and what are you trying to do with them?
– Gabe Sechan
Nov 19 '18 at 13:57
I'd need the gravity sensor as said above. Specifically I'd need it's Z value which tells if the screen is face up, face down or somewhere in between... I'll update the question
– Hons
Nov 19 '18 at 14:11
add a comment |
1
Can you ask a more specific question? What sensors are you trying to use, and what are you trying to do with them?
– Gabe Sechan
Nov 19 '18 at 13:57
I'd need the gravity sensor as said above. Specifically I'd need it's Z value which tells if the screen is face up, face down or somewhere in between... I'll update the question
– Hons
Nov 19 '18 at 14:11
1
1
Can you ask a more specific question? What sensors are you trying to use, and what are you trying to do with them?
– Gabe Sechan
Nov 19 '18 at 13:57
Can you ask a more specific question? What sensors are you trying to use, and what are you trying to do with them?
– Gabe Sechan
Nov 19 '18 at 13:57
I'd need the gravity sensor as said above. Specifically I'd need it's Z value which tells if the screen is face up, face down or somewhere in between... I'll update the question
– Hons
Nov 19 '18 at 14:11
I'd need the gravity sensor as said above. Specifically I'd need it's Z value which tells if the screen is face up, face down or somewhere in between... I'll update the question
– Hons
Nov 19 '18 at 14:11
add a comment |
2 Answers
2
active
oldest
votes
If you really want it as Kotlin/Native library on Android - it will have direct access to Android NDK, not SDK.
So for sensors it can use functions from
https://android.googlesource.com/platform/frameworks/native/+/jb-dev/include/android/sensor.h
In Kotlin/Native these functions available in package platform.android.*
But if all you want is just make your library available in both iOS and Android - much simpler and common way is compile same sources by Kotlin/Native to iOS and by regular Kotlin to Android, using "kotlin-multiplatform" gradle plugin.
Thanks that was the right hint! At the end I just had a configuration error in the Android part, so I could not access/import the android specific part... Unfortunately the official guide (kotlinlang.org/docs/tutorials/native/…) is a bit missleading here, but taking the relevant parts from github.com/irgaly/kotlin-multiplatform solved it.
– Hons
Nov 20 '18 at 9:00
add a comment |
I think it would be something like;
val sensorManager: SensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensorManager.registerListener(object : SensorEventListener {
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
//Not needed
}
override fun onSensorChanged(event: SensorEvent?) {
if (event?.sensor?.type == Sensor.TYPE_GRAVITY) {
System.out.println("X: " + event.values[0])
System.out.println("Y: " + event.values[1])
System.out.println("Z: " + event.values[2])
}
}
}, sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY), Sensor.TYPE_GRAVITY)
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375901%2fkotlin-native-access-device-sensor-in-android-specific-part%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
If you really want it as Kotlin/Native library on Android - it will have direct access to Android NDK, not SDK.
So for sensors it can use functions from
https://android.googlesource.com/platform/frameworks/native/+/jb-dev/include/android/sensor.h
In Kotlin/Native these functions available in package platform.android.*
But if all you want is just make your library available in both iOS and Android - much simpler and common way is compile same sources by Kotlin/Native to iOS and by regular Kotlin to Android, using "kotlin-multiplatform" gradle plugin.
Thanks that was the right hint! At the end I just had a configuration error in the Android part, so I could not access/import the android specific part... Unfortunately the official guide (kotlinlang.org/docs/tutorials/native/…) is a bit missleading here, but taking the relevant parts from github.com/irgaly/kotlin-multiplatform solved it.
– Hons
Nov 20 '18 at 9:00
add a comment |
If you really want it as Kotlin/Native library on Android - it will have direct access to Android NDK, not SDK.
So for sensors it can use functions from
https://android.googlesource.com/platform/frameworks/native/+/jb-dev/include/android/sensor.h
In Kotlin/Native these functions available in package platform.android.*
But if all you want is just make your library available in both iOS and Android - much simpler and common way is compile same sources by Kotlin/Native to iOS and by regular Kotlin to Android, using "kotlin-multiplatform" gradle plugin.
Thanks that was the right hint! At the end I just had a configuration error in the Android part, so I could not access/import the android specific part... Unfortunately the official guide (kotlinlang.org/docs/tutorials/native/…) is a bit missleading here, but taking the relevant parts from github.com/irgaly/kotlin-multiplatform solved it.
– Hons
Nov 20 '18 at 9:00
add a comment |
If you really want it as Kotlin/Native library on Android - it will have direct access to Android NDK, not SDK.
So for sensors it can use functions from
https://android.googlesource.com/platform/frameworks/native/+/jb-dev/include/android/sensor.h
In Kotlin/Native these functions available in package platform.android.*
But if all you want is just make your library available in both iOS and Android - much simpler and common way is compile same sources by Kotlin/Native to iOS and by regular Kotlin to Android, using "kotlin-multiplatform" gradle plugin.
If you really want it as Kotlin/Native library on Android - it will have direct access to Android NDK, not SDK.
So for sensors it can use functions from
https://android.googlesource.com/platform/frameworks/native/+/jb-dev/include/android/sensor.h
In Kotlin/Native these functions available in package platform.android.*
But if all you want is just make your library available in both iOS and Android - much simpler and common way is compile same sources by Kotlin/Native to iOS and by regular Kotlin to Android, using "kotlin-multiplatform" gradle plugin.
edited Nov 20 '18 at 5:57
answered Nov 20 '18 at 2:10
Mike SinkovskyMike Sinkovsky
362
362
Thanks that was the right hint! At the end I just had a configuration error in the Android part, so I could not access/import the android specific part... Unfortunately the official guide (kotlinlang.org/docs/tutorials/native/…) is a bit missleading here, but taking the relevant parts from github.com/irgaly/kotlin-multiplatform solved it.
– Hons
Nov 20 '18 at 9:00
add a comment |
Thanks that was the right hint! At the end I just had a configuration error in the Android part, so I could not access/import the android specific part... Unfortunately the official guide (kotlinlang.org/docs/tutorials/native/…) is a bit missleading here, but taking the relevant parts from github.com/irgaly/kotlin-multiplatform solved it.
– Hons
Nov 20 '18 at 9:00
Thanks that was the right hint! At the end I just had a configuration error in the Android part, so I could not access/import the android specific part... Unfortunately the official guide (kotlinlang.org/docs/tutorials/native/…) is a bit missleading here, but taking the relevant parts from github.com/irgaly/kotlin-multiplatform solved it.
– Hons
Nov 20 '18 at 9:00
Thanks that was the right hint! At the end I just had a configuration error in the Android part, so I could not access/import the android specific part... Unfortunately the official guide (kotlinlang.org/docs/tutorials/native/…) is a bit missleading here, but taking the relevant parts from github.com/irgaly/kotlin-multiplatform solved it.
– Hons
Nov 20 '18 at 9:00
add a comment |
I think it would be something like;
val sensorManager: SensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensorManager.registerListener(object : SensorEventListener {
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
//Not needed
}
override fun onSensorChanged(event: SensorEvent?) {
if (event?.sensor?.type == Sensor.TYPE_GRAVITY) {
System.out.println("X: " + event.values[0])
System.out.println("Y: " + event.values[1])
System.out.println("Z: " + event.values[2])
}
}
}, sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY), Sensor.TYPE_GRAVITY)
add a comment |
I think it would be something like;
val sensorManager: SensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensorManager.registerListener(object : SensorEventListener {
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
//Not needed
}
override fun onSensorChanged(event: SensorEvent?) {
if (event?.sensor?.type == Sensor.TYPE_GRAVITY) {
System.out.println("X: " + event.values[0])
System.out.println("Y: " + event.values[1])
System.out.println("Z: " + event.values[2])
}
}
}, sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY), Sensor.TYPE_GRAVITY)
add a comment |
I think it would be something like;
val sensorManager: SensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensorManager.registerListener(object : SensorEventListener {
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
//Not needed
}
override fun onSensorChanged(event: SensorEvent?) {
if (event?.sensor?.type == Sensor.TYPE_GRAVITY) {
System.out.println("X: " + event.values[0])
System.out.println("Y: " + event.values[1])
System.out.println("Z: " + event.values[2])
}
}
}, sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY), Sensor.TYPE_GRAVITY)
I think it would be something like;
val sensorManager: SensorManager = getSystemService(Context.SENSOR_SERVICE) as SensorManager
sensorManager.registerListener(object : SensorEventListener {
override fun onAccuracyChanged(sensor: Sensor?, accuracy: Int) {
//Not needed
}
override fun onSensorChanged(event: SensorEvent?) {
if (event?.sensor?.type == Sensor.TYPE_GRAVITY) {
System.out.println("X: " + event.values[0])
System.out.println("Y: " + event.values[1])
System.out.println("Z: " + event.values[2])
}
}
}, sensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY), Sensor.TYPE_GRAVITY)
answered Nov 19 '18 at 14:56
Blue JonesBlue Jones
1657
1657
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53375901%2fkotlin-native-access-device-sensor-in-android-specific-part%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
Can you ask a more specific question? What sensors are you trying to use, and what are you trying to do with them?
– Gabe Sechan
Nov 19 '18 at 13:57
I'd need the gravity sensor as said above. Specifically I'd need it's Z value which tells if the screen is face up, face down or somewhere in between... I'll update the question
– Hons
Nov 19 '18 at 14:11