using Vibration

Is there a way to use the Vibrator class?

Or what do I need to do to get following to work:

public void vibrate(int duration)

 {
    Vibrator vibs = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
    vibs.vibrate(duration);    
 }

Now it is compiling it without any erros…

public void vibrate(int duration)
 {
    if (defined(Android)) {
        Vibrator vibs = (Vibrator) Android.android.content.Context.VIBRATOR_SERVICE_GET_4135();
        vibs.vibrate(duration);
    }
 }

But on the Smartphone: This function crashes the application…

And this isn’t working too(… non-static method … getSystemService … ), normally this wouldn’t be a big problem, but here i don’t know how to solve it.

if (defined(Android)) {
        Vibrator vibs = (Vibrator) Android.android.content.Context.getSystemService(Android.android.content.Context.VIBRATOR_SERVICE);
        vibs.vibrate(duration);
    }

No one can help me with this?

Creating a “Vibrator Object” and use it on Android? (need it for an Referee-App for Tablesoccer-Competitions :slight_smile: )

Hi!

I don’t know why your code is crashing. Can you please check if you are actually getting a Vibrator object out before calling .vibrate(duration) on it?

if (vibs == null)
    debug_log "Vibrator is null";
else
    vibs.vibrate(duration);

Then we will be one step closer to findig out why it doesn’t work. 't

Thanks for your help.

When I just call this (App is on the Smartphone), the App crashes…

if (defined(Android)) {
        Vibrator vibs = (Vibrator) Android.android.content.Context.VIBRATOR_SERVICE_GET_4135();
    }

Sorry about the issue and thanks for digging into it. I’ve raised an issue for this and I’ll keep you posted

Thanks :slight_smile:

Well to start, thankyou. Your issues found a sneaky little bug in the java->uno typecast logic. We will try to get the fix out in the next release (I just need to run the some tests on other projects).

There was one missing part to your example that would also cause a crash and that is that android forces you to run many UI and system related tasks on a thread where a Looper is running.

In most cases people use the uiThread for this via the runOnUiThread method.

See the following for the code I used to test this issue.

using Uno;
using Android.Base;
using Android.Runtime;
using Android.android.os;
using Android.android.app;
using Android.android.content;

namespace Vibrate
{
    public class Vibrate : Uno.Application
    {
        public Vibrate()
        {
            Uno.Application.Current.Window.PointerPressed += Press;
            Permissions.RequestPermission(Permissions.VIBRATE);
        }

        public void Press(object sender, Uno.Platform.PointerEventArgs args)
        {
            var runnable = UnoHelper.RunnableFromAction(Buzz);
            Activity.GetAppActivity().runOnUiThread(runnable);
        }

        private void Buzz()
        {
            var v = (Vibrator) Activity.GetAppActivity().getSystemService(Context.VIBRATOR_SERVICE);
            v.vibrate(500);
        }

        public override void Draw()
        {
            ClearColor = float4(1, 0, 1, 1);
        }
    }
}