Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. Unable to Trigger Vibration on Android Device from Qt, C++ development platform.
QtWS25 Last Chance

Unable to Trigger Vibration on Android Device from Qt, C++ development platform.

Scheduled Pinned Locked Moved Unsolved Mobile and Embedded
2 Posts 2 Posters 338 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    NaveenKumar1203
    wrote on 12 Apr 2024, 03:55 last edited by
    #1

    In Qt 5.15.2 I'm developing a Qt/QML application where I need to trigger vibration on an Android device when the user clicks the button. I've followed all the necessary steps, including importing the Qt.AndroidExtras module, adding the appropriate permissions to the AndroidManifest.xml file, and calling the QtAndroid.currentActivity.vibrate() function in the onClicked handler of the login button.

    However, following these steps, vibration is not working when I click the login button. I've verified that the onClicked handler is being triggered and that there are no errors or warnings in the Android logcat output.

    Here's the relevant code snippet from my files ,

    .pro:

    QT += androidextras
    QTANDROID_EXPORTED_JAVA_LIBRARIES += $$PWD/build-qgroundcontrol-Android_Qt_5_15_2_Clang_Multi_Abi-Debug/android-build/libs/QtAndroidExtras.jar

    c++:

    void vibrate(int milliseconds) {

    QAndroidJniObject vibrator = QAndroidJniObject::callStaticObjectMethod("android/os/Vibrator", "getSystemService",
                                                                           "(Ljava/lang/String;)Ljava/lang/Object;",
                                                                           QAndroidJniObject::fromString("vibrator").object());
    
    if (vibrator.isValid()) {
        
        vibrator.callMethod<void>("vibrate", "(J)V", jlong(milliseconds));
    }
    

    }

    int main(int argc, char *argv[]) {
    QGuiApplication app(argc, argv);

    vibrate(10000);
    
    return app.exec();
    

    }

    Android manifest.xml:

    <uses-permission android:name="android.permission.VIBRATE"/>

    Include statements:
    #include <QAndroidJniObject>
    #include <QtAndroidExtras>
    import Qt.AndroidExtras 1.0


    qml code :

    Button {

      onClicked: {
         vibrationController.vibrate(1000);
    }
    
    A 1 Reply Last reply 13 Apr 2024, 08:27
    0
    • N NaveenKumar1203
      12 Apr 2024, 03:55

      In Qt 5.15.2 I'm developing a Qt/QML application where I need to trigger vibration on an Android device when the user clicks the button. I've followed all the necessary steps, including importing the Qt.AndroidExtras module, adding the appropriate permissions to the AndroidManifest.xml file, and calling the QtAndroid.currentActivity.vibrate() function in the onClicked handler of the login button.

      However, following these steps, vibration is not working when I click the login button. I've verified that the onClicked handler is being triggered and that there are no errors or warnings in the Android logcat output.

      Here's the relevant code snippet from my files ,

      .pro:

      QT += androidextras
      QTANDROID_EXPORTED_JAVA_LIBRARIES += $$PWD/build-qgroundcontrol-Android_Qt_5_15_2_Clang_Multi_Abi-Debug/android-build/libs/QtAndroidExtras.jar

      c++:

      void vibrate(int milliseconds) {

      QAndroidJniObject vibrator = QAndroidJniObject::callStaticObjectMethod("android/os/Vibrator", "getSystemService",
                                                                             "(Ljava/lang/String;)Ljava/lang/Object;",
                                                                             QAndroidJniObject::fromString("vibrator").object());
      
      if (vibrator.isValid()) {
          
          vibrator.callMethod<void>("vibrate", "(J)V", jlong(milliseconds));
      }
      

      }

      int main(int argc, char *argv[]) {
      QGuiApplication app(argc, argv);

      vibrate(10000);
      
      return app.exec();
      

      }

      Android manifest.xml:

      <uses-permission android:name="android.permission.VIBRATE"/>

      Include statements:
      #include <QAndroidJniObject>
      #include <QtAndroidExtras>
      import Qt.AndroidExtras 1.0


      qml code :

      Button {

        onClicked: {
           vibrationController.vibrate(1000);
      }
      
      A Offline
      A Offline
      Axel Spoerl
      Moderators
      wrote on 13 Apr 2024, 08:27 last edited by Axel Spoerl
      #2

      @NaveenKumar1203
      Two general remarks, apart from the actual problem:

      • A lot of Android related improvements are available from Qt 6.5 onward. Maybe upgrading from outdated 5.15.2 is an option.
      • please format your code, using the code tags and verify everything is formatted correctly. It makes it so much easier for others to read what you write.

      vibration is not working when I click the login button

      Does that mean, the vibration works in main()?
      here:

      int main(int argc, char *argv[]) {
      QGuiApplication app(argc, argv);
      vibrate(10000);
      return app.exec();

      but not here:

      Button {
      onClicked: {
      vibrationController.vibrate(1000);
      }

      In that case, please show your vibrationControllerextension class. My guess would be that it's either not exposed to QML correctly, or vibrate()is not Q_INVOKABLE.

      Software Engineer
      The Qt Company, Oslo

      1 Reply Last reply
      0

      2/2

      13 Apr 2024, 08:27

      • Login

      • Login or register to search.
      2 out of 2
      • First post
        2/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved