| 1 |
// |
|
| 2 |
// CBManagerState.swift |
|
| 3 |
// USB Meter |
|
| 4 |
// |
|
| 5 |
// Created by Bogdan Timofte on 02/03/2020. |
|
| 6 |
// Copyright © 2020 Bogdan Timofte. All rights reserved. |
|
| 7 |
// |
|
| 8 | ||
| 9 |
//import Foundation |
|
| 10 | ||
| 11 |
import CoreBluetooth |
|
| 12 |
import SwiftUI |
|
| 13 | ||
| 14 |
//Manager States |
|
| 15 |
//.poweredOff A state that indicates Bluetooth is currently powered off. |
|
| 16 |
//.poweredOn A state that indicates Bluetooth is currently powered on and available to use. |
|
| 17 |
//.resetting A state that indicates the connection with the system service was momentarily lost. |
|
| 18 |
//.unauthorized A state that indicates the application isn’t authorized to use the Bluetooth low energy role. |
|
| 19 |
//.unknown The manager’s state is unknown. |
|
| 20 |
//.unsupported A state that indicates this device doesn’t support the Bluetooth low energy central or client role. |
|
| 21 | ||
| 22 |
extension CBManagerState {
|
|
| 23 |
var description: String {
|
|
| 24 |
switch self {
|
|
| 25 |
case .poweredOff: |
|
| 26 |
return "CBManagerState.poweredOff" |
|
| 27 |
case .poweredOn: |
|
| 28 |
return "CBManagerState.poweredOn" |
|
| 29 |
case .resetting: |
|
| 30 |
return "CBManagerState.resetting" |
|
| 31 |
case .unauthorized: |
|
| 32 |
return "CBManagerState.unauthorized" |
|
| 33 |
case .unknown: |
|
| 34 |
return "CBManagerState.unknown" |
|
| 35 |
case .unsupported: |
|
| 36 |
return "CBManagerState.unsupported" |
|
| 37 |
default: |
|
| 38 |
return "CBManagerState.other" |
|
| 39 |
} |
|
| 40 |
} |
|
| 41 | ||
| 42 |
var color: Color {
|
|
| 43 |
switch self {
|
|
| 44 |
case .poweredOff: |
|
| 45 |
return Color.red |
|
| 46 |
case .poweredOn: |
|
| 47 |
return Color.blue |
|
| 48 |
case .resetting: |
|
| 49 |
return Color.green |
|
| 50 |
case .unauthorized: |
|
| 51 |
return Color.orange |
|
| 52 |
case .unknown: |
|
| 53 |
return Color.secondary |
|
| 54 |
case .unsupported: |
|
| 55 |
return Color.gray |
|
| 56 |
default: |
|
| 57 |
return Color.yellow |
|
| 58 |
} |
|
| 59 |
} |
|
| 60 | ||
| 61 |
var helpView: AnyView {
|
|
| 62 |
switch self {
|
|
| 63 |
case .poweredOff: |
|
| 64 |
return AnyView(poweredOffHelperView()) |
|
| 65 |
case .poweredOn: |
|
| 66 |
return AnyView(poweredOnHelperView()) |
|
| 67 |
case .resetting: |
|
| 68 |
return AnyView(resettingHelperView()) |
|
| 69 |
case .unauthorized: |
|
| 70 |
return AnyView(unauthorizedHelperView()) |
|
| 71 |
case .unknown: |
|
| 72 |
return AnyView(unknownHelperView()) |
|
| 73 |
case .unsupported: |
|
| 74 |
return AnyView(unsupportedHelperView()) |
|
| 75 |
default: |
|
| 76 |
return AnyView(defaultHelperView()) |
|
| 77 |
} |
|
| 78 |
} |
|
| 79 | ||
| 80 |
private struct poweredOffHelperView: View {
|
|
| 81 |
var body: some View {
|
|
| 82 |
Text("Bluetooth is turned off on this device. Tou can turn it on in Settings->Bluetooth")
|
|
| 83 |
} |
|
| 84 |
} |
|
| 85 |
private struct poweredOnHelperView: View {
|
|
| 86 |
var body: some View {
|
|
| 87 |
Text("Bluetooth is up an running")
|
|
| 88 |
} |
|
| 89 |
} |
|
| 90 | ||
| 91 |
private struct resettingHelperView: View {
|
|
| 92 |
var body: some View {
|
|
| 93 |
VStack {
|
|
| 94 |
Text("Bluetooth is resetting")
|
|
| 95 |
Text("Maybe wait for a while...")
|
|
| 96 |
} |
|
| 97 |
} |
|
| 98 |
} |
|
| 99 | ||
| 100 |
private struct unauthorizedHelperView: View {
|
|
| 101 |
var body: some View {
|
|
| 102 |
HStack {
|
|
| 103 |
Text("This application does not have permission to access Bluetooth. You can give it in ")
|
|
| 104 |
Button(action: { UIApplication.shared.open(URL(string: UIApplication.openSettingsURLString)!, options: [:], completionHandler: nil) }) {
|
|
| 105 |
Text("Settings")
|
|
| 106 |
} |
|
| 107 |
} |
|
| 108 |
} |
|
| 109 |
} |
|
| 110 | ||
| 111 |
private struct unknownHelperView: View {
|
|
| 112 |
var body: some View {
|
|
| 113 |
VStack {
|
|
| 114 |
Text("Bluetooth state is unknown!")
|
|
| 115 |
Text("There is no help available for this situation.")
|
|
| 116 |
} |
|
| 117 |
} |
|
| 118 |
} |
|
| 119 | ||
| 120 |
private struct unsupportedHelperView: View {
|
|
| 121 |
var body: some View {
|
|
| 122 |
VStack {
|
|
| 123 |
Text("Your device does not have required capabilities to establish Bluetooth connections with USB Meters")
|
|
| 124 |
Text("There is no help available for this situation.")
|
|
| 125 |
} |
|
| 126 |
} |
|
| 127 |
} |
|
| 128 | ||
| 129 |
private struct defaultHelperView: View {
|
|
| 130 |
var body: some View {
|
|
| 131 |
VStack {
|
|
| 132 |
Text("Unknown Bluetooth state.")
|
|
| 133 |
Text("You may contact develloper.")
|
|
| 134 |
} |
|
| 135 |
} |
|
| 136 |
} |
|
| 137 |
} |