BluetoothProvider.swift 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. //
  2. // BluetoothProvider.swift
  3. // LoopKit
  4. //
  5. // Created by Darin Krauss on 3/1/21.
  6. // Copyright © 2021 LoopKit Authors. All rights reserved.
  7. //
  8. import Foundation
  9. public enum BluetoothAuthorization: Int {
  10. /// User has not yet made a choice regarding whether the application may use Bluetooth.
  11. case notDetermined
  12. /// This application is not authorized to use Bluetooth. The user cannot change this application’s status,
  13. case restricted
  14. /// User has explicitly denied this application from using Bluetooth.
  15. case denied
  16. /// User has authorized this application to use Bluetooth.
  17. case authorized
  18. }
  19. public enum BluetoothState: Int {
  20. /// State unknown, update imminent.
  21. case unknown
  22. /// The connection with the system service was momentarily lost, update imminent.
  23. case resetting
  24. /// The platform doesn't support the Bluetooth Low Energy Central/Client role.
  25. case unsupported
  26. /// The application is not authorized to use the Bluetooth Low Energy role.
  27. case unauthorized
  28. /// Bluetooth is currently powered off.
  29. case poweredOff
  30. /// Bluetooth is currently powered on and available to use.
  31. case poweredOn
  32. }
  33. public protocol BluetoothObserver: AnyObject {
  34. /// Informs the observer that the Bluetooth state has changed to the given value.
  35. ///
  36. /// - Parameters:
  37. /// - state: The latest Bluetooth state.
  38. func bluetoothDidUpdateState(_ state: BluetoothState)
  39. }
  40. public protocol BluetoothProvider: AnyObject {
  41. /// The current Bluetooth authorization.
  42. var bluetoothAuthorization: BluetoothAuthorization { get }
  43. /// The current Bluetooth state. If Bluetooth has not been authorized then returns .unknown.
  44. var bluetoothState: BluetoothState { get }
  45. /// Authorize Bluetooth. Should only be invoked if bluetoothAuthorization is .notDetermined.
  46. ///
  47. /// - Parameters:
  48. /// - completion: Invoked when Bluetooth authorization is complete along with the resulting authorization.
  49. func authorizeBluetooth(_ completion: @escaping (BluetoothAuthorization) -> Void)
  50. /// Start observing Bluetooth changes.
  51. ///
  52. /// - Parameters:
  53. /// - observer: The observer observing Bluetooth changes.
  54. /// - queue: The Dispatch queue upon which to notify the observer of Bluetooth changes.
  55. func addBluetoothObserver(_ observer: BluetoothObserver, queue: DispatchQueue)
  56. /// Stop observing Bluetooth changes.
  57. ///
  58. /// - Parameters:
  59. /// - observer: The observer observing Bluetooth changes.
  60. func removeBluetoothObserver(_ observer: BluetoothObserver)
  61. }