Extern help required for android vibration

Monkey Targets Forums/Android/Extern help required for android vibration

Sensei(Posted 2014) [#1]
Hey guys,

The documentation is cryptic to me and I've also looked at S3nk's QRCode monkey and native .java files to try implement the android vibration functionality in my game.
I based it off diddy's android.java file https://code.google.com/p/diddy/source/browse/src/diddy/native/diddy.android.java

My java file (game.android.java) is as follows:
import android.os.Vibrator;

class _game {
	public static Vibrator vibrator;
	public static String inputString = "";
	
	public static void startVibrate(int millisec)
	{
		try {
			vibrator = (Vibrator)BBAndroidGame._androidGame._activity.getSystemService(Context.VIBRATOR_SERVICE);
			if (vibrator!=null)
				vibrator.vibrate(millisec);
		} catch (java.lang.SecurityException e) {
			android.util.Log.e("[Monkey]", "SecurityException: " + android.util.Log.getStackTraceString(e));
		}
	}

	public static void stopVibrate()
	{
		try {
			if (vibrator!=null)
				vibrator.cancel();
		} catch (java.lang.SecurityException e) {
			android.util.Log.e("[Monkey]", "SecurityException: " + android.util.Log.getStackTraceString(e));
		}
	}
}


And my monkey source is as follows:
Strict
Import mojo
Import "native/game.${TARGET}.${LANG}"
Extern
	'Class game = "_game"
	'	Function startVibrate:game(mill:Int)
	'	Function stopVibrate:game()
	'End
	Function startVibrate:Int(mill:Int) = "_game.startVibrate"
	Function stopVibrate:Int() = "_game.stopVibrate"
Public ' Back to monkey code


As can be seen above, I've tried the extern code in two ways. In the first commented out way, I tried calling it in my monkey source like so:
If Enemy.DetectHit(x, y)
  #If TARGET = "android"
    game.startVibrate(1000)
   #End
End


And in the latter implementation above, the same, but just using "startVibrate(1000)" without the game. preceding it.

I've tried it on my tablet and I get no vibration at all.

Anyone know what I am doing wrong?


therevills(Posted 2014) [#2]
You need to a permission to the AndroidManifest to make vibrate work:
<uses-permission android:name="android.permission.VIBRATE" />


(Also remember to add the MIT license when using Diddy code)


Sensei(Posted 2014) [#3]
Thanks Steve! I'll give it a go tonight. Don't worry, I'll include the licence 😀


therevills(Posted 2014) [#4]
Good luck :)

Also if you want to add the AndroidManifest stuff via code, in your MonkeyX code you can do something like this:
#ANDROID_MANIFEST_APPLICATION+="<activity android:name=~qcom.google.ads.AdActivity~q android:configChanges=~qkeyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize~q />"


So for the vibrate you could try:
#ANDROID_MANIFEST_MAIN+="<uses-permission android:name=~qandroid.permission.VIBRATE~q />"

As you need to add it to the "main" part of the manifest.


Sensei(Posted 2014) [#5]
Thanks a lot Steve. That worked a treat. I added it in the source as in your last example. Now to try get the Xbox 360 desktop controller to vibrate and the Ouya :)


Sensei(Posted 2014) [#6]
Well I was stupid enough to think that vibration is supported on other platforms. No vibration on Ouya, Vita or desktop. Not sure if I can get it to work for Xna builds yet but still looking into it.
If only there was a universal vibration option for all platforms :)