getPorts() method return nothing

220 views
Skip to first unread message

Christian LeMoussel

unread,
Jul 21, 2021, 3:45:28 AM7/21/21
to Chromium Extensions
I'm new to Chrome Extension development. I am trying to develop an extension with the use of Web Serial.

If I use requestPort() method, like this :

    <button id="connect">Connect</button>

    <script>
        const connectButton = document.getElementById("connect");
        connectButton.addEventListener('click', async () => {
            try {
                const port = await navigator.serial.requestPort();
                console.log(port)
            } catch (e) {
                console.error(e)
            }
        });
    </script>

It's OK, to prompt the user for which device the site should be allowed to control.
    
    
But with getPorts() method, like this :

    <script>
        const connectButton = document.getElementById("connect");
        connectButton.addEventListener('click', async () => {
            try {
                const ports = await navigator.serial.getPorts();
                console.log(ports)
            } catch (e) {
                console.error(e)
            }
        });
    </script>

I got nothing with ports.length == 0   
Any idea what I didn't understand?

Jackie Han

unread,
Jul 21, 2021, 3:16:15 PM7/21/21
to Christian LeMoussel, Chromium Extensions
I did a simple test.
1. await navigator.serial.requestPort(); and select one
2. then await navigator.serial.getPorts(); will return that port, no problem

As the document says, getPorts return ports that were requested before. So if it didn't request, no ports would return.


--
You received this message because you are subscribed to the Google Groups "Chromium Extensions" group.
To unsubscribe from this group and stop receiving emails from it, send an email to chromium-extens...@chromium.org.
To view this discussion on the web visit https://groups.google.com/a/chromium.org/d/msgid/chromium-extensions/a2578421-816e-4268-be30-bd57b398096bn%40chromium.org.

Christian LeMoussel

unread,
Jul 22, 2021, 1:46:46 AM7/22/21
to Jackie Han, Chromium Extensions
Ok, I'll check it out. Thanks.
Reply all
Reply to author
Forward
0 new messages