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.
https://www.codenameone.com/
Other
1.7k stars 406 forks source link

Vibrate doesn't work in android and ios #3702

Open DurankGts opened 1 year ago

DurankGts commented 1 year ago

Display.getInstance().vibrate(1000);

why this code doesn't work?

DurankGts commented 1 year ago

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?

shai-almog commented 1 year ago

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.

regulas1 commented 1 year ago

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?

shai-almog commented 1 year ago

Sure @regulas1 assigned the issue to you.

regulas1 commented 1 year ago

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)

shai-almog commented 1 year ago

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.

regulas1 commented 1 year ago

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

shai-almog commented 1 year ago

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.

regulas1 commented 1 year ago

makes sense thanks for the great help

u7484052 commented 11 months ago

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.

regulas1 commented 11 months ago

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

shai-almog commented 11 months ago

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

u7484052 commented 11 months ago

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.

IvanoskiHarmonia commented 1 month ago

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