You are reading a single comment by @ChristianW and its replies. Click here to read the full conversation.
  • Having 5 pucks at hand I'd like to do some round-robin communication:

    • request a list of available pucks around me (works)
    • iterate through the list (issue A below)
    • receive feedback during the UART connection (issue B below)

    I am aware, that connection multiple peripherals at the same time is not possible atm, so I do it sequentially (with recursive callbacks).

    This is my connection code:

    function transfer(device, text, callback) {
      var char;
      var result = "";
      console.log( "connecting " + device.name );
      return device.gatt.connect().then(function(d) {
        device = d;
        console.log( "connected" );
        return d.getPrimaryService("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
      }).then(function(s) {
        console.log( "service found" );
        return s.getCharacteristic("6e400002-b5a3-f393-e0a9-e50e24dcca9e");
      }).then(function(c) {
        char = c;
        console.log( "characteristic found" );
        function sender(resolve, reject) {
          console.log( "sending" );
          if (text.length) {
            char.writeValue(text.substr(0,20)).then(function() {
              sender(resolve, reject);
            }).catch(reject);
            text = text.substr(20);
          } else  {
            resolve();
          }
        }
        return new Promise( sender );
    /* 
       }).then(function() {
        function receiver(resolve, reject) {
          console.log( "receiving" );
          char.readValue().then(function(chunk) {
            console.log(chunk);
            if( chunk.length > 0 ) {
              result += chunk;
              receiver(resolve, reject);
            }
            else {
              resolve();
            }
          }).catch(reject);
        }
        return new Promise( receiver ); */
      }).then(function() {
        device.disconnect();
        if (callback) callback(result);
      });
    }
    

    Issue A:
    As long as I disable (/**/) the receiving part I can pass commands to the first puck successfully.
    But when I try to connect the second one I get BLE error 18 - why?

    found Puck.js f002
    found Puck.js 38a4
    found Puck.js c9fc
    connecting Puck.js c9fc
    connected
    service found
    characteristic found
    sending
    sending
    done
    connecting Puck.js 38a4
    Uncaught Error: Unhandled promise rejection: Error: Got BLE error code 18

    Question: What does that mean and what can I do about it?

    Issue B:
    I also like to get the feedback of the command sent to the pucks.
    So I tried to add the receiver part (in comments) but it always receives the echo of the data I just sent.

    Question: How can I get the full response and how do I detect the end of a transmission?

    I'll upload the full sourcecode as attachment so you have the complete picture.

    Thanks in advance,
    Christian


    1 Attachment

About

Avatar for ChristianW @ChristianW started