How to manual disconnect from BLE UART server

Posted on
  • Hi,

    Espruino beginner here, in need for some help.

    I am running code from here:
    https://www.espruino.com/BLE+UART

    var gatt;
    NRF.connect("30:ae:a4:5d:b3:f2 public").then(function(g) {
      gatt = g;
      return gatt.getPrimaryService("6e400001-b5a3-f3­93-e0a9-e50e24dcca9e");
    }).then(function(service) {
      return service.getCharacteristic("6e400003-b5a3­-f393-e0a9-e50e24dcca9e");
    }).then(function(characteristic) {
      characteristic.on('characteristicvaluech­anged', function(event) {
        console.log("RX: "+E.toString(event.target.value.buffer))­;
      });
      return characteristic.startNotifications();
    }).then(function() {
      console.log("Done!");
    });
    

    The code:

    • connects OK to my BLE UART server (An ESP32 with Arduino BLE server code that repeats sending BLE-UART messages)
    • displays the data sent by the BLE server in the Espruino IDE.

      Data is like:

      RX: D07-06-2020 12.23
      RX: SAAPL,311.01,+11.5%
      RX: SASML,299.45,-4.5%
      RX: Hottentottententente
      RX: Quit
      RX: D07-06-2020 12.23
      RX: SAAPL,311.01,+11.5%
      RX: SASML,299.45,-4.5%
      RX: Hottentottententente
      RX: Quit
      RX: D07-06-2020 12.23
      

    But I would like to disconnect from the server when data “Quit” is received.

    Something like:

    if(buffer.includes("Quit"){
      gatt.disconnect();
    }
    

    Have read many things, search the forum, and tried many examples, to no avail.

    Who can help along?

  • That should work absolutely fine: http://www.espruino.com/Reference#l_Blue­toothRemoteGATTServer_disconnect

    Although the code you post needs another ) in it. What isn't working for you? It just doesn't seem to disconnect for you?

  • Thanks for your response Gordon.

    Code changed to:

    var gatt;
    NRF.connect("30:ae:a4:5d:b3:f2 public").then(function(g) {
      gatt = g;
      return gatt.getPrimaryService("6e400001-b5a3-f3­93-e0a9-e50e24dcca9e");
    }).then(function(service) {
      return service.getCharacteristic("6e400003-b5a3­-f393-e0a9-e50e24dcca9e");
    }).then(function(characteristic) {
      characteristic.on('characteristicvaluech­anged', function(event) {
        
        buf =E.toString(event.target.value.buffer);
        console.log("RX: "+buf);
        if(buf.includes("Quit")){
          gatt.disconnect();
        }
        
      });
      return characteristic.startNotifications();
    }).then(function() {
      console.log("Done!");
    });
    

    The response after uploading the code varies with changing the code, here 3 examples:

     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v06 (c) 2019 G.Williams
    >
    Done!
    RX: D07-06-2020 12.23
    RX: SAAPL,311.01,+11.5%
    RX: SASML,299.45,-4.5%
    RX: Hottentottententente
    RX: Quit
    RX: D07-06-2020 12.23
    RX: SAAPL,311.01,+11.5%
    RX: SASML,299.45,-4.5%
    RX: Hottentottententente
    RX: Quit
    Uncaught Error: BLE task 3 is already in progress
     at line 1 col 107
    ...s("Quit")){gatt.disconnect();}
                                  ^
    in function called from system
    RX: D07-06-2020 12.23
    RX: SAAPL,311.01,+11.5%
    > 
    
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v06 (c) 2019 G.Williams
    >
    Done!
    RX: D07-06-2020 12.23
    RX: SAAPL,311.01,+11.5%
    RX: SASML,299.45,-4.5%
    RX: Hottentottententente
    RX: Quit
    RX: D07-06-2020 12.23
    RX: SAAPL,311.01,+11.5%
    RX: SASML,299.45,-4.5%
    RX: Hottentottententente
    RX: Quit
    Uncaught Error: Not connected
     at line 1 col 107
    ...s("Quit")){gatt.disconnect();}
                                  ^
    in function called from system
    >
    
     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v06 (c) 2019 G.Williams
    >
    Uncaught Error: Unhandled promise rejection: Disconnected
    >
    

    Not sure what is happening in the background.

  • Added a “print” when “Quit" is received

    It seems like the "disconnect" is not executed, output is:

    >
    Done!
    RX: SASML,299.45,-4.5%
    RX: Hottentottententente
    RX: Quit
    Quit received, disconnecting now
    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
    RX: D07-06-2020 12.23
    Uncaught Error: BLE task 3 is already in progress
     at line 1 col 149
    ...ting now");gatt.disconnect();}
                                  ^
    in function called from system
    RX: SAAPL,311.01,+11.5%
    RX: SASML,299.45,-4.5%
    > 
    
  • BLE task 3 is already in progress isn't very friendly I know, but it actually means that the disconnect was already in progress.

    I guess I see RX: Quit twice, so the first time disconnect is actually called and the disconnection was in progress - but for some reason the disconnect doesn't seem to happen immediately.

  • Ok so BLE task 3 is already in progress should be interpreted as disconnect, right ;-)

    And the guess that the 1st disconnect is fired, but takes some time to process, so that the 2nd disconnect result in "BLE task 3 ......progress" seems plausible.

    But I'm not out of the woods yet.

    When I start the code I get this, as before, disconnects after some "overshoot".
    The UART server recognises the disconnect after the last outputline, stops sending notification, start advertising again for a new connection.

      _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v06 (c) 2019 G.Williams
    >
    Done!
    RX: SASML,299.45,-4.5%
    RX: Hottentottententente
    RX: Quit
    Quit received, disconnecting now
    RX: D07-06-2020 12.23
    RX: SAAPL,311.01,+11.5%
    RX: SASML,299.45,-4.5%
    >
    

    When I re-upload the code for a 2nd time, the UART server recognises the reconnect and starts sending data, but the BangJS does not process the notifications and displays no output, and after 10 secs or so I get an "Uncaught error". My server never gets a disconnect, so keeps sending data, forever.....

     ____                 _
    |  __|___ ___ ___ _ _|_|___ ___
    |  __|_ -| . |  _| | | |   | . |
    |____|___|  _|_| |___|_|_|_|___|
             |_| espruino.com
     2v06 (c) 2019 G.Williams
    >
    Uncaught Error: Unhandled promise rejection: Disconnected
    > 
    

    My intention was to process the code with a setInterval every minute or so, but with the current behaviour that seems problematic.

    Hmmmmm, problems, problems.

    Thanks for your help though, if you have any other suggestions, yes please.

  • I added a setInterval to process the code ever 10 secs, the behaviour is now like this:

    • sometimes it is processed "OK" with the overshoot and the "uncaught error BLE task .... progress"
    • sometimes it result just in an "uncaught error ........ promise rejection", no data received

    Maybe you know the source of the "uncaught error ........ promise rejection"?

    Code and selection of output for reference below.

    
    setInterval(function() {
    
    
    var gatt;
    
    print("Getting data from server");
      
    NRF.connect("30:ae:a4:5d:b3:f2 public").then(function(g) {
      gatt = g;
      return gatt.getPrimaryService("6e400001-b5a3-f3­93-e0a9-e50e24dcca9e");
    }).then(function(service) {
      return service.getCharacteristic("6e400003-b5a3­-f393-e0a9-e50e24dcca9e");
    }).then(function(characteristic) {
      characteristic.on('characteristicvaluech­anged', function(event) {
        
        buf =E.toString(event.target.value.buffer);
        console.log("RX: "+buf);
        if(buf.includes("Quit")){
          print("Quit received, disconnecting now");
          gatt.disconnect();
        }
        
      });
      return characteristic.startNotifications();
    }).then(function() {
      console.log("Done!");
    });
    
    }, 10000);
    

    Output like this:

    Getting data from server
    Uncaught Error: Unhandled promise rejection: Disconnected
    Getting data from server
    Uncaught Error: Unhandled promise rejection: Disconnected
    Getting data from server
    Uncaught Error: Unhandled promise rejection: Disconnected
    Getting data from server
    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
    RX: D07-06-2020 12.23
    Uncaught Error: BLE task 4 is already in progress
     at line 1 col 149
    ...ting now");gatt.disconnect();}
                                  ^
    in function called from system
    RX: SAAPL,311.01,+11.5%
    RX: SASML,299.45,-4.5%
    RX: Hottentottententente
    Uncaught Error: Unhandled promise rejection: Disconnected
    Getting data from server
    Uncaught Error: Unhandled promise rejection: Disconnected
    Getting data from server
    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
    RX: D07-06-2020 12.23
    Uncaught Error: BLE task 4 is already in progress
     at line 1 col 149
    ...ting now");gatt.disconnect();}
                                  ^
    in function called from system
    RX: SAAPL,311.01,+11.5%
    RX: SASML,299.45,-4.5%
    RX: Hottentottententente
    Uncaught Error: Unhandled promise rejection: Disconnected
    Getting data from server
    Uncaught Error: Unhandled promise rejection: Disconnected
    Getting data from server
    Uncaught Error: Unhandled promise rejection: Disconnected
    Getting data from server
    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
    
  • Increase interval from 10 secs to 1 minute, no change in behaviour. Seems that errors are not caused by timing issues.

  • The 'Disconnected' rejection just seems to occur when Espruino disconnects while trying to connect. It might be made worse by the fact it's trying to keep a connection to the PC going as well? All I can suggest is to do an automatic retry, which you could do by adding .catch(function(err) {...})

  • 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+Control­ling+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.

  • You do seem to be setting:

            rxCharacteristic = c;
            busy = false;
    

    Which would allow it to try and connect again, even before it had disconnected.

    Could that be your issue?

  • No, that's not it.

    The lines are in the example on your site and I just left them in.
    https://www.espruino.com/Puck.js+Control­ling+Other+Pucks

    To be sure I removed them but it makes no difference. Output with the setInterval:

    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
    Done!
    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 think I'm getting close to a "perfect" solution and in hindsite it will be a simple obvious mistake on my part. I catching up my reading om "promises" so at least I'm learning here, that's what it's all about, right!?

    Appreciate your tireless help!

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

How to manual disconnect from BLE UART server

Posted by Avatar for gerardwr @gerardwr

Actions