• Hi,

    sorry for opening a new topic again already, but I hope that our notes might help somebody else later on.

    Right now I am connecting like this to my bike sensor:

    NRF.requestDevice({ timeout:5000, filters: [{ services: "0x1816"  /* namePrefix: 'Wahoo' */ }] }).then(function(device) {
      console.log(device);
      return device.gatt.connect();
      }).then(function(gatt_object) {
        gatt = gatt_object;
        s = "Looking for service...";
        g.clear(); g.drawString(s ,0 ,0); g.flip();
        return gatt.getPrimaryService("1816");
        }).then(function(service) {
          s = "Looking for char...";
          g.clear(); g.drawString(s ,0 ,0); g.flip();
          return service.getCharacteristic("0x2A5B");
        }).then(function(characteristic) {
          characteristic.on('characteristicvaluechanged', OnNotify);
          return characteristic.startNotifications();
          }).then(function() {
            connected = true;
            t = getTime();
            setDisplayMode("livedata");
            gatt.device.on('gattserverdisconnected', function(reason) {console.log("gattserverdisconnected, Grund: "+ reason+" Zeit: " + (getTime() -t) + " sec"); disconnect(reason);} ); 
    }
    

    Initially, I even had a timeout of 20 seconds in requestDevice, which led to pretty unreliable connection intitation.
    With nrfConnect I see that the sensor starts advertising really fast with an interval of 330ms initially.
    I wonder if it is possible to cut the timeout short (right now it seems that the code scans for the full timeout before handing back scan results) and return a matching device immediately?

    Or is it preferable to cache the sensor's address and do a direct connect to that later on? That would have the (very small) drawback of needing additional logic to do a full scan if the previous/cached device is not successfully connected, to enable connecting to new/other sensors.

    Thanks,

    Joost

  • Hi,

    I wonder if it is possible to cut the timeout short (right now it seems that the code scans for the full timeout before handing back scan results) and return a matching device immediately?

    If you try a 'cutting edge' build then that's something I've fixed (and will be in Espruino 1v100) - it'll be significantly faster.

    However...

    is it preferable to cache the sensor's address and do a direct connect to that later on?

    Nothing stops you from caching the result of NRF.requestDevice and then calling device.gatt.connect() when you want to connect. As you say, you could then just add a .catch handler to the connect promise, and if it fails you run NRF.requestDevice again.

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

Fastest and/or most reliable way to connect to BLE central/server

Posted by Avatar for Joost @Joost

Actions