Kotlin/Native access device sensor in Android specific part
up vote
0
down vote
favorite
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 |
up vote
0
down vote
favorite
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 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 at 14:11
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
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 at 14:23
asked Nov 19 at 13:41
Hons
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 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 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 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 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 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 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 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 at 14:11
add a comment |
2 Answers
2
active
oldest
votes
up vote
1
down vote
accepted
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 at 9:00
add a comment |
up vote
0
down vote
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 |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
accepted
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 at 9:00
add a comment |
up vote
1
down vote
accepted
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 at 9:00
add a comment |
up vote
1
down vote
accepted
up vote
1
down vote
accepted
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 at 5:57
answered Nov 20 at 2:10
Mike 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 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 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 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 at 9:00
add a comment |
up vote
0
down vote
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 |
up vote
0
down vote
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 |
up vote
0
down vote
up vote
0
down vote
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 at 14:56
Blue Jones
1666
1666
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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 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 at 14:11