@@ -306,11 +306,42 @@ bool BLECentral::scan(SecondsValue runSeconds,
306
306
BLECENTRAL_ENSURE_ADAPTER_AVAIL (failure);
307
307
308
308
if (is_scanning) {
309
+ /*
310
+ AsyncAction::Details asyncAlreadyRunningAct("scanrunning", scanCompleted, failure);
311
+ asyncAlreadyRunningAct.trigger = [](AsyncAction::Details * thisAction) {
312
+ thisAction->completedWithSuccess();
313
+ };
309
314
310
- BLECENTRAL_TRIGGER_CB_IFSET (failure, " already scanning" );
311
- return false ;
315
+ queueAsyncAction(asyncAlreadyRunningAct);
316
+ */
317
+ return true ;
312
318
}
313
319
320
+ if (connected_to.size () && ! numAsyncActionsQueued ()) {
321
+ BLECNTL_DEBUGLN (" asked to scan but we're connected and there's nothing queued to disconn... do that first" );
322
+
323
+ Device::Details * dets = deviceDetails (connected_to);
324
+ if (dets && dets->connection ) {
325
+ // we really seem to be still connected to this thing
326
+ AsyncAction::Details autoDisconnAction (" autodisconn" , [](){
327
+ BLECNTL_DEBUGLN (" auto disconn completed" );
328
+ }, [](){
329
+ BLECNTL_DEBUGLN (" hm, auto disconn failed?" );
330
+ });
331
+
332
+ UUID devId = dets->id ;
333
+
334
+ autoDisconnAction.trigger = [this , devId](AsyncAction::Details * thisAction){
335
+
336
+ this ->disconnect (devId, [](){
337
+ }, [](){
338
+ });
339
+ };
340
+
341
+ }
342
+ }
343
+
344
+
314
345
315
346
devDiscoveredCb = deviceDiscoveredCb;
316
347
@@ -320,7 +351,7 @@ bool BLECentral::scan(SecondsValue runSeconds,
320
351
if (gattlib_adapter_scan_enable_async (adapter, clib_device_discovered_callback,
321
352
runSeconds, clib_scan_complete_callback) != 0 )
322
353
{
323
-
354
+ this -> is_scanning = false ;
324
355
thisAction->completedWithFailure ();
325
356
}
326
357
@@ -329,27 +360,6 @@ bool BLECentral::scan(SecondsValue runSeconds,
329
360
330
361
queueAsyncAction (asyncAct);
331
362
332
-
333
-
334
- #if 0
335
- // let's push this onto the queue, because bluez has
336
- // a tendency to spit out all the cached deviced immediately,
337
- // and we want to ensure that
338
- // a) we're well into 'scanning mode' when the discovered callback triggers, and
339
- // b) we want the discovered cb to alway be called in the same context -- meaning
340
- // if the async processing happens is some thread, then every cb should happen
341
- // in that thread.
342
- reportQueue.push_back([runSeconds, this, failure](){
343
-
344
- if (gattlib_adapter_scan_enable_async(adapter, clib_device_discovered_callback,
345
- runSeconds, clib_scan_complete_callback) != 0)
346
- {
347
- BLECENTRAL_TRIGGER_CB_IFSET(failure, "scan enable call failed");
348
- }
349
-
350
- });
351
- #endif
352
-
353
363
is_scanning = true ;
354
364
return true ;
355
365
}
@@ -381,7 +391,13 @@ bool BLECentral::stopScan(Callbacks::SuccessNotification succ,
381
391
Callbacks::Error failure) {
382
392
BLECENTRAL_ENSURE_ADAPTER_AVAIL (failure);
383
393
if (! is_scanning) {
384
- BLECENTRAL_TRIGGER_CB_IFSET (failure, " not currently scanning" );
394
+
395
+ AsyncAction::Details alreadyDoneAct (" stopScanDone" , succ, failure);
396
+ alreadyDoneAct.trigger = [](AsyncAction::Details * thisAction) {
397
+ thisAction->completedWithSuccess ();
398
+ };
399
+ queueAsyncAction (alreadyDoneAct);
400
+ return true ;
385
401
}
386
402
387
403
// we are currently scanning, which means we have an async op running
@@ -962,7 +978,7 @@ void BLECentral::deviceDiscovered(const Discovery::Device & dev) {
962
978
963
979
void BLECentral::scanCompleted () {
964
980
965
- // is_scanning = false;
981
+ is_scanning = false ;
966
982
currentOpCompleted ();
967
983
968
984
}
0 commit comments