Explain Codes LogoExplain Codes Logo

How to make an Android device vibrate? with different frequency?

java
haptic-feedback
vibration-patterns
android-development
Alex KataevbyAlex Kataev·Feb 19, 2025
TLDR

To make a device vibrate, first import the Vibrator service with getSystemService(Context.VIBRATOR_SERVICE). If operating on API 26 and above (Oreo), utilize VibrationEffect.createWaveform for customized vibration patterns and amplitude control:

Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { VibrationEffect effect = VibrationEffect.createWaveform(new long[]{0, 100, 1000, 300}, new int[]{0, 255, 0, 255}, -1); vibrator.vibrate(effect); } else { vibrator.vibrate(500); // Vibrate for a fixed 500ms, because oldies like it steady }

Remember to include the vibration permission in your manifest:

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

If on pre-Oreo devices, unfortunately, you're stuck with fixed-duration vibrations with no amplitude control.

Device Compatibility Check

Before jumping into vibration patterns, it's important to ensure that the device supports vibration:

if (vibrator.hasVibrator()) { Log.v("Vibration", "Device is ready to party!"); } else { Log.v("Vibration", "Party crasher! Device cannot vibrate."); }

This check helps to gracefully degrade the experience if vibration is n/a.

Crafting the User Experience

Quick Feedback Responses

For instant tactile feedback, like for button clicks, the HapticFeedbackConstants class in Kotlin can be used without the need to add extra permissions:

button.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS);

Handling Complex Vibration Patterns

You can juggle around "vibration and pause" durations to create complex vibration patterns:

long[] MacyGray = {0, 400, 100, 300, 100, 200}; // The title track of her 'Vibrate' album int[] amplitudes = {VibrationEffect.DEFAULT_AMPLITUDE, 255, 100, 255, 100, 200}; VibrationEffect customEffect = VibrationEffect.createWaveform(MacyGray, amplitudes, -1); vibrator.vibrate(customEffect);

User Comfort Comes First

While vibrations can be a powerful UX tool, like Natasha Bedingfield said, it's all about balance. Avoid excessive vibration that may end up annoying your users.

Best Practices and Considerations

App Permissions and Compatibility

Ensure compatibility across Android versions by checking API levels and declaring VIBRATE permission in the app's AndroidManifest.xml:

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

Cross-check the permission at runtime:

if (ContextCompat.checkSelfPermission(context, Manifest.permission.VIBRATE) != PackageManager.PERMISSION_GRANTED) { // Woops! Permission not granted, fallback plan needed! }

User's Preference Matters

Don't forget, users have different preferences for feedback intensity. Creating options for users for customized amplitude or pattern is always a winner move.

Wearbles and IoT

Explore wearables and IoT devices with haptic capabilities. Most principles remain the same, but with extra fun on device connectivity and user engagement.