2

以下示例 (1) 列出了在 Chrome 版本 70 中作为文件打开时可用的蓝牙设备。

将完全相同的示例上传到 Amazon Ligtsail 实例时,我收到以下错误消息:

Uncaught TypeError: Cannot read property 'requestDevice' of undefined
at connect ((index):9)
at HTMLButtonElement.onclick ((index):4)

谁能向我解释可能是什么问题?由于这是 JavaScript,它应该在浏览器中执行并且与任何服务器设置无关?

.

(1) 在 Amazon Lightsail 上生成错误的示例代码

<html>
<body>
<button onclick="connect();">Click here</button>
<script>
function connect() {
    console.log('Getting Bluetooth Device...');
    navigator.bluetooth.requestDevice(
    {
        acceptAllDevices : true
    })
    .then(device => {
        console.log('Got device');
    })
     .catch(error => {
        console.log(error);
    });
 }
</script>
</body>
</html>
4

1 回答 1

4

正如@kdgregory 建议的那样,仅navigator.bluetoothHTTPS

由于此实验性 API 是添加到 Web 的一项强大的新功能,因此 Google Chrome 旨在使其仅可用于安全上下文。这意味着您需要在构建时考虑到 TLS。

我添加了一个GitHub 页面,它可以工作。

在此处输入图像描述

于 2018-11-15T16:06:50.970 回答