Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLE Central Manager doesn't timeout scan after successful connection #85

Open
romansavrulin opened this issue Mar 14, 2019 · 6 comments

Comments

@romansavrulin
Copy link

Bluecap from Carthage, version 0.7.0.

The following simple code

let discoveryFuture = manager.startScanning(forServiceUUIDs: [CBUUID(string:uuid)], timeout:5.0).flatMap { peripheral -> FutureStream<Void> in
                log.info("scanFuture: Found peripheral UUID: \(peripheral.identifier.uuidString). Trying to connect")
                self.manager.stopScanning()
                self.peripheral = peripheral
            
                return peripheral.connect(connectionTimeout: 5, capacity: 5)
            }

will successfully timeout scan for not available service for the first time. But if I will scan for the service that is available and connect to the device, the second scan attempt for unavailable service will never timeout.

Is it a Bluecap or Core Bluetooth issue?

@troystribling
Copy link
Owner

That is the way it is implemented. You can stop the scan anytime after you connect.

@romansavrulin
Copy link
Author

@troystribling But the scan is stopped right after discovery. Why the second scan attempt doesn't employ timeout?

@romansavrulin
Copy link
Author

@troystribling Quick follow-up sequence

  1. disable the peripheral you are trying to discover
  2. start scanning for it with timeout
  3. you will get timeout as expected
  4. enable the peripheral you are trying to discover
  5. start scanning for it with timeout
  6. you will successfully connect and stop scanning in the future
  7. disconnect
  8. disable the peripheral you are trying to discover
  9. start scanning for it with timeout
  10. you won't get timeout for that discovery attempt

Am I missing something?

@troystribling
Copy link
Owner

Did not follow. That sounds like a bug. Start looking here. https://github.com/troystribling/BlueCap/blob/master/BlueCapKit/Central/CentralManager.swift#L212. The code is not that complicated. I do not have the time available now to look into this but will be will ing to accept a PR.

@romansavrulin
Copy link
Author

@troystribling Ok, thank you. Will dive into this, once we will be working on new maintenance release of our App

@romansavrulin
Copy link
Author

Quick try to fix the timeout inside the library wasn't successful, so I've worked-around this by the following code

 func startDiscovery(uuid:String){

     scanTimeoutTimer = Timer.scheduledTimer(withTimeInterval: 10.0, repeats: false) { _ in
            log.info("Scan timeout has passed")
            if self.manager.isScanning {
                self.manager.stopScanning()
                //trigger timeout event here
            }
            self.scanTimeoutTimer?.invalidate()
        }
                
        let discoveryFuture = manager.startScanning(forServiceUUIDs: [CBUUID(string:uuid)]).flatMap { peripheral -> FutureStream<Void> in
                log.info("scanFuture: Found peripheral UUID: \(peripheral.identifier.uuidString). Trying to connect")
                self.manager.stopScanning()
                self.scanTimeoutTimer?.invalidate()
                return peripheral.connect(connectionTimeout: 10, capacity: 1)
            }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants