Skip to content
Advertisement

Web Bluetooth: how to check if bluetooth is enabled with javascript?

Is it possible to check if bluetooth is enabled with javascript and is there any event to detect its change?

There is getAvailability() method in Web Bluetooth API:

getAvailability() returns a promise that resolves with true if the user agent is running on a system that has a Bluetooth radio and false otherwise. The powered state of the Bluetooth radio does not influence the availability because applications can prompt users to turn on the radio using requestDevice(). The availabilitychanged event can be used to detect changes in the availability of the Bluetooth radio.

This API is useful for developers to know whether they should show UI for Web Bluetooth or not to the user.

Web Bluetooth / Availability Sample

But as described, powered state of the Bluetooth radio does not influence the availability, it does not react on bluetooth turning off from computer settings.

Advertisement

Answer

Correct, getAvailability() only tells you if the system has a Bluetooth radio or not.

There isn’t a specific API that will tell the site if the radio is turned on or off. If requestDevice() is called and the user has the radio off, they will be notified about it in the device chooser in Chrome. They have the option to cancel the prompt or have Bluetooth settings open up for them to enable it. Either way, requestDevice() will reject with an error until Bluetooth is enabled in settings by the user.

Advertisement