ws-slave.js
function checkConnected(peripheral, callback){
  if (peripheral.state === 'connected') {
    debug(' - connected');
    if (callback) { callback(); }
  } else if (peripheral.state === 'connecting'){
    debug(' - connecting....');
    //wait until the connection completes, invoke callback    
    peripheral.once('connect', function(){
      if (callback){
        callback();
      }
    }) 
  } 
  else { //not connected
    debug(' - not connected');
    //if peripheral is lost by noble
    if (! noble._peripherals[peripheral.id]) {
        console.log('No such peripheral! This should not happen, restart manually...');
        return
    }

    peripheral.connect( function(error) {
        if (error) {
          debug('checkconnected -> reconnect error !'.red + error)
        } else {
          debug('checkconnected -> reconnect');          
        }
        if (callback) { callback(error); };
    });
  } 
}
function checkConnected(peripheral, callback){
  if (peripheral.state === 'connected') {
    debug(' - connected');
    if (callback) { callback(); }
  } else if (peripheral.state === 'connecting'){
    debug(' - connecting....');
    //wait until the connection completes, invoke callback    
    peripheral.once('connect', function(){
      if (callback){
        callback();
      }
    }) 
  } 
  else { //not connected
    debug(' - not connected');
    //if peripheral is lost by noble
    if (! noble._peripherals[peripheral.id]) {
        console.log('No such peripheral! This should not happen, restart manually...');
        return
    }

    peripheral.connect( function(error) {
        if (error) {
          debug('checkconnected -> reconnect error !'.red + error)
        } else {
          debug('checkconnected -> reconnect');          
        }
        if (callback) { callback(error); };
    });
  } 
}
function checkConnected ( peripheral , callback ) { if ( peripheral . state === 'connected' ) { debug ( ' - connected' ) ; if ( callback ) { callback ( ) ; } } else if ( peripheral . state === 'connecting' ) { debug ( ' - connecting....' ) ; //wait until the connection completes, invoke callback     peripheral . once ( 'connect' , function ( ) { if ( callback ) { callback ( ) ; } } ) } else { //not connected debug ( ' - not connected' ) ; //if peripheral is lost by noble if ( ! noble . _peripherals [ peripheral . id ] ) { console . log ( 'No such peripheral! This should not happen, restart manually...' ) ; return } peripheral . connect ( function ( error ) { if ( error ) { debug ( 'checkconnected -> reconnect error !' . red + error ) } else { debug ( 'checkconnected -> reconnect' ) ; } if ( callback ) { callback ( error ) ; } ; } ) ; } }
check whether connected to peripheral, if not - reconnect, and then invoke peripheral function
check whether connected to peripheral if not - reconnect and then invoke peripheral function