Kotlin/Native access device sensor in Android specific part












0















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?










share|improve this question




















  • 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
















0















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?










share|improve this question




















  • 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














0












0








0








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?










share|improve this question
















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?







android kotlin cross-platform kotlin-native






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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












2 Answers
2






active

oldest

votes


















1














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.






share|improve this answer


























  • 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



















0














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)





share|improve this answer























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


    }
    });














    draft saved

    draft discarded


















    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









    1














    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.






    share|improve this answer


























    • 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
















    1














    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.






    share|improve this answer


























    • 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














    1












    1








    1







    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.






    share|improve this answer















    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.







    share|improve this answer














    share|improve this answer



    share|improve this answer








    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



















    • 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













    0














    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)





    share|improve this answer




























      0














      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)





      share|improve this answer


























        0












        0








        0







        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)





        share|improve this answer













        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)






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 19 '18 at 14:56









        Blue JonesBlue Jones

        1657




        1657






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            MongoDB - Not Authorized To Execute Command

            How to fix TextFormField cause rebuild widget in Flutter

            in spring boot 2.1 many test slices are not allowed anymore due to multiple @BootstrapWith