Vibrator

Monkey Targets Forums/Android/Vibrator

Virtech(Posted 2011) [#1]
Is it possible to extend monkey so it can control the phones vibrator?

I tryed cooking some code myself, but it wont compile :(

I would appreciate if someone could have a look and spot any obvious mistakes...

vibandroid.java
// VibAndroid.java

import android.content.Context;
import android.os.Vibrator;

class VibAndroid
{
    static void Vibrate()
    {
        // See the docs for android.os.Vibrator for more info about the format of this array
        long SPEED_BASE = 100;
        long[] pattern = new long[] { SPEED_BASE, SPEED_BASE, SPEED_BASE };

        // Start the vibration
        Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(SPEED_BASE, -1);

    }
}


testvib.monkey
Import mojo
Import "VibAndroid.java"

Extern
	Function Vibrate:Void()="VibAndroid.Vibrate"

Public

Class Game Extends App
    Method OnCreate()
        SetUpdateRate 60
    End
    Method OnRender()
        Cls 30,30,30
        DrawText "TOUCH TO VIBRATE",0,0
        If MouseDown(0) Then Vibrate()
    End
End

Function Main()
    New Game
End



Uncle(Posted 2011) [#2]
Hi,

I was just looking at this because I fancied using the same function.


class VibAndroid
{
    static void Vibrate()
    {
	
        // Start the vibration
        android.os.Vibrator vibrator = (Vibrator) MonkeyGame.activity.getSystemService(android.content.Context.VIBRATOR_SERVICE);
        vibrator.vibrate(50);	
		
    }
}



Try swapping your vibandroid.java file to the above, and it should work.

Also I forgot to mention you will need to add the following permission in your manifest file :

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


TeaBoy(Posted 2011) [#3]
ah, I was just writing a feature for monkey to use the vibrator ;o), it's not completely finished as I wanted to add a bit more, but this is what I have so far.

If anyone can help me improve the code as I'm fairly new to Java myself, please do, so I can learn from it :o)

Happy to help!

monkeyvibrate.java

import android.os.Vibrator;
import android.content.Context;

class monkeyvibrate
{
  public static Vibrator vibrator;
  
  public static void StartVibrate(int millisec)
  {
	vibrator = (Vibrator)MonkeyGame.activity.getSystemService(Context.VIBRATOR_SERVICE);
	vibrator.vibrate(millisec);
  }
  
  public static void StopVibrate()
  {
    vibrator.cancel();
  }
}


monkeyvibrate.monkey

Import mojo
Import "monkeyvibrate.java"

Extern
Function StartVibrate:Void(millisec : Int) = "monkeyvibrate.StartVibrate"
Function StopVibrate:Void() = "monkeyvibrate.StopVibrate"

Public



MonkeyVibrateTest.monkey

Import mojo.app
Import mojo.graphics
Import mojo.input
Import monkeyvibrate

Class MonkeyVibrateTest Extends App
  Method OnCreate()
    SetUpdateRate 30
  End

  Method OnUpdate()
    If TouchHit(0) Then
      StartVibrate(1000)
    Endif
  End

  Method OnRender()
  End

End Class

Function Main()
  New MonkeyVibrateTest
End Function


as stated by Uncle, don't forget the

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

in the manifest file.


therevills(Posted 2011) [#4]
Cool... Thanks for this, Im sure I tried this earlier and it didnt work.

We've slightly altered it and adding it to Diddy:

	public static Vibrator vibrator;

	public static void startVibrate(int millisec)
	{
		try {
			vibrator = (Vibrator)MonkeyGame.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));
		}
	}


So now it wont display the permissions warning and it wont crash if you call StopVibrate before StartVibrate :)


TeaBoy(Posted 2011) [#5]
ah nice addition, I will commit your changes to memory ;) thanks!


jayparker(Posted 2011) [#6]
Is there any way to adjust strength of vibration?

[EDIT]
I found the answer :)
http://groups.google.com/group/android-developers/browse_thread/thread/10bcd0036c42f418


Bladko(Posted 2011) [#7]
i have HTC Desire Z and i can feel that vibration is comming from particular place from the phone (left side, top side etc) including strength of the vibro.

Can we do something similar here ? This really gives you great feeling of playing the "real" device not just the phone.

I think this might be related to device not to the software.


Snader(Posted 2013) [#8]
I am trying to implement vibration in my app. So I created the files as stated by Teaboy, but when building the MonkeyVibrateTest.monkey app, I get this:

-pre-compile:

-compile:
    [javac] Compiling 3 source files to D:\Programming\Projects\vibrate\MonkeyVibrateTest.build\android\bin\classes
    [javac] D:\Programming\Projects\vibrate\MonkeyVibrateTest.build\android\src\com\monkey\MonkeyGame.java:2581: error: cannot find symbol
    [javac] 	vibrator = (Vibrator)MonkeyGame.activity.getSystemService(Context.VIBRATOR_SERVICE);
    [javac] 	                               ^
    [javac]   symbol:   variable activity
    [javac]   location: class MonkeyGame
    [javac] Note: D:\Programming\Projects\vibrate\MonkeyVibrateTest.build\android\src\com\monkey\MonkeyGame.java uses unchecked or unsafe operations.
    [javac] Note: Recompile with -Xlint:unchecked for details.
    [javac] 1 error

BUILD FAILED
d:\android-sdk\tools\ant\build.xml:710: The following error occurred while executing this line:
d:\android-sdk\tools\ant\build.xml:723: Compile failed; see the compiler error output for details.

Total time: 2 secondsTRANS FAILED: Android build failed.

Abnormal program termination. Exit code: -1


I have no clue what to do...


therevills(Posted 2013) [#9]
That's because Monkey has changed quite a bit since these posts were made. Check out how I updated Diddy to make it work with the new version:

http://code.google.com/p/diddy/source/browse/src/diddy/native/diddy.android.java

Basically, MonkeyGame.activity is no more and you have to use BBAndroidGame._androidGame._activity instead.


Snader(Posted 2013) [#10]
Thank you Therevills. Got it working now!