Search code examples
serial-portgoogle-chrome-app

Chrome App - Cannot read property 'connect' of undefined


I'm working on a Raspberry pi 3, I have a webpage that sends text data to a Chrome App, and everything works fine up to here.

After that the Chrome App should send that data to the serial port and here appears the error:

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

The problem could be that this is not a Chrome App, it's an Extension and it can't use this API cause only Chrome Apps have access to the hardware, but I followed this guide to make my first Chrome App (https://developer.chrome.com/apps/first_app), so maybe there's something i didn't understand or a step that i missed.

Here's my code, thanks in advance for the help!

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

Solution

  • Solved, i just forgot to put this line of code in my manifest.json:

    "permissions":["serial"],