0

Using RxAndroidBle for Bluetooth LE connections. It works to scan and identify devices with a given Service UUID (and add them to a Vector). But when I add a spinlock (actually an AtomicBoolean) in a while loop to detect when the scan is finished, it no longer seems to find the devices.

public void scanForScopes() {
    Log.d( LOG_TAG, "Entered scanForScopes()" );
    rxBleDeviceVector.clear( );
    asDeviceVector.clear( );
    // since we added time and device count limits, we know this Observable will not run forever
    scanSubscriber = new AsScanSubscriber<>( ); 
    scanSubscription = asBleClient
            .scanBleDevices( asServiceIdArray )
            .subscribeOn( Schedulers.io() )
            .observeOn( AndroidSchedulers.mainThread( ) )
            .take( MAX_SCOPES )
            .take( SCAN_TIME, TimeUnit.SECONDS )
            .doOnNext( this::addRxBleScanResult ) // works
            .doOnCompleted( () -> Log.d( LOG_TAG, "Scan for scope devices has completed" ) ) // doesn't seem to be called
            .subscribe( scanSubscriber ); // calls scanSubscriber.onStart() (which sets nowScanning true)

I suspect that a couple of things may be going on:

  1. The main UI thread has forged ahead and tried to display an empty vector of discovered devices. If that's the case, how can I make it wait for the device scan to complete without bogging down the system? I think the RxJava platform eliminates the AsyncTask option.
  2. The while() loop that tests for scan completion may be consuming too many CPU cycles, preventing the scan from succeeding. How can I fix this?
Haris Qurashi
  • 2,104
  • 1
  • 13
  • 28
Robert Lewis
  • 1,847
  • 2
  • 18
  • 43
  • 1
    keep the code in a function that you want to execute after asynctask is completed. and call it on post execute of the asynctask. – Sagar Nayak Feb 20 '17 at 06:41
  • 1
    where is the while loop? why are you trying to wait actively? the Observable will run in async fashion and will fire the events when it will finish – yosriz Feb 20 '17 at 07:35

1 Answers1

0

Solution to the problem was to avoid it, by using scanSubscriber's onCompleted() callback to continue the program's flow.

Robert Lewis
  • 1,847
  • 2
  • 18
  • 43