iOS and Android Native functionality (vibration and alert)

There have been quite a few people asking on the forums about how to achieve this and so here is the implementation I use within my own project, which should provide suitable reference for anyone looking to implement a similar system themselves.

I will, however, start with a disclaimer - this isn’t necessarily the best way to go about doing this - it is simply the method I used.

I assume throughout you have your project setup the way described here - http://www.raywenderlich.com/33750/cocos2d-x-tutorial-for-ios-and-android-getting-started

First download the attached zip file.

iOS

We’ll first get it setup for iOS as it is simpler.

Copy TTIOSNative.h, TTIOSNative.mm and TTIONativeInterface.h into your proj.ios directory. Then add them as references to your app target.

Copy TTNative.h and TTNative.cpp into your classes folder and add them to your app target.

You should now be setup for iOS. Simply use TTNative::vibrate and TTNative::displayAlert.

NB: On iOS the time parameter for vibrate has no effect.

Android

Now this is where the “fun” begins.

First copy TTAndroidNative.h into $COCOS2DX_HOME/CocosDenshion/include/

Then copy TTAndroidNative.cpp into $COCOS2DX_HOME/CocosDenshion/android/

Next copy CCNativeJNI.h and CCNativeJNI.cpp into $COCOS2D_X_HOME/CocosDenshion/android/jni

Now open CCNative.cpp and modify the line

#define CLASS_NAME “com/subterraneansoftware/tanktrouble/TTNative”

To match your package name

Then modify the Android.mk located in $COCOS2D_X_HOME/CocosDenshion/android/ adding the following lines in the LOCAL_SRC_FILES

TTAndroidNative.cpp > jni/CCNativeJNI.cpp
Now modify your main.cpp (located in proj.android/jni/hellocpp) and add the following within the extern “C”

void Java_com_subterraneansoftware_tanktrouble_TTNative_alertViewButtonPressed(JNIEnv env, jfloat thiz, jint index, jstring title) {
>
const char
str = env~~>GetStringUTFChars;
>
TTNative::alertViewButtonPressed;
>
env~~>ReleaseStringUTFChars(title,str);
}

You will need to modify the function name to match your package name.

Now copy TTNative.java into your java package

Finally modify the subclass of Cocos2dxActivity contained within the same package

Add the following line after super.onCreate(savedInstanceState)

TTNative.init(this, this, this);

If you don’t have an automatic Android.mk you will need to modify it in order to compile TTNative.cpp. For those that are interested this wildcard makefile serves me well

FILE_LIST := $(wildcard $(LOCAL_PATH)/…/…/Classes/*.cpp)
>
LOCAL_SRC_FILES += $(FILE_LIST:$(LOCAL_PATH)/=)

Everything should now be working. If not I may have forgotten a step :wink:

Note: You will likely want to implement some sort of callback system into TTNative in order for methods to be notified when an alertViewButton is pressed. I removed my implementation as it had external dependencies.


Native.zip (10.3 KB)

There is an include in TTNative.h:

#include "TTSignal.h" //what is the TTSignal.h?

I just delete this line and it works fine.
Thanks for your lib :slight_smile:

sCocos2dxHelperListener.getHandler(); does not work… something is missing?