Search code examples
javascriptgoogle-chrome-app

Close serial port on close of Google Chrome App


I am trying to get the app to close the serial port on exit of the app and also force a reload of the app so that when the user opens the app again, they are able to access the serial port.

Here is my code:

  chrome.app.window.onClosed.addListener(function() {
     chrome.serial.close(connectionId, onClose);
     chrome.runtime.reload();
  });

It does not seem to be reloading the app. If I physically call chrome.runtime.reload() once the app has started everything works fine. Otherwise, it gets hung up when attempting to open the serial connection.

Thanks!


Solution

  • onClose gets called when the app is closing. What you're doing is trying to start the app when it closes, effectively preventing the app from ever actually closing.

    Are you registering your onClosed handler from the background page? It looks like chrome.serial.close is asynchronous, so perhaps there is a problem running it from a window that is currently closing.