jordanebelanger / SwiftyBluetooth

Closures based APIs for CoreBluetooth
MIT License
209 stars 66 forks source link

terminating scan early #18

Closed TaiPhamD closed 7 years ago

TaiPhamD commented 7 years ago

Hi , Once I am in a scanForPeripherals(withServiceUUIDs: nil, timeoutAfter: 15) { scanResult in

  case .scanResult ...{
         //I found some condition that I want to exit the scan early how?
   }

how do I terminate this scan early while I am working inside the closure code?

jordanebelanger commented 7 years ago

Central.sharedInstance.stopScan() even from inside the closure should terminate the scan preemptively.

TaiPhamD commented 7 years ago

would SwiftyBluetooth.stopScan() do the same thing too?

--update I found the source code it does do the same thing :) . Thank you! `import CoreBluetooth

// MARK: Shorthands for the Central singleton instance interface

/// Scans for Peripherals through a CBCentralManager scanForPeripheralsWithServices(...) function call. /// /// - Parameter timeout: The scanning time in seconds before the scan is stopped and the completion closure is called with a scanStopped result. /// - Parameter serviceUUIDs: The service UUIDs to search peripherals for or nil if looking for all peripherals. /// - Parameter completion: The closures, called multiple times throughout a scan. public func scanForPeripherals(withServiceUUIDs serviceUUIDs: [CBUUIDConvertible]? = nil, timeoutAfter timeout: TimeInterval, completion: @escaping PeripheralScanCallback) { Central.sharedInstance.scanForPeripherals(withServiceUUIDs: serviceUUIDs, timeoutAfter: timeout, completion: completion) }

/// Will stop the current scan through a CBCentralManager stopScan() function call and invokes the completion /// closures of the original scanWithTimeout function call with a scanStopped result containing an error if something went wrong. public func stopScan() { Central.sharedInstance.stopScan() }`