How to invoke vibrate device api

Hello~
i have question.

i find article
http://www.cocos2d-x.org/boards/6/topics/1040

how can i invoke the api from sdk directly?

for example. android sdk is java base. but cocos2d-x is c++.
i have no idea…

please give me hint. or some example!

I think you can refer the implementation of CocosDenshion.

you mean …2d-x\CocosDenshion\iphone\CocosDenshion.m file?

read that code is too difficult to me. what part is invoke sdk?

my eyes spinning! and head too.

On iOS, you can mix using c*+ and object-c, …2d-x\CocosDenshion\iphone\SimpleAudioEngine.mm file.
On andorid, you can invoke java code from c*+ through jni, …2d-x\CocosDenshion\android\jni\SimpleAudioEngineJni.cpp file.

Right, I decided I wanted to add vibration to my game so i decided to write some methods using jni to invoke the android sdk api and places them into cocos2dDenshin…

For anyone who wants to try it here’s what to do.

I edited 7 files in total. Here are the instructions of what to do:

  1. You must edit the AndroidManifest.xml file and add the following line:

This will allow your app to use vibration. So for example

<?xml version="1.0" encoding="utf-8"?>









  1. I modified 6 other files which you can replace from the zip. Here’s where each goes:

2.1 Both Cocos2dxActivity.java and Cocos2dxsound.java are generated in your project directory everytime you run the create-android-project.bat file. You must place Cocos2dxActivity.java and Cocos2dxsound.java and into 2dx\lib

The modifications I added were adding 3 methods; vibrate(long time), vibrateWithPattern(long[] pattern, int repeat), cancelVibrate().

2.2 Next two files are SimpleAudioEngineJni.h and SimpleAudioEngineJni.cpp. These two files go into
The modifications I added were adding and delcaring 3 methods; vibrateJNI(long long time), vibrateWithPatternJNI(long long pattern[], int repeat), cancelVibrateJNI().

2.3 The next file is SimpleAudioEngine.h. This goes into

The modifications I added were declaring 3 methods; vibrate(long long time), vibrateWithPattern(long long pattern[], int repeat), cancelVibrate()

2.4 The last file is SimpleAudioEngine.cpp. This goes into
The modifications I added were adding 3 methods; vibrate(long long time), vibrateWithPattern(long long pattern[], int repeat), cancelVibrate().

to add a vibration all you need to do is call the method like so: CocosDenshion::SimpleAudioEngine::sharedEngine()->vibrate(time);

the variable time is a long type integer and is a measurement of milliseconds.

the methods were created with intention of keeping the same structure of the vibrate methods from the android sdk: http://developer.android.com/reference/android/os/Vibrator.html.

Thanks for sharing Ray To !