I am working on a simple plugin. Now I am trying to add a vibrate property. But this code is not working. Where am I going wrong? My code is as follows. Can you help me plea
I see you have a solution. For those who don't want to use Java. You can do this from C# only.
1.Go to , Copy the AndroidManifest.xml file to your
2.Now open the copied Manifest file from and add to it. Save, Build and Run. If this is a permission problem, that should now be solved.
What your AndroidManifest.xml should look like(Unity 5.4):
You don't even need to write Java plugin for simple vibration. This can be done with Unity's AndroidJavaClass and AndroidJavaObject classes.
Complete Vibration Plugin without Java.
using UnityEngine;
using System.Collections;
public class Vibrate
{
public AndroidJavaClass unityPlayer;
public AndroidJavaObject currentActivity;
public AndroidJavaObject sysService;
public void Vibrate()
{
unityPlayer = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
currentActivity = unityPlayer.GetStatic("currentActivity");
sysService = currentActivity.Call("getSystemService", "vibrator");
}
//Functions from https://developer.android.com/reference/android/os/Vibrator.html
public void vibrate()
{
sysService.Call("vibrate");
}
public void vibrate(long milliseconds)
{
sysService.Call("vibrate", milliseconds);
}
public void vibrate(long[] pattern, int repeat)
{
sysService.Call("vibrate", pattern, repeat);
}
public void cancel()
{
sysService.Call("cancel");
}
public bool hasVibrator()
{
return sysService.Call("hasVibrator");
}
}
Usage:
Vibrate vibrate = new Vibrate();
if (vibrate.hasVibrator())
{
//Vibrate
vibrate.vibrate();
//Vibrate for 500 milliseconds
vibrate.vibrate(500);
// Start without a delay
// Vibrate for 200 milliseconds
// Sleep for 2000 milliseconds
long[] pattern = { 0, 200, 2000 };
vibrate.vibrate(pattern, 0);
//Cancel Vibration
vibrate.cancel();
}