google-chrome - chrome 和蓝牙 - 连接、发现 - 如何使其正常工作?

标签 google-chrome web bluetooth

我正在研究通过网页发现附近蓝牙设备的项目。

这是我的限制: - 在 windows 7 或 10 下开发,在手机上使用 android 5 - 外设是蓝牙信标 - 中央是装有 android 6 的手机

首先我制作了一个如下所示的页面:

<html>
<head>
  <script src="jquery-3.1.1.js"></script>
</head>
<body>

<div id="target" style="width: 100px; height: 100px; border: 1px solid red"></div>

<script>
$('#target').click(function() { 
  chrome.bluetooth.startDiscovery(function () { 
  navigator.bluetooth.requestDevice({ filters: [{ services:['battery_service'] }] })
  .then(device => {

    console.log(device);
  return device.gatt.connect();
  }).then(server => { console.log(server)}).catch(error  => { console.log(error); });
})
</script>
</body>
</html>

这段代码让我可以将蓝牙设备与名为“battery_service”的服务与网页配对。它正在工作(morover) - 它打开“chrome 系统”窗口以搜索附近的设备并找到一个,如果我已经在 Windows 中将它配对的话。如果它之前没有配对,它将一无所获。 Chrome 说 gatt 服务器不存在,所以我无法连接它,并且对象具有 gatt 属性,gatt 属性具有 gatt 属性(依此类推到无穷大)

无论如何它都会返回包含device.namedevice.id 属性的device 对象。其他属性未定义

我在互联网上找到的任何示例都没有对我有用。

所以这里有问题:

  1. 是否可以使用windows作为开发平台?
  2. 可以用android 5做蓝牙外设吗?
  3. chrome.bluetooth 对象和 navigator.bluetooth 对象有什么区别?每个教程都使用 chrome.bluetooth 但是它在我的 chrome 浏览器中是 undefined object 。 Navigator.bluetooth 如前所述工作。
  4. 我找到了 chrome.bluetooth.startDiscovery 但 navigator.bluetooth 没有这样的方法,而且 chrome.bluetooth 是空对象我根本无法发现它。因为我什至不想连接到设备,但只知道它的存在,所以每次进入蓝牙范围时都可以不配对设备吗?
  5. 是否有任何工作示例?
  6. Windows 配对应该足够了吗?出于安全原因,我们是否需要在 Chrome 中配对已由操作系统配对的设备?

我使用 CHROME 56.0.2924.76 64 位。

感谢您的帮助! 卡雷格

最佳答案

首先让我说Windows 10 对网络蓝牙的支持在不久的将来现已可用。定期查看Web Bluetooth implementation status页面。

回复:您的问题,您正在合并 Bluetooth Chrome API (适用于 Chrome 应用程序)和 Web Bluetooth API .由于 Windows 10 支持尚未完成,您将很难连接到 GATT 服务器。

  1. is it possible to use windows as development platform?

暂时没有。很快就可以了。

  1. is it possible to use android 5 to be bluetooth perpiheral?

是的。我一直用 BLE Peripheral Simulator 这样做安卓应用。

  1. what is the difference between chrome.bluetooth object and navigator.bluetooth object? Every tutorial uses chrome.bluetooth however it is undefined object in my chrome browser. Navigator.bluetooth works as mentioned before.

chrome.bluetooth 仅适用于 Chrome Appsnavigator.bluetooth 可用于 Chrome 应用程序和网站,因为这是一个 Web API。

  1. i've found chrome.bluetooth.startDiscovery but navigator.bluetooth doesnt have such method, and chrome.bluetooth is empty object i can not discover it at all. Because i do not want even to connect to devices, but only know its presence is it possible not to pair the devices each time it comes into the bluetooth range?

navigator.bluetoothrequestDevice 方法开始扫描。这将弹出一个选择器,用户可以从中选择一个设备。看这个device info sample

  1. are there any working examples?

https://googlechrome.github.io/samples/web-bluetooth/ 有很多

关于google-chrome - chrome 和蓝牙 - 连接、发现 - 如何使其正常工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41935541/

相关文章:

sqlite - Chrome 版本 75.0.3770.80(官方版本)(64 位)- 无法开始事务(3850 磁盘 I/O 错误)

css - 你如何使用CSS居中表格

android - 有 Bluedroid 测试或示例代码吗?

html - 两个 div 之间的 div 根据需要使用多少

css - 我的外部样式表不影响我的 HTML 文件

java - 蓝牙 GATT 尝试在空对象引用上调用虚拟方法 'void android.content.Context.sendBroadcast(android.content.Intent)'

java - Android 蓝牙多个客户端连接一台主机

javascript - 如何在 'now playing card'中显示音乐专辑封面?

java - ChromeDriver 的 org.openqa.selenium.remote.SessionNotFoundException

javascript - 如何彻底去除手机浏览器长按震动?