简体   繁体   中英

Get user-defined vibration duration

We can trigger vibration on supported devices like so:

navigator.vibrate(50);

But does the API support any access to user-defaults? In other words, is it possible to get the vibration duration from the OS? So when a user would normally press a button when using the device, get that vibration duration (if any)?

Unfortunately, no, at least not in its current state (W3C Editor's Draft 27 June 2019)

You can read through the entire specification here ; it is pretty short.

Unfortunately as you can see there is only a single method defined, vibrate , so this API certainly will not let you get user-set defaults .

And reading further into the validate and normalize section that talks about the pattern, it looks like you cannot even trigger user defined-length patterns , you have to be explicit about your timings.

It may be possible that a later Draft (or a separate "User preferences API") will add functionality like the one you need, but I personally doubt it (because of reasons mentioned in comments).

This is currently not possible directly or indirectly.

Per the specification ,

Vibration API is not a source of data on its own and as such is not producing any data possible to consume on the Web.

Therefore, no data can be obtained via the vibration API directly.

However, the section also states

[I]t is known that it can serve as a source of events for other APIs. In particular, it is known that certain sensors such as accelerometers or gyroscopes are prone to tiny imperfections during their manufacturing. As such, they provide a fingerprinting surface that can be exploited utilizing the vibration stimuli generated via the Vibration API.

So in theory, yes, you could obtain the actual duration by using the accelerators and/or gyroscopes and transforming the data appropriately.

However, there's another caveat: per the WebIDL , a VibratePattern (defined as number | number[] ) must be provided as an argument. As such, you can't invoke vibration with the default .

I personally doubt this will be converted to an optional parameter in a future version of the specification due to privacy considerations (this very much allows for fingerprinting).

Keep in mind, though, that not all platforms even support vibration, and will return false when called. It's also perfectly valid to refuse action (which will also return false )

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM