title: CoreBluetooth: detect device out of range/ connection timeout
body: <p>I am designing an iOS framework to handle multiple BLE devices (all of the same kind). Everything is working very well at the moment, except one thing:</p>
  
  <p>The client wants a list with available devices. But how can I detect when a device, that has been discovered in the past, is not available anymore?</p>
  
  <p>Another problem occurs, when I try to connect to a device that is not available anymore. Documentation says: Connection attempts never time out and</p>
  
  <p>And yes, I never get an error via didFailToConnectPeripheral.</p>
  
  <p>I did some research but couldn't figure out how handle these problems via CoreBluetooth properly. So I developed my own solutions, but I am not sure if that is the right way (or at least a good way, cause there may be several ways to do it). </p>
  
  <p><strong>1. Detecting devices that are not available anymore</strong></p>
  
  <p>I scan with</p>
  
  <pre><code>[_centralManager scanForPeripheralsWithServices:services options:@{CBCentralManagerScanOptionAllowDuplicatesKey: @(TRUE)}];
  </code></pre>
  
  <p>so I receive advertisments all the time as long as a device is not connected. I check with a timer that the advertisement reoccured in a given time interval (large enough corresponding to the devices ad interval). If the advertisement didn't occur in the interval, I remove the device from the list.</p>
  
  <p><strong>2. Detecting connection timeout</strong>
  Well, that's a pretty easy one I think. I use my own timeout function and cancel the connection request if the timer expires.</p>
  
  <p>If somebody ever came across these problem, I would be very interested in your opinion and/or your solution of course.</p>
  
  <p>UPDATE 2014-12-17:</p>
  
  <p>In the meantime I worked  on my own solution using timers and it seems to work pretty well. </p>
  
  <p>Connection timeout is straight forward. Simply set a timer to 5 seconds or whatever you think is good for you. If the timer expires and the device did not connect, simply cancel the connection and tell the user that there was a problem.</p>
  
  <p>Detecting devices that go out of range was a bit trickier. For every discovered device I start a timer that fires after double the time, the device sends advertisements. If the device does send another advertisement till the timer expires, it probably went out of range or was turned off or connected to another device.</p>
  
  <p>I don't want to answer my own question because I hope that maybe Apple will one day take care of those problems. </p>