Proximity API is available in mobile browsers only that’s because this API require sensors to measure distance which is available only on mobile phones.
Proximity API is used to detect if an object is nearby the device. For example: If you want to play music just by touching the phone then this is useful or if you want the device to make a call when you take the device near your ear. There can be many other use cases.
Using proximity API you can find the distance between the nearby object, ofcourse you cannot find what the object is?
Let’s have a look at the API:
{
// API supported.
//this event is fired when an object is comes near or goes far away from the device.
window.addEventListener('deviceproximity', function(event) {
console.log("An object is " + event.value + " centimeters far away");
console.log("Minimum distance sensor can detect is " + event.min + " centimeters");
console.log("Maximum distance sensor can detect is " + event.max + " centimeters");
});
//this event is fired when an object is comes near or goes far away from the device. Its same as deviceproximity but the callback gets a different arguement.
window.addEventListener('userproximity', function(event) {
if(event.near == true)
{
console.log("Object is nearer to the device");
}
else
{
console.log("Object is not nearer to the device");
}
});
}
else
{
// API not supported
console.log("Device Proximity API");
}
Leave a Reply