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 please.
Android:
import android.os.Vibrator;
public class Brid { private Context context; private static Brid instance; public Brid() { this.instance = this; } public static Brid instance() { if(instance == null) { instance = new Brid(); } return instance; } public void setContext(Context context) { this.context = context; } public void Vibrate() { Vibrator v = (Vibrator) this.context.getSystemService(Context.VIBRATOR_SERVICE); v.vibrate(100); }
}
Unity3D:
public static void Vibrate(AndroidJavaObject Brid, AndroidJavaObject context) { if (Brid == null) { using (AndroidJavaClass activityClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer")) { context = activityClass.GetStatic<AndroidJavaObject>("currentActivity"); } using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.xxx.ultimatemobile.Brid")) { if (pluginClass != null) { Brid = pluginClass.CallStatic<AndroidJavaObject>("instance"); Brid.Call<AndroidJavaObject>("setContext", context); Brid.Call<AndroidJavaObject>("Vibrate"); } } } }
Button Event:
public void vibrate() { #if UNITY_ANDROID Bridge.Vibrate(null, null); #endif }
Thanks in advance...
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 <UnityInstallationDirecory>\Editor\Data\PlaybackEngines\AndroidPlayer\Apk
, Copy the AndroidManifest.xml
file to your <ProjectName>Assets\Plugins\Android
2.Now open the copied Manifest file from <ProjectName>Assets\Plugins\Android
and add <uses-permission android:name="android.permission.VIBRATE"/>
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):
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.unity3d.player" android:versionCode="1" android:versionName="1.0"> <uses-permission android:name="android.permission.VIBRATE"/> <application android:theme="@style/UnityThemeSelector" android:icon="@drawable/app_icon" android:label="@string/app_name" android:debuggable="true"> <activity android:name="com.unity3d.player.UnityPlayerActivity" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> <meta-data android:name="unityplayer.UnityActivity" android:value="true" /> </activity> </application> </manifest>
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<AndroidJavaObject>("currentActivity"); sysService = currentActivity.Call<AndroidJavaObject>("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<bool>("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(); }
My solution as follows.
Unity:
using (AndroidJavaClass pluginClass = new AndroidJavaClass("com.xx.ultimatemobile.Brid")) { if (pluginClass != null) { Brid = pluginClass.CallStatic<AndroidJavaObject>("instance"); Brid.Call("setContext", context); context.Call("runOnUiThread", new AndroidJavaRunnable(() => { Brid.Call("Vibrate"); })); } }
Also, I found Manifest.xml file example before 1 minute. Now the plugin is working.