How to add vibration to any cocos2dx 3.3 Android app

I had originally avoided using vibration in my game, because I had found several different posts trying to figure out exactly what you needed to do, and I wasn’t able to find any tutorials that really broke down the process, so I chugged some coffee and dug into the code that was linked on the official forums. forum post. I can’t overstate how helpful it was to have a sub 10 second turn around time, between using Visual Studio to write the code, cocos to compile it (cocos compile -p android) and then to push it to my android phone with adb (adb install -r debug.apk), and then monitoring what was happening with logcat (adb logcat *:w), filtering only for warnings, errors and anything higher). Before we jump into it though, here’s a rough summary:

  1. You need to add a header in your Classes/ folder where you just have the vibrate function declared but not defined.
  2. Add a vibration function definition in the, in a .cpp file. I used , which I believe is the default for a starter cocos2dxv3 project. In this method you call the method you’ve defined in your java, in this case it was defined as a static method on your Activity. The default was named AppActivity .
  3. make sure you’ve got the vibration permission on your , <uses-permission android:name= android.permission.VIBRATE>”

So, in one of your standard C++ headers in the Classes/ folder, add

    void vibrate(int milliseconds);

and in the associated <tt>.cpp</tt> file, call it: 

    void get_hit()

In your , you’ll want to add that line

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

In a cpp file the jni will deal with, say , you’ll need to define the connection between the Java and C++ by calling the Java method:

    /* this function should already exist though */
    void cocos_android_app_init (JNIEnv* env, jobject thiz) {
        AppDelegate *pAppDelegate = new AppDelegate();

    /* this is the new one you're adding, where org/cocos2dx/cpp/AppActivity
       is the java file where your Activity with the vibrate static method is defined
    void vibrate(int milliseconds)
        JniMethodInfo minfo;
        CCAssert(JniHelper::getStaticMethodInfo(minfo, "org/cocos2dx/cpp/AppActivity", "vibrate", "(I)V"), "Function doesn't exist");
        minfo.env->CallStaticVoidMethod(minfo.classID, minfo.methodID, (jint)milliseconds);

Finally, here’s the .java file where the android call to vibrate is made. Mine was

    /* default imports */
    package org.cocos2dx.cpp;

    import org.cocos2dx.lib.Cocos2dxActivity;

    /* imports needed for vibration stuff */
    import android.os.Vibrator;
    import android.content.Context;
    import android.util.Log;
    import android.os.Bundle;

    public class AppActivity extends Cocos2dxActivity {

        private static volatile Cocos2dxActivity mainActivity;
        public static void setMainActivity(Cocos2dxActivity activity)
            mainActivity = activity;

        public static Cocos2dxActivity getMainActivity()
            if(mainActivity == null)
                Log.w("MY_APP_NAME_GOES_HERE", "Warning : null main Activity");
            return mainActivity;

    	protected void onCreate(Bundle savedInstanceState)

        public static void vibrate(int milliseconds)
            Vibrator v = (Vibrator) getMainActivity().getSystemService(Context.VIBRATOR_SERVICE);
            if (getMainActivity().getSystemService(Context.VIBRATOR_SERVICE) != null)

Hope this helped you!

Leave a Reply

Your email address will not be published.