【问题标题】:Chrome App bluetoothChrome 应用蓝牙
【发布时间】:2016-07-29 21:46:50
【问题描述】:

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

这是我开始收到此错误的时间: Error message

这是我正在使用的代码

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);
        });
    });
});

【问题讨论】:

    标签: javascript api google-chrome bluetooth google-chrome-app


    【解决方案1】:

    查看了这个docs并设法看到了类似的代码sn-p:

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

    如果你仔细看,onRecieve 部分的示例中似乎有错字。应该是onReceive。你可以看到一个正确的样本here

    IE之前,C之后除外

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-09-02
      • 1970-01-01
      • 1970-01-01
      • 2014-08-23
      • 1970-01-01
      相关资源
      最近更新 更多