androidwear-ossamsung-galaxy-watch-4

Vibrator calls not causing watch to vibrate while watch is in ambient(?) mode


I've been trying to write a simple vibration app for my Samsung Galaxy 4 watch but am having an issue. The watch will not vibrate when the screen is off. It will only vibrate when my application is open. Any ideas why this is? Here is some of my code:

// MainActivity
Intent i = new Intent(buttonView.getContext(), AlarmService.class);
startService(i);
// AlarmService
Alarm alarm = new Alarm();
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
    alarm.setAlarm(this);
}
// Alarm
public void setAlarm(Context context)
{
    AlarmManager am =( AlarmManager)context.getSystemService(Context.ALARM_SERVICE);
    Intent i = new Intent(context, Alarm.class);
    PendingIntent pi = PendingIntent.getBroadcast(context, 0, i, 0);
    long mills = getMillsForNextThirtySeconds();
    am.setExact(AlarmManager.RTC_WAKEUP, mills, pi);
}

@Override
public void onReceive(Context context, Intent intent)
{
    PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
    PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "szelag:test");

    wl.acquire();
    Vibrator vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
    VibrationEffect effect = VibrationEffect.createOneShot(1500, 255);
    
    // Watch never vibrates when it is in ambient(?) mode.
    vibrator.vibrate(effect);
    wl.release();
}

Thank you!


Solution

  • See docs here https://developer.android.com/reference/android/os/VibratorManager

    The app should be in foreground for the vibration to happen.

    Maybe relevant Android Play Vibration from Foreground Service