如何在ChromeOS上从Chrome应用程序配对新的蓝牙设备

How to pair a new Bluetooth device from a Chrome App on ChromeOS?

本文关键字:ChromeOS 上从 应用程序 Chrome      更新时间:2023-09-26

我正在使用Chrome.bluetooth Javascript API和PNACL编写Chrome应用程序。我可以打开蓝牙发现、查找设备、连接并成功通信。但我不知道如何从我的应用程序中以编程方式配对新设备。

有Windows和Mac系统的API;ChromeOS上有类似产品吗?

使用chrome.bluetooth API连接到仅适用于OS X、Windows和chrome OS的蓝牙设备。所有功能通过chrome.runtime.lastError.报告故障

你可以让你的chrome应用程序连接到任何支持RFCOMM或L2CAP服务的设备,包括市场上大多数经典蓝牙设备。

正如Chrome-蓝牙中详细介绍的那样,连接到设备需要三件事:

  • 使用bluetoothSocket.create创建的用于建立连接的套接字
  • 要连接的设备的地址
  • 服务本身的UUID

示例代码实现:

var uuid = '1105';
var onConnectedCallback = function() {
  if (chrome.runtime.lastError) {
    console.log("Connection failed: " + chrome.runtime.lastError.message);
  } else {
    // Profile implementation here.
  }
};
chrome.bluetoothSocket.create(function(createInfo) {
  chrome.bluetoothSocket.connect(createInfo.socketId,
    device.address, uuid, onConnectedCallback);
});

还请注意,在进行连接之前,您应该使用bluetooth.getDevice或设备发现API验证适配器是否知道该设备。

更多信息和示例代码实现可以在文档中找到。