Vibration (Xamarin.Essentials API of the Week)

Vibration (Xamarin.Essentials API of the Week)

>>In this week, Xamarin.Essential API of the Week where
I show you how to vibrate your device with a
Vibrate API. So tune in. [MUSIC]>>Welcome back everyone to the Xamarin.Essentials API of the Week. Today, I’m going to show you
how to vibrate your device using the Vibrate API built
into Xamarin.Essentials. So let’s get started.
Here I am inside of my App Essentials application
and I’ve installed Xamarin.Essentials into
all of my applications. I’ve followed
the getting started code and registered
Xamarin.Essentials everywhere. Over here in our documentation, I can learn about the setup here for Xamarin.Essentials and
the APIs that I need to use. So here, for instance, as
a full getting started guide, and it’s going to tell
me that if I want to vibrate my device on Android, I need to add a permission, on iOS nothing and same thing on UWP. Now what’s cool about
the Vibrate API, is that allows us to vibrate the device for
a specified amount of time. Additionally, you can cancel the vibration on
select devices such as, Windows and on Android. So let’s go ahead and
integrate this into our app. So since is on Android, I’m going to go into
our Android code. I’ve copied that permission
and I’m going to put it into our manifest file here. You can see I have
other permissions up here such as network state and
battery stats that I need. Now, what I’m able to do
is integrate Vibrate. So here I have a little slider
that allows me to specify the milliseconds between zero and 5,000 milliseconds or five seconds, and put a little vibrate. I can vibrate, I can cancel. So let’s go into the code
behind and actually do it. So what I’m going to do
here is simply come into my Vibrate method and
I can say, Vibration. which is the API. and I have Cancel and I have Vibrate. So here we’re going to
vibrate and I can give it a default here which
is 500 milliseconds. I can specify a duration which is between zero and 5,000
milliseconds or a TimeSpan. So for instance, I can say Timespan.fromMilliseconds and
say slider.sliderTime.value. That’s it, I’ve just
vibrated my device. If I want to cancel I
say Vibration.Cancel, and I’m done that’s literally it. Two lines of code, one to
start and one to cancel. Now, I’m going to go ahead
and recompile my application, and we’ll actually see our device
vibrate here on the podium. All right. Our application is
launched so I’m going to go into the vibrate option here
and I have a slider up top that I can drag along.
I’m going to hit “Vibrate”. Now, you can’t feel it or touch
it but rest assured it is vibrating in my hand
which is awesome. If hit ”Cancel” it cancels it. I can adjust it as much or
as little as I want right here in the slider and I get a beautiful nice
vibration of my device. So if I’m doing notifications
or anything like that, I can go and integrate
this into my application. Just like that you have
vibration in your app with Xamarin.Essentials
in a single line of code. I hope that you enjoyed this
Xamarin.Essential API of the Week. You can find more information
at You’d learn about this API and
all of the APIs built right in. I hope that you enjoyed this episode
and thanks for watching. [MUSIC].>>Hey, it’s James. I’m just checking in and wanted
to thank you for watching this awesome
Xamarin.Essential API of the Week. Don’t forget to do
all the things such as, like, subscribe, and ding that notification
bell for even more videos. While you’re here, check out more Xamarin.Essential API
videos of the Week. They’re all right here. Go do
it right now, it’s awesome.

4 thoughts on “Vibration (Xamarin.Essentials API of the Week)”

  1. James,
    Thank you for the awesome videos! I have two question for you.
    1st you mention this will only work with android and Windows, is that means the cancellation will not work with iOS?
    2nd in a lot of your great awesome videos you keep saying “WINDOWS”! For sure you are not talking about desktops, laptop, or even surface devices. So are you talking about future windows mobile OS. 😀
    Best regards.

Leave a Reply

Your email address will not be published. Required fields are marked *