• Hi,

    I have a problem that when connecting two Pucks together using BLE, there seems to be some sort of timeout going on which I did not program. I am using 1v91.

    I have a central and a peripheral Puck. Here is the code for the peripheral, which is pretty vanilla:

    var g_value = 0;
    
    function onInit() {
      NRF.setServices({
        "A4AF09E7-5BC2-46F6-BA08-DA8E0982D84D" :
        {
          0x2A6E:
          {
            readable: true,
            writable: true,
            notify: true,
            value : g_value,
            onWrite : function(evt)
            {
              digitalPulse(LED2, true, 10);
              g_value = evt.data[0];
            }
        }}});
    }
    

    Here is the code for the central:

    var g_gatt = false;
    var g_characteristic = false;
    var g_counter = 0;
    
    function connect(nextStep) {
        console.log("Requesting device");
        NRF.requestDevice({
            filters: [{
                name: 'Puck.js 2061'
            }]
        }).then(function(device) {
            console.log("Connecting to GATT");
            return device.gatt.connect();
        }).then(function(gatt) {
            console.log("Connecting to service");
            g_gatt = gatt;
            return g_gatt.getPrimaryService("A4AF09E7-5BC2-­46F6-BA08-DA8E0982D84D");
        }).then(function(service) {
            console.log("Getting characteristic");
            return service.getCharacteristic("0x2A6E");
        }).then(function(characteristic) {
            console.log("Calling next step");
            g_characteristic = characteristic;
            nextStep();
        }).catch(function() {
            console.log("Error connecting");
        });
    }
    
    function write(value) {
        if (!g_characteristic)
            connect(function() {
                write(value);
            });
        else
        {
            console.log("Writing ", value);
            g_characteristic.writeValue(value).catch­(function() {
                g_characteristic = false;
                write(value);
            });
        }
    }
    
    function onInit() {
        setWatch(function() {
            write(g_counter++);
        }, BTN, {
            edge: "rising",
            debounce: 50,
            repeat: true
        });
    }
    

    Again, nothing special. So when I first press the button on the central, it will connect to the peripheral and write the value. The peripheral will shortly blink green. Connecting of course takes a while, but subsequent writes will be fast. So far so good.

    The problem is that after letting the Pucks rest for a while, the write will fail. Then the catch will come into play and reconnect. So there seems to be some kind of inactivity timeout somewhere. I did not find any documentation for that or any hint how I can avoid that. Any ideas?

    The question is if there is a better way to send integers (preferably not just 8 bits) than sending them as a character and reconverting to an integer? Also, the way I handle the !g_characteristic case feels pretty clumsy. Any better idea on how to handle that? Generally, any hint on how to program the central more elegant is welcome.

    Thanks!

About

Avatar for stevie4711 @stevie4711 started