Chrome应用程序蓝牙



我正在尝试创建一个chrome应用程序,该应用程序在某个蓝牙设备发送数据时显示。具体来说,我有两个蓝牙鼠标,我想确定哪一个在特定的时间被移动。我遵循了Chrome开发文档,并取得了成功,直到我尝试在接收时添加一个侦听器来查看来自设备的数据。我收到一个"无法读取未定义的属性"addListener"错误。

这是我开始出现以下错误的时候:错误消息

这是我正在使用的代码

chrome.bluetooth.getDevices(function(devices) {
  for (var i = 0; i < devices.length; i++) {
    //Displaying device names
    console.log(i+": "+devices[i].name);
  }
  //uuid for a specific device
  var uuid = "00001200-0000-1000-8000-00805f9b34fb";
  // var uuid = devices[4].uuid;
  
    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,
        devices[4].address, uuid, onConnectedCallback);
      console.log(createInfo);
      chrome.bluetoothSocket.onRecieve.addListener(function(receiveInfo) {
          if (receiveInfo.socketId != socketId)
            return;
        console.log(receiveInfo);
        });
    });
});

查看了这些文档,并看到了类似的代码片段:

chrome.bluetoothSocket.onRecieve.addListener(function(receiveInfo) {
  if (receiveInfo.socketId != socketId)
    return;
  // receiveInfo.data is an ArrayBuffer.
});

如果你仔细看,onRecieve部分的样本中似乎有拼写错误。它应该是onReceive。你可以在这里看到一个正确的样本。

IE之前,除了在C之后。

相关内容

最新更新