Search code examples
javascriptserverlocalhostnavigatorweb-bluetooth

Navigator.bluetooth does not exist


I have a simple page hosted on locally that uses the bluetooth webAPI. When opening the page through localhost the script works as intended but when I connect to the server through using my IP on my computer or another it doesn't work, The error I got was: Uncaught TypeError: Cannot read properties of undefined (reading 'requestDevice'). I tried logging the Navigator object and saw that it had noticeably less properties on the remove connection than on the localhost, one of the missing properties was Bluetooth. Why does the Navigator object has less properties on the remote connection? and is it possible to fix it?

How it looks like on localhost:

Navigator on localhost

How it looks like on remote connection: Navigator on remove connection


Solution

  • According to https://web.dev/bluetooth/#https-only. Web Bluetooth is only available in secure browsing contexts. It means you either have to serve your webpage over https:// or localhost such as http://127.0.0.1 or http://localhost.

    So it means you might want to always use https, and http://localhost is an exception for local development convenience.