BluetoothProvider.swift 2.5 KB

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