BLEConnectUtils.swift 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. //
  2. // BLEConnectUtils.swift
  3. // SolarBT
  4. //
  5. // Created by weclouds on 2019/11/4.
  6. // Copyright © 2019 weclouds. All rights reserved.
  7. //
  8. import UIKit
  9. class BLEConnectUtils: NSObject {
  10. static let shared : BLEConnectUtils = {
  11. let tool = BLEConnectUtils()
  12. return tool
  13. }()
  14. var startWrite:(()->Void)?
  15. private var timer: DispatchSourceTimer?
  16. //查询是否可写
  17. func checkWriteStatus(_ startWrite : @escaping (()->Void)) {
  18. self.startWrite = startWrite
  19. timer = DispatchSource.makeTimerSource(flags: [], queue: DispatchQueue.global())
  20. //dealiyne 开始执行时间
  21. //repeating : 重复时间将
  22. //leewa : 时间精度
  23. timer?.schedule(deadline: .now() + .seconds(0), repeating: DispatchTimeInterval.seconds(1), leeway: DispatchTimeInterval.seconds(0))
  24. timer?.setEventHandler(handler: {
  25. self.timerAction()
  26. })
  27. timer?.resume()
  28. }
  29. @objc func timerAction() {
  30. let canWrite = BabyBluetoothSwift.shareInstance()?.canWrite()
  31. log.debug( "是否能写 -- \(canWrite)")
  32. if BabyBluetoothSwift.shareInstance()?.canWrite() == true {
  33. self.startWrite!()
  34. timer?.cancel()
  35. timer = nil
  36. }
  37. }
  38. }