0

I have had a look, and see some great examples of nesting functions, but even with reading them, I cannot see why I am getting undefined when i call this function:

function readBttnDetec() {
    var devid = localStorage.getItem('vBttn');
    var bd = 0;
    bd = ble.read(devid, 'fffffff0-00f7-4000-b000-000000000000',
             'FFFFFFF2-00F7-4000-B000-000000000000', 
             function(t) {
                  var data = new Uint8Array(t)
                  console.log('returns: ' + data[0]); // this returns 6
                  return data[0];
             }, function(f) {
                 console.log(f);        
             });   

             return bd;
}

This is the call:

//check button state
        var detecs = readBttnDetec();
        console.log(detecs);
        if(detecs == 2) {
            // fall detection disabled     
             $('#playfall').removeClass('km-state-active');    
        } else if(detecs == 6) {
            // fall detection enabled
            $('#playfall').addClass('km-state-active');
        } else {
            // error reading button
        }

I am missing something simple I am sure of it, but I cannot see it.

Thanks in advance

Andrew Walker
  • 492
  • 1
  • 5
  • 23
  • I can't see where you declare `ble`? – Zakaria Acharki Jan 16 '16 at 11:55
  • @ZakariaAcharki Hiya, that is declared when the App starts, and is part of a Central-BLE plugin. It does work, I just need to connect it to a toggle switch, but i cannot return the value. `data[0] = 6` but `bd = undefined`. Cannot work out why – Andrew Walker Jan 16 '16 at 12:00
  • @Andreas possibly a duplicate, but as I read it, this is not asyn function, nor an Ajax one. It should not continue with the function until the button returns answer. But perhaps i am wrong ? But I did declare `bd` with zero, so if anything it should return Zero, not undefined ? – Andrew Walker Jan 16 '16 at 12:06
  • 1
    If you have a function with name `read` that accepts a callback, it's most likely async. Otherwise, why would you have to pass a callback in the first place? – Felix Kling Jan 16 '16 at 12:08
  • The result you're looking for (`6`) would be the return value of an asynchronously called function - the callback of `ble.read()`. I don't know what `ble` is and what `ble.read()` returns. – Andreas Jan 16 '16 at 12:09
  • Hi, I am guessing it must be async based on comments on here. `ble.read` brings back { "0" : x }, which is a state of characteristic of a bluetooth peripheral. In my case it's either a 2, or 6. Which i wanted to change a toggle button. But the docs I have with it did not state it was async, just 'connected to'. But thank you for all your help, it's very much appreciated. – Andrew Walker Jan 16 '16 at 22:02

0 Answers0