Interhaptics SDK for Unity 1.6
Loading...
Searching...
No Matches
Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction Class Reference

Classes

class  PredefinedEffect
 

Public Types

enum  logLevel { Disabled , Info , Warning }
 

Static Public Member Functions

static void Initialize ()
 
static void Vibrate (long milliseconds, int amplitude=-1, bool cancel=false)
 Vibrate for Milliseconds, with Amplitude (if available). If amplitude is -1, amplitude is Disabled. If -1, device DefaultAmplitude is used. Otherwise, values between 1-255 are allowed. If 'cancel' is true, Cancel() will be called automatically.
 
static void Vibrate (long[] pattern, int[] amplitudes=null, int repeat=-1, bool cancel=false)
 Vibrate Pattern (pattern of durations, with format Off-On-Off-On and so on). Amplitudes can be Null (for default) or array of Pattern array length with values between 1-255. To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating. If 'cancel' is true, Cancel() will be called automatically.
 
static void VibratePredefined (int effectId, bool cancel=false)
 Vibrate predefined effect (described in Vibration.PredefinedEffect). Available from Api Level >= 29. If 'cancel' is true, Cancel() will be called automatically.
 
static float PulseFromBuffer (int[] _buffer)
 
static long[] ParsePattern (string pattern)
 
static long[] TimingsFromAmplitudes (int[] amplitudes, long amplitudeDuration)
 
static int GetApiLevel ()
 Returns Android Api Level.
 
static int GetDefaultAmplitude ()
 Returns Default Amplitude of device, or 0.
 
static bool HasVibrator ()
 Returns true if device has vibrator.
 
static bool HasAmplitudeControl ()
 Return true if device supports amplitude control.
 
static void Cancel ()
 Tries to cancel current vibration.
 

Static Public Attributes

static logLevel LogLevel = logLevel.Warning
 
static int AmplitudeThreshold = 100
 

Detailed Description

Definition at line 9 of file GenericAndroidHapticAbstraction.cs.

Member Enumeration Documentation

◆ logLevel

Member Function Documentation

◆ Cancel()

◆ GetApiLevel()

static int Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.GetApiLevel ( )
static

Returns Android Api Level.

◆ GetDefaultAmplitude()

static int Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.GetDefaultAmplitude ( )
static

Returns Default Amplitude of device, or 0.

◆ HasAmplitudeControl()

static bool Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.HasAmplitudeControl ( )
static

Return true if device supports amplitude control.

Definition at line 318 of file GenericAndroidHapticAbstraction.cs.

319 {
320 if (HasVibrator() && doesSupportVibrationEffect())
321 {
322 return vibrator.Call<bool>("hasAmplitudeControl"); // API 26+ specific
323 }
324 else
325 {
326 return false; // no amplitude control below API level 26
327 }
328 }

References Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.HasVibrator().

Referenced by Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Vibrate(), and Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Vibrate().

◆ HasVibrator()

◆ Initialize()

static void Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Initialize ( )
static

Definition at line 31 of file GenericAndroidHapticAbstraction.cs.

32 {
33 // load references safely
34 if (isInitialized == false && UnityEngine.Application.platform == UnityEngine.RuntimePlatform.Android)
35 {
36 // Add APP VIBRATION PERMISSION to the Manifest
37 #if UNITY_ANDROID
38 if (UnityEngine.Application.isConsolePlatform)
39 {
40 UnityEngine.Handheld.Vibrate();
41 }
42 #endif
43 // Get Api Level
44 using (UnityEngine.AndroidJavaClass androidVersionClass = new UnityEngine.AndroidJavaClass("android.os.Build$VERSION"))
45 {
46 ApiLevel = androidVersionClass.GetStatic<int>("SDK_INT");
47 }
48
49 // Get UnityPlayer and CurrentActivity
50 using (UnityEngine.AndroidJavaClass unityPlayer = new UnityEngine.AndroidJavaClass("com.unity3d.player.UnityPlayer"))
51 using (UnityEngine.AndroidJavaObject currentActivity = unityPlayer.GetStatic<UnityEngine.AndroidJavaObject>("currentActivity"))
52 {
53 if (currentActivity != null)
54 {
55 vibrator = currentActivity.Call<UnityEngine.AndroidJavaObject>("getSystemService", "vibrator");
56
57 // if device supports vibration effects, get corresponding class
58 if (doesSupportVibrationEffect())
59 {
60 vibrationEffectClass = new UnityEngine.AndroidJavaClass("android.os.VibrationEffect");
61 defaultAmplitude = UnityEngine.Mathf.Clamp(vibrationEffectClass.GetStatic<int>("DEFAULT_AMPLITUDE"), -1, 255);
62 }
63
64 // if device supports predefined effects, get their IDs
65 if (doesSupportPredefinedEffect())
66 {
67 PredefinedEffect.EFFECT_CLICK = vibrationEffectClass.GetStatic<int>("EFFECT_CLICK");
68 PredefinedEffect.EFFECT_DOUBLE_CLICK = vibrationEffectClass.GetStatic<int>("EFFECT_DOUBLE_CLICK");
69 PredefinedEffect.EFFECT_HEAVY_CLICK = vibrationEffectClass.GetStatic<int>("EFFECT_HEAVY_CLICK");
70 PredefinedEffect.EFFECT_TICK = vibrationEffectClass.GetStatic<int>("EFFECT_TICK");
71 }
72 }
73 }
74
75 logAuto("Vibration component initialized", logLevel.Info);
76 isInitialized = true;
77 }
78 }

Referenced by Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Vibrate(), Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Vibrate(), and Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.VibratePredefined().

◆ ParsePattern()

static long[] Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.ParsePattern ( string pattern)
static

Definition at line 237 of file GenericAndroidHapticAbstraction.cs.

238 {
239 if (pattern == null)
240 {
241 return new long[0];
242 }
243 pattern = pattern.Trim();
244 string[] split = pattern.Split(',');
245
246 long[] timings = new long[split.Length];
247 for (int i = 0; i < split.Length; i++)
248 {
249 if (int.TryParse(split[i].Trim(), out int duration))
250 {
251 timings[i] = duration < 0 ? 0 : duration;
252 }
253 else
254 {
255 timings[i] = 0;
256 }
257 }
258
259 return timings;
260 }

◆ PulseFromBuffer()

static float Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.PulseFromBuffer ( int[] _buffer)
static

Definition at line 221 of file GenericAndroidHapticAbstraction.cs.

222 {
223 if (_buffer.Length > 0)
224 {
225 float result = 0;
226 for (int i = 0; i < _buffer.Length; i++)
227 {
228 result += _buffer[i];
229 }
230
231 return UnityEngine.Mathf.Clamp(result / _buffer.Length, 0, 255);
232 }
233
234 return 0;
235 }

◆ TimingsFromAmplitudes()

static long[] Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.TimingsFromAmplitudes ( int[] amplitudes,
long amplitudeDuration )
static

Definition at line 262 of file GenericAndroidHapticAbstraction.cs.

263 {
264 if (amplitudes == null)
265 {
266 return new long[] { 0, 0 };
267 }
268
269 System.Collections.Generic.List<long> timings = new System.Collections.Generic.List<long>();
270
271 int currentAmplitudeIndex = 0;
272 long currentTimingValue = 0;
273
274 while (currentAmplitudeIndex < amplitudes.Length)
275 {
276 //get duration under threshold
277 currentTimingValue = 0;
278 while (currentAmplitudeIndex < amplitudes.Length && amplitudes[currentAmplitudeIndex] < AmplitudeThreshold)
279 {
280 currentTimingValue += amplitudeDuration;
281 currentAmplitudeIndex++;
282 }
283 timings.Add(currentTimingValue);
284
285 //get duration over threshold
286 currentTimingValue = 0;
287 while (currentAmplitudeIndex < amplitudes.Length && amplitudes[currentAmplitudeIndex] >= AmplitudeThreshold)
288 {
289 currentTimingValue += amplitudeDuration;
290 currentAmplitudeIndex++;
291 }
292 timings.Add(currentTimingValue);
293 }
294
295 return timings.ToArray();
296 }

References Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.AmplitudeThreshold.

Referenced by Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Vibrate().

◆ Vibrate() [1/2]

static void Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Vibrate ( long milliseconds,
int amplitude = -1,
bool cancel = false )
static

Vibrate for Milliseconds, with Amplitude (if available). If amplitude is -1, amplitude is Disabled. If -1, device DefaultAmplitude is used. Otherwise, values between 1-255 are allowed. If 'cancel' is true, Cancel() will be called automatically.

Definition at line 87 of file GenericAndroidHapticAbstraction.cs.

88 {
89 string funcToStr() => string.Format("Vibrate ({0}, {1}, {2})", milliseconds, amplitude, cancel);
90 Initialize(); // make sure script is initialized
91 if (isInitialized == false)
92 {
93 logAuto(funcToStr() + ": Not initialized", logLevel.Warning);
94 }
95 else if (HasVibrator() == false)
96 {
97 logAuto(funcToStr() + ": Device doesn't have Vibrator", logLevel.Warning);
98 }
99 else
100 {
101 if (cancel)
102 {
103 Cancel();
104 }
105 if (doesSupportVibrationEffect())
106 {
107 // validate amplitude
108 amplitude = UnityEngine.Mathf.Clamp(amplitude, -1, 255);
109 if (amplitude == -1)
110 {
111 amplitude = 255; // if -1, disable amplitude (use maximum amplitude)
112 }
113 if (amplitude != 255 && HasAmplitudeControl() == false)
114 { // if amplitude was set, but not supported, notify developer
115 logAuto(funcToStr() + ": Device doesn't have Amplitude Control, but Amplitude was set", logLevel.Warning);
116 }
117 //if (amplitude == 0) amplitude = defaultAmplitude; // if 0, use device DefaultAmplitude
118
119 // if amplitude is not supported, use 255; if amplitude is -1, use systems DefaultAmplitude. Otherwise use user-defined value.
120 amplitude = HasAmplitudeControl() == false ? 255 : amplitude;
121 vibrateEffect(milliseconds, amplitude);
122 logAuto(funcToStr() + ": Effect called", logLevel.Info);
123 }
124 else
125 {
126 vibrateLegacy(milliseconds);
127 logAuto(funcToStr() + ": Legacy called", logLevel.Info);
128 }
129 }
130 }
static bool HasAmplitudeControl()
Return true if device supports amplitude control.

References Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Cancel(), Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.HasAmplitudeControl(), Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.HasVibrator(), and Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Initialize().

◆ Vibrate() [2/2]

static void Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Vibrate ( long[] pattern,
int[] amplitudes = null,
int repeat = -1,
bool cancel = false )
static

Vibrate Pattern (pattern of durations, with format Off-On-Off-On and so on). Amplitudes can be Null (for default) or array of Pattern array length with values between 1-255. To cause the pattern to repeat, pass the index into the pattern array at which to start the repeat, or -1 to disable repeating. If 'cancel' is true, Cancel() will be called automatically.

Definition at line 138 of file GenericAndroidHapticAbstraction.cs.

139 {
140 string funcToStr() => string.Format("Vibrate (pattern, amplitudes, repeat, cancel)");
141
142 Initialize(); // make sure script is initialized
143 if (isInitialized == false)
144 {
145 logAuto(funcToStr() + ": Not initialized", logLevel.Warning);
146 }
147 else if (HasVibrator() == false)
148 {
149 logAuto(funcToStr() + ": Device doesn't have Vibrator", logLevel.Warning);
150 }
151 else
152 {
153 // check Amplitudes array length
154 if (amplitudes != null && amplitudes.Length != pattern.Length)
155 {
156 logAuto(funcToStr() + ": Length of Amplitudes array is not equal to Pattern array. Amplitudes will be ignored.", logLevel.Warning);
157 amplitudes = null;
158 }
159 // limit amplitudes between 1 and 255
160 if (amplitudes != null)
161 {
162 clampAmplitudesArray(amplitudes);
163 }
164
165 // vibrate
166 if (cancel)
167 {
168 Cancel();
169 }
170 if (doesSupportVibrationEffect())
171 {
172 if (amplitudes != null && HasAmplitudeControl() == true)
173 {
174 //Case High Capabilities
175 vibrateEffect(pattern, amplitudes, repeat);
176 logAuto(funcToStr() + ": Effect with amplitudes called", logLevel.Info);
177 return;
178 }
179 }
180
181 logAuto(funcToStr() + ": called but device has no amplitude control", logLevel.Warning);
182 //Case Medium & low capabilities
183 vibrateEffect(TimingsFromAmplitudes(amplitudes, pattern[1]), repeat);
184 }
185 }
static long[] TimingsFromAmplitudes(int[] amplitudes, long amplitudeDuration)

References Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Cancel(), Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.HasAmplitudeControl(), Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.HasVibrator(), Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Initialize(), and Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.TimingsFromAmplitudes().

◆ VibratePredefined()

static void Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.VibratePredefined ( int effectId,
bool cancel = false )
static

Vibrate predefined effect (described in Vibration.PredefinedEffect). Available from Api Level >= 29. If 'cancel' is true, Cancel() will be called automatically.

Definition at line 191 of file GenericAndroidHapticAbstraction.cs.

192 {
193 string funcToStr() => string.Format("VibratePredefined ({0})", effectId);
194
195 Initialize(); // make sure script is initialized
196 if (isInitialized == false)
197 {
198 logAuto(funcToStr() + ": Not initialized", logLevel.Warning);
199 }
200 else if (HasVibrator() == false)
201 {
202 logAuto(funcToStr() + ": Device doesn't have Vibrator", logLevel.Warning);
203 }
204 else if (doesSupportPredefinedEffect() == false)
205 {
206 logAuto(funcToStr() + ": Device doesn't support Predefined Effects (Api Level >= 29)", logLevel.Warning);
207 }
208 else
209 {
210 if (cancel)
211 {
212 Cancel();
213 }
214 vibrateEffectPredefined(effectId);
215 logAuto(funcToStr() + ": Predefined effect called", logLevel.Info);
216 }
217 }

References Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Cancel(), Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.HasVibrator(), and Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.Initialize().

Member Data Documentation

◆ AmplitudeThreshold

int Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.AmplitudeThreshold = 100
static

◆ LogLevel

logLevel Interhaptics.Platforms.Mobile.GenericAndroidHapticAbstraction.LogLevel = logLevel.Warning
static

Definition at line 12 of file GenericAndroidHapticAbstraction.cs.


The documentation for this class was generated from the following file: