Web 蓝牙:如何检查 javascript 是否启用了蓝牙?

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

是否可以通过 javascript 检查蓝牙是否已启用,是否有任何事件可以检测到它的变化?

Web蓝牙中有getAvailability()方法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

但如前所述,蓝牙无线电的供电状态不会影响可用性,它不会对计算机设置中的蓝牙关闭做出反应。

正确,getAvailability() 只告诉您系统是否有蓝牙无线电。

没有特定的 API 可以告诉站点无线电是打开还是关闭。如果调用 requestDevice() 并且用户关闭了无线电,他们将在 Chrome 的设备选择器中收到相关通知。他们可以选择取消提示或打开蓝牙设置以启用它。无论哪种方式,requestDevice() 都会拒绝并报错,直到用户在设置中启用蓝牙。