NRF multiple connections?

Posted on
  • 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:

    1. function transfer(device, text, callback) {
    2. var char;
    3. var result = "";
    4. console.log( "connecting " + device.name );
    5. return device.gatt.connect().then(function(d) {
    6. device = d;
    7. console.log( "connected" );
    8. return d.getPrimaryService("6e400001-b5a3-f393-e0a9-e50e24dcca9e");
    9. }).then(function(s) {
    10. console.log( "service found" );
    11. return s.getCharacteristic("6e400002-b5a3-f393-e0a9-e50e24dcca9e");
    12. }).then(function(c) {
    13. char = c;
    14. console.log( "characteristic found" );
    15. function sender(resolve, reject) {
    16. console.log( "sending" );
    17. if (text.length) {
    18. char.writeValue(text.substr(0,20)).then(function() {
    19. sender(resolve, reject);
    20. }).catch(reject);
    21. text = text.substr(20);
    22. } else {
    23. resolve();
    24. }
    25. }
    26. return new Promise( sender );
    27. /*
    28. }).then(function() {
    29. function receiver(resolve, reject) {
    30. console.log( "receiving" );
    31. char.readValue().then(function(chunk) {
    32. console.log(chunk);
    33. if( chunk.length > 0 ) {
    34. result += chunk;
    35. receiver(resolve, reject);
    36. }
    37. else {
    38. resolve();
    39. }
    40. }).catch(reject);
    41. }
    42. return new Promise( receiver ); */
    43. }).then(function() {
    44. device.disconnect();
    45. if (callback) callback(result);
    46. });
    47. }

    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

  • Try adding a delay between disconnecting from one puck and connecting to the other. Disconnect starts a disconnect but it takes some time to complete, and the web Bluetooth spec doesn't seem to remove a return a promise.

    Unfortunately notifications aren't implemented yet, so you can't get the result. It'll be in a firmware update in the new year though

  • Thanks again, Gordon.

    Adding the timeout solved it:

    1. scan( function( queue ) {
    2. // recurse through queue
    3. function process() {
    4. if( queue.length ) {
    5. transfer( queue.pop(), command, function( result ) {
    6. console.log( "done" );
    7. setTimeout( process, 150 );
    8. });
    9. }
    10. }
    11. process();
    12. });

    150 ms seems sufficient.
    I was able to go down to about 145 ms, but everything below still caused some errors.

    I'm very thrilled to get the notifications.
    Good work - keep it up!

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

NRF multiple connections?

Posted by Avatar for ChristianW @ChristianW

Actions