• Yes, an automatic retry has crossed my mind, but I wanted to understand why these thing "happed" first.

    Based on https://www.espruino.com/Puck.js+Controlling+Other+Pucks I made a version with busy/connected flags.

    // Are we busy?
    var busy = false;
    
    // The device, if we're connected
    var connected = false;
    
    var d;
    
    // The 'tx' characteristic, if connected
    var txCharacteristic = false;
    var rxCharacteristic = false;
    
    function getData(){
    
    // Function to call 'toggle' on the other Puck
      if (!busy) {
        print("Getting data now");
        busy = true;
        if (!connected) {
          NRF.connect("30:ae:a4:5d:b3:f2 public").then(function(device) {
            // print(device);
            d=device;
            return d.getPrimaryService("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
          }).then(function(s) {
            return s.getCharacteristic("6e400003-b5a3-f393-e0a9-e50e24dcca9e");
          }).then(function(c) {
            // print(c);
            rxCharacteristic = c;
            busy = false;
    
      c.on('characteristicvaluechanged', function(event) {
        buf =E.toString(event.target.value.buffer);
        
        print("RX  " + buf);
    
        
        if(buf.includes("Quit")){
          print("Quit received, disconnecting now");
          d.disconnect();
          connected=false;
          busy = false;
        }
      });
      
      return c.startNotifications();
            
          });
        }
      }
    
    }
    
    getData();
    

    This works brilliant.

    • Every time I upload the sketch connection is made to the UART server without any errors or complaints.
    • And the disconnect is fired at receiving of "Quit", and recognised by the UART server.

    Output of 2 subsequent uploads, no errors.

    
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v06 (c) 2019 G.Williams
    >Getting data now
    RX  D07-06-2020 12.23
    RX  SAAPL,311.01,+11.5%
    RX  SASML,299.45,-4.5%
    RX  Hottentottententente
    RX  D07-06-2020 12.23
    RX  SAAPL,311.01,+11.5%
    RX  SASML,299.45,-4.5%
    RX  Hottentottententente
    RX  D07-06-2020 12.23
    RX  SAAPL,311.01,+11.5%
    RX  SASML,299.45,-4.5%
    RX  Hottentottententente
    RX  D07-06-2020 12.23
    RX  SAAPL,311.01,+11.5%
    RX  SASML,299.45,-4.5%
    RX  Hottentottententente
    RX  Quit
    Quit received, disconnecting now
    >
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v06 (c) 2019 G.Williams
    >Getting data now
    RX  D07-06-2020 12.23
    RX  SAAPL,311.01,+11.5%
    RX  SASML,299.45,-4.5%
    RX  Hottentottententente
    RX  D07-06-2020 12.23
    RX  SAAPL,311.01,+11.5%
    RX  SASML,299.45,-4.5%
    RX  Hottentottententente
    RX  D07-06-2020 12.23
    RX  SAAPL,311.01,+11.5%
    RX  SASML,299.45,-4.5%
    RX  Hottentottententente
    RX  D07-06-2020 12.23
    RX  SAAPL,311.01,+11.5%
    RX  SASML,299.45,-4.5%
    RX  Hottentottententente
    RX  Quit
    Quit received, disconnecting now
    Disconnected from Web Bluetooth, Bangle.js 0195
    >
     
    

    But when I execute the getData function with a setInterval, the "uncaught errors .... promise....." reappear.

    
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v06 (c) 2019 G.Williams
    >
    Getting data now
    Getting data now
    Uncaught Error: Unhandled promise rejection: BLE error 0x12
    RX  D07-06-2020 12.23
    RX  SAAPL,311.01,+11.5%
    RX  SASML,299.45,-4.5%
    RX  Hottentottententente
    RX  D07-06-2020 12.23
    RX  SAAPL,311.01,+11.5%
    RX  SASML,299.45,-4.5%
    RX  Hottentottententente
    RX  D07-06-2020 12.23
    RX  SAAPL,311.01,+11.5%
    RX  SASML,299.45,-4.5%
    RX  Hottentottententente
    RX  D07-06-2020 12.23
    RX  SAAPL,311.01,+11.5%
    RX  SASML,299.45,-4.5%
    RX  Hottentottententente
    RX  Quit
    Quit received, disconnecting now
    Getting data now
    Uncaught Error: Unhandled promise rejection: Disconnected
    > 
    

    I assume that in the subsequent uploads the Espruino environment is fully reset, and the errors do not occur, but using setInterval the environment is not "cleaned" in a similar way.

About

Avatar for gerardwr @gerardwr started