【问题标题】:Bluetooth requestDevice() is undefined on Amazon Lightsail蓝牙 requestDevice() 在 Amazon Lightsail 上未定义
【发布时间】:2018-11-17 21:37:30
【问题描述】:

以下示例 (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>

【问题讨论】:

  • 如果您通过 HTTPS 端点访问会发生什么?

标签: amazon-web-services bluetooth web-bluetooth


【解决方案1】:

正如@kdgregory 所建议的,navigator.bluetoothHTTPS only

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

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

【讨论】:

  • 我认为这可能是问题所在,但没有时间查看。很高兴看到您能够解决它,并写一个很好的答案来解释它。
猜你喜欢
  • 1970-01-01
  • 2021-12-20
  • 2021-03-22
  • 2020-09-25
  • 2021-03-20
  • 2018-05-25
  • 1970-01-01
  • 1970-01-01
  • 2021-09-04
相关资源
最近更新 更多