Unable to Trigger Vibration on Android Device from Qt, C++ development platform.
-
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.jarc++:
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); }
-
@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
vibrationController
extension class. My guess would be that it's either not exposed to QML correctly, orvibrate()
is notQ_INVOKABLE
.