var background_mecoffee_socket_id;

// https://bugs.chromium.org/p/chromium/issues/detail?id=370744

chrome.app.runtime.onLaunched.addListener(function() {
  
  chrome.app.window.create('index.html', {
    'bounds': {
      'width': 800,
      'height': 600
    } },

    function( win ) {
    	console.log( 'in win callback');

    	win.onClosed.addListener( function() { 
    		console.log( 'window close'); 

    		chrome.bluetoothSocket.disconnect( background_mecoffee_socket_id );
  			chrome.bluetoothSocket.close( background_mecoffee_socket_id );

    	} );
    }    

  );

  chrome.bluetooth.getAdapterState(function(adapter) {
  console.log("Adapter " + adapter.address + ": " + adapter.name);
  });

});

chrome.runtime.onMessage.addListener(
  function(request, sender, sendResponse) {
  	console.log('msgZ!')
    //console.log(sender.tab ?
      //          "from a content script:" + sender.tab.url :
        //        "from the extension");
    if (request.type == "socket" ) {
    	console.log( 'socketid received' );

    	background_mecoffee_socket_id = request.socket;

      // sendResponse({farewell: "goodbye"});
    }
  });

