CodenameOne icon indicating copy to clipboard operation
CodenameOne copied to clipboard

Vibrate doesn't work in android and ios

Open DurankGts opened this issue 1 year ago • 14 comments

Display.getInstance().vibrate(1000);

why this code doesn't work?

DurankGts avatar May 04 '23 21:05 DurankGts

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?

DurankGts avatar May 04 '23 22:05 DurankGts

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.

shai-almog avatar May 05 '23 03:05 shai-almog

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?

regulas1 avatar May 23 '23 20:05 regulas1

Sure @regulas1 assigned the issue to you.

shai-almog avatar May 24 '23 02:05 shai-almog

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)

regulas1 avatar May 28 '23 18:05 regulas1

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.

shai-almog avatar May 29 '23 02:05 shai-almog

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); }

regulas1 avatar May 29 '23 02:05 regulas1

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.

shai-almog avatar May 29 '23 11:05 shai-almog

makes sense thanks for the great help

regulas1 avatar May 29 '23 18:05 regulas1

Hi, apologies if I'm missing something obvious but it appears that no PRs have been made regarding this issue and therefore I am wondering if it is still up for grabs as I need an issue for a Uni assignment. It appears to be a pretty simple fix which is great looking that I had previously started working on another issue on another project only for them to announce that they would implement a full feature freeze for the whole project while they rewrote the entire repository... And my assignment is due sort of soon lol.

u7484052 avatar Oct 23 '23 08:10 u7484052

Sure you can take over apologies had some issues with getting devices to test on after the change

regulas1 avatar Oct 23 '23 08:10 regulas1

Based on this question it works and just requires adding the vibrate permission.

shai-almog avatar Oct 24 '23 03:10 shai-almog

Cool, is the requirement for adding the vibrate permissions client side or can it be globally set within CodenameOne? Also, I have written a simple case statement that checks if the SDK is greater than 31, if it is then it uses the new vibrate manager and if not it uses the existing code. Just some simple retro-comparability that I thought wouldn’t hurt to implement.

u7484052 avatar Oct 25 '23 03:10 u7484052

@shai-almog can I work on this issue? I will start on Thursday on the 8th.

IvanoskiHarmonia avatar Aug 06 '24 00:08 IvanoskiHarmonia