Marcell
Marcell

Reputation: 111

Web Bluetooth & Chrome Extension: User cancelled the requestDevice() chooser

I'm getting the following error when trying to open the Web Bluetooth device selector dialog from a Google Chrome extension popup: DOMException: User cancelled the requestDevice() chooser.

I'm using TypeScript and browserify with ES2020 target, module and lib. Here is the code that runs from my popup.html

document.getElementById("test-button")?.addEventListener("click", async () => {
    await navigator.bluetooth
        .requestDevice({ acceptAllDevices: true })
        .then((device) => {
            alert(device.name);
        })
        .catch((error) => {
            console.error(error); // <- Getting the error here!
        });
});

I'm assuming there's some Chrome extension magic that tricks the web-bluetooth into thinking that I clicked away from the dialog, but have no idea how to get around this. Any idea?

Upvotes: 0

Views: 1013

Answers (1)

Fran&#231;ois Beaufort
Fran&#231;ois Beaufort

Reputation: 5629

As discussed in https://bugs.chromium.org/p/chromium/issues/detail?id=994185, Web Bluetooth is NOT supported in Chrome Extensions popup windows.

It is supported however in a Chrome Extension "tab/options" page as it acts as a regular tab.

I haven't tried to reproduce this yet but I believe the issue is that Web Bluetooth doesn't have code to handle creating a permission prompt as a child of an extension popup window. It works for the options page because it is a normal Chrome tab and would also work for a standalone app window.

My suggestion would be to open a Chrome Extension regular page that contains some code to interact with nearby Bluetooth devices.

// popup.js

button.addEventListener("click", function() {
  // Open a page that contains Javascript to handle Bluetooth devices.
  chrome.tabs.create({ url: "page.html" });
});

Upvotes: 2

Related Questions