Chrome 应用程序 - 无法读取未定义的 属性 'connect'

Chrome App - Cannot read property 'connect' of undefined

我正在开发 Raspberry pi 3,我有一个将文本数据发送到 Chrome 应用程序的网页,到目前为止一切正常。

之后 Chrome 应用程序应该将该数据发送到串行端口,这里出现错误:

Error in event handler for runtime.onMessageExternal: TypeError: Cannot read property 'connect' of undefined

问题可能是这不是 Chrome 应用程序,它是一个扩展,它不能使用这个 API 因为只有 Chrome 应用程序可以访问硬件,但我按照本指南制作了我的第一个 Chrome 应用程序 (https://developer.chrome.com/apps/first_app),所以可能有一些我不明白的地方或我错过了一个步骤。

这是我的代码,在此先感谢您的帮助!

manifest.json

{
  "name": "Send serial data",
  "description": "App to send serial data.",
  "version": "1.0",
  "manifest_version": 2,
  "app": {
    "background": {
      "scripts": ["background.js"]
    }
  },
  "icons": { "16": "img-16.png", "128": "img-128.png" },
  "externally_connectable" : {
    "matches": ["*://localhost/*"]
  }
}

background.js

var msg ;

function openSend(data) {
    var onConnect ;
    onConnect = function(connectionInfo) {
        _this.connectionId = connectionInfo.connectionId;
    }

    chrome.serial.connect("/dev/ttyAMA0", {bitrate: 115200}, onConnect);

    chrome.serial.send(connectionId, convertStringToArrayBuffer(data), function(sendInfo) {
        if(sendInfo.error) $.modal('<div id="title">Unable to send data: ' + sendInfo.error + '</div>')
    });
}

chrome.runtime.onMessageExternal.addListener((message, sender, sendResponse) => {
    console.log(message.data);
    msg = message.data ;
    openSend(msg);  
});

解决了,我只是忘了把这行代码放在我的 manifest.json:

"permissions":["serial"],