codenameone / CodenameOne

Cross-platform framework for building truly native mobile apps with Java or Kotlin. Write Once Run Anywhere support for iOS, Android, Desktop & Web.

Home Page:https://www.codenameone.com/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Vibrate doesn't work in android and ios

DurankGts opened this issue · comments

commented

Display.getInstance().vibrate(1000);

why this code doesn't work?

commented

I just add this to
codename1.arg.android.xpermissions = <uses-permission android:name="android.permission.VIBRATE" />

and it actually work in android 11 but not in ios.

Any support about this?

It seems the code here should be updated based on this.

On iOS there's no guarantee vibrate will work as mentioned here. Relevant paragraph:

On some iOS devices, you can pass the kSystemSoundID_Vibrate constant to invoke vibration. On other iOS devices, calling this function with that constant does nothing.

This applies to this code.

So this should only be fixed for Android only as far as I can tell.

commented

Hi There May I take This Up ? I am new to Open Source so I want to try this one.
Please let me know if it good for me to work with?

Sure @regulas1
assigned the issue to you.

commented

Hi @shai-almog can you check if the code below is the correct fix ?

Replaced this
v = (Vibrator) getContext().getSystemService(Context.VIBRATOR_SERVICE);
With this
val v: Vibrator = (getSystemService(Context.VIBRATOR_SERVICE) as? VibratorManager)?.defaultVibrator ?: (getSystemService(Context.VIBRATOR_SERVICE) as Vibrator)

The second code is in Kotlin, not Java. So no it isn't correct. You need to adapt the Kotlin syntax to Java.
We only support Java 6 level syntax at the moment so you can't even use current syntax levels.

commented

Apologies.
may you please let me know if this is the fix?
Vibrator v; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) { VibratorManager vibratorManager = (VibratorManager) getSystemService("vibrator_manager"); v = vibratorManager.getDefaultVibrator(); } else { v = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE); }

I can't compile things in my head. I suggest making the changes and trying to compile them. The easiest way is to create a test application, generate a native Android project from that and try to debug this in Android Studio.

The code you included assumes this is an Activity but in Codename One our activity is typically a separate class from the implementation where this code resides. So you would need to make some changes to that code.

commented

makes sense thanks for the great help