2016-04-08 261 views
0

我想創建一個Chrome應用程序,當某個藍牙設備正在發送數據時顯示該應用程序。具體來說,我有2個藍牙鼠標,我想確定哪一個正在特定的時間移動。 我跟着Chrome開發文檔,併成功,直到我試圖實現在接收添加一個偵聽器來查看來自設備的數據。我得到一個「無法讀取屬性」addListener「的未定義」錯誤。Chrome應用程序藍牙

這是當我開始收到此錯誤: 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); 
 
     }); 
 
    }); 
 
});

回答

0

經過了這docs工作的代碼,並設法看到類似的代碼片段:

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

如果仔細看一下,看起來onRecieve部分樣本中存在拼寫錯誤。它應該是onReceive。你可以看到一個正確的樣本here

我除C後前ē。 ;) 希望這可以幫助。祝你好運。