Error code reporting time issue

Ask, and ye shall receive.
Amman
Posts: 14
Joined: 21:42, 19 Feb 2015

Error code reporting time issue

Postby Amman » 20:11, 13 Aug 2015

Hi,
Working with the evothings BLE example using the RFduino.
It works great when connecting and turning on and off the blue LED. However, when the RFduino is not available, the client app on my phone takes forever to report back that it can not connect with the device. It is stuck on "Connecting..." for long time.

Where in the code can I make changes to set the error reporting time, let say after trying to connect to the rfduino for up to a maximum of 30sec, if can't find one, report Error:


Thanks

Fredrik
Site Admin
Posts: 196
Joined: 15:00, 18 Nov 2013

Re: Error code reporting time issue

Postby Fredrik » 07:54, 14 Aug 2015

That would be the function evothings.rfduinoble.connect().

There, one might call setTimeout() before startScan(), store the timer ID and disable it in the startScan() callback if a device is found.

Amman
Posts: 14
Joined: 21:42, 19 Feb 2015

Re: Error code reporting time issue

Postby Amman » 16:17, 14 Aug 2015

Thanks Fredrik,

I need a little bit more guidance as I am not expert with js. This is what I did (after some research how to use setTimeout) and, as you suggested modified the code. But my question is I wanted my phone to start scanning and report back within 30sec, the Error message - if it can't find the RFduino, So i put the setTimeout after the startScan, would that be wrong? Here is my modified code. I tested this but it did not work, (could not connect to the RFduino even if it is present). So I think I did something wrong modifying it. Also not 100% sure if i placed the clearTimeout(myVar); correctly in the code, Any suggestions would be great

Code: Select all

evothings.rfduinoble.connect = function(deviceName, success, fail)
   {
      
      evothings.easyble.startScan(
         myVar = setTimeout(
         function(device)
         {
            console.log('found device: ' + device.name);
            if (device.name == deviceName)
            {
               evothings.easyble.stopScan();
               console.log('connectToDevice');
               internal.connectToDevice(device, success, fail);
               clearTimeout(myVar);
            }
         }, 3000),
         function(errorCode)
         {
            fail(errorCode);
         });
   };


Thanks a lot ! :D

Fredrik
Site Admin
Posts: 196
Joined: 15:00, 18 Nov 2013

Re: Error code reporting time issue

Postby Fredrik » 09:29, 17 Aug 2015

You put the setTimeout() call inside startScan(), which wouldn't work well. The timer should be a local variable (the "var" specifier), so outside functions can't accidentally mangle it. We also need to call stopScan() to prevent further callbacks.

Disclaimer: this code is untested and may fail.

Code: Select all

evothings.rfduinoble.connect = function(deviceName, success, fail)
{
   var timer = setTimeout(function() {
      console.log('timeout!');

      evothings.easyble.stopScan();
      timer = false;
      
      fail('timeout!');
   }, 3000);

   evothings.easyble.startScan(
      function(device)
      {
         console.log('found device: ' + device.name);
         if (device.name == deviceName)
         {
            evothings.easyble.stopScan();
            console.log('connectToDevice');
            internal.connectToDevice(device, success, fail);
            clearTimeout(timer);
         }
      },
      function(errorCode)
      {
         fail(errorCode);
      });
};


Return to “Questions and answers”

Who is online

Users browsing this forum: No registered users and 6 guests