Skip to content

Commit

Permalink
iOS Example: refactored slightly to make example more readable
Browse files Browse the repository at this point in the history
  • Loading branch information
JOikarinen committed Jun 2, 2021
1 parent 8ba102e commit 8d2ce46
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 26 deletions.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@

/* Begin PBXBuildFile section */
A4B37D34C263EBFC51C2D60B /* Pods_iosBleSdkTestApp.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = 20983D0AB4CED8600A47BC36 /* Pods_iosBleSdkTestApp.framework */; };
A5367D1726676CB100CDDEBE /* Messages.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5367D1626676CB100CDDEBE /* Messages.swift */; };
A5776BA62663A28700A36BA5 /* iosBleSdkTestApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5776BA52663A28700A36BA5 /* iosBleSdkTestApp.swift */; };
A5776BA82663A28700A36BA5 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5776BA72663A28700A36BA5 /* ContentView.swift */; };
A5776BAA2663A28800A36BA5 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A5776BA92663A28800A36BA5 /* Assets.xcassets */; };
A5776BAD2663A28800A36BA5 /* Preview Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = A5776BAC2663A28800A36BA5 /* Preview Assets.xcassets */; };
A5776BB82663A28800A36BA5 /* iosBleSdkTestAppTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5776BB72663A28800A36BA5 /* iosBleSdkTestAppTests.swift */; };
A5776BC32663A28800A36BA5 /* iosBleSdkTestAppUITests.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5776BC22663A28800A36BA5 /* iosBleSdkTestAppUITests.swift */; };
A5776BD12663A58E00A36BA5 /* ViewModel.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5776BD02663A58E00A36BA5 /* ViewModel.swift */; };
A5776BD12663A58E00A36BA5 /* PolarBleSdkManager.swift in Sources */ = {isa = PBXBuildFile; fileRef = A5776BD02663A58E00A36BA5 /* PolarBleSdkManager.swift */; };
/* End PBXBuildFile section */

/* Begin PBXContainerItemProxy section */
Expand All @@ -36,6 +37,7 @@

/* Begin PBXFileReference section */
20983D0AB4CED8600A47BC36 /* Pods_iosBleSdkTestApp.framework */ = {isa = PBXFileReference; explicitFileType = wrapper.framework; includeInIndex = 0; path = Pods_iosBleSdkTestApp.framework; sourceTree = BUILT_PRODUCTS_DIR; };
A5367D1626676CB100CDDEBE /* Messages.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Messages.swift; sourceTree = "<group>"; };
A5776BA22663A28700A36BA5 /* iosBleSdkTestApp.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = iosBleSdkTestApp.app; sourceTree = BUILT_PRODUCTS_DIR; };
A5776BA52663A28700A36BA5 /* iosBleSdkTestApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iosBleSdkTestApp.swift; sourceTree = "<group>"; };
A5776BA72663A28700A36BA5 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
Expand All @@ -48,7 +50,7 @@
A5776BBE2663A28800A36BA5 /* iosBleSdkTestAppUITests.xctest */ = {isa = PBXFileReference; explicitFileType = wrapper.cfbundle; includeInIndex = 0; path = iosBleSdkTestAppUITests.xctest; sourceTree = BUILT_PRODUCTS_DIR; };
A5776BC22663A28800A36BA5 /* iosBleSdkTestAppUITests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = iosBleSdkTestAppUITests.swift; sourceTree = "<group>"; };
A5776BC42663A28800A36BA5 /* Info.plist */ = {isa = PBXFileReference; lastKnownFileType = text.plist.xml; path = Info.plist; sourceTree = "<group>"; };
A5776BD02663A58E00A36BA5 /* ViewModel.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ViewModel.swift; sourceTree = "<group>"; };
A5776BD02663A58E00A36BA5 /* PolarBleSdkManager.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PolarBleSdkManager.swift; sourceTree = "<group>"; };
B1DB285981A115801B3D58A6 /* Pods-iosBleSdkTestApp.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iosBleSdkTestApp.debug.xcconfig"; path = "Target Support Files/Pods-iosBleSdkTestApp/Pods-iosBleSdkTestApp.debug.xcconfig"; sourceTree = "<group>"; };
DF07E3AF5F24477508A18851 /* Pods-iosBleSdkTestApp.release.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-iosBleSdkTestApp.release.xcconfig"; path = "Target Support Files/Pods-iosBleSdkTestApp/Pods-iosBleSdkTestApp.release.xcconfig"; sourceTree = "<group>"; };
/* End PBXFileReference section */
Expand Down Expand Up @@ -88,6 +90,22 @@
path = Pods;
sourceTree = "<group>";
};
A5367D1426676C7100CDDEBE /* Views */ = {
isa = PBXGroup;
children = (
A5776BA72663A28700A36BA5 /* ContentView.swift */,
);
path = Views;
sourceTree = "<group>";
};
A5367D1526676C8F00CDDEBE /* Models */ = {
isa = PBXGroup;
children = (
A5367D1626676CB100CDDEBE /* Messages.swift */,
);
path = Models;
sourceTree = "<group>";
};
A5776B992663A28700A36BA5 = {
isa = PBXGroup;
children = (
Expand All @@ -113,12 +131,13 @@
A5776BA42663A28700A36BA5 /* iosBleSdkTestApp */ = {
isa = PBXGroup;
children = (
A5367D1526676C8F00CDDEBE /* Models */,
A5367D1426676C7100CDDEBE /* Views */,
A5776BA52663A28700A36BA5 /* iosBleSdkTestApp.swift */,
A5776BA72663A28700A36BA5 /* ContentView.swift */,
A5776BA92663A28800A36BA5 /* Assets.xcassets */,
A5776BAE2663A28800A36BA5 /* Info.plist */,
A5776BAB2663A28800A36BA5 /* Preview Content */,
A5776BD02663A58E00A36BA5 /* ViewModel.swift */,
A5776BD02663A58E00A36BA5 /* PolarBleSdkManager.swift */,
);
path = iosBleSdkTestApp;
sourceTree = "<group>";
Expand Down Expand Up @@ -330,8 +349,9 @@
isa = PBXSourcesBuildPhase;
buildActionMask = 2147483647;
files = (
A5367D1726676CB100CDDEBE /* Messages.swift in Sources */,
A5776BA82663A28700A36BA5 /* ContentView.swift in Sources */,
A5776BD12663A58E00A36BA5 /* ViewModel.swift in Sources */,
A5776BD12663A58E00A36BA5 /* PolarBleSdkManager.swift in Sources */,
A5776BA62663A28700A36BA5 /* iosBleSdkTestApp.swift in Sources */,
);
runOnlyForDeploymentPostprocessing = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/// Copyright © 2019 Polar Electro Oy. All rights reserved.

import Foundation

struct ErrorMessage: Identifiable {
let id = UUID()
let text: String
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,13 @@ import PolarBleSdk
import RxSwift
import CoreBluetooth

class ViewModel : ObservableObject {
class PolarBleSdkManager : ObservableObject {

// NOTICE this example utilizes all available features
private var api = PolarBleApiDefaultImpl.polarImplementation(DispatchQueue.main, features: Features.allFeatures.rawValue)

private var deviceId = "8C4CAD2D" //TODO replace this with your device id

private var broadcastDisposable: Disposable?
private var autoConnectDisposable: Disposable?
private var searchDisposable: Disposable?
Expand All @@ -19,8 +22,6 @@ class ViewModel : ObservableObject {
private var ppgDisposable: Disposable?
private var ppiDisposable: Disposable?
private var exerciseEntry: PolarExerciseEntry?
private var deviceId = "8C4CAD2D" //TODO replace this with your device id

@Published private(set) var bluetoothPowerOn: Bool
@Published private(set) var broadcastEnabled: Bool = false
@Published private(set) var seachEnabled: Bool = false
Expand All @@ -32,6 +33,7 @@ class ViewModel : ObservableObject {
@Published private(set) var ppiEnabled: Bool = false
@Published private(set) var sdkModeEnabled: Bool = false
@Published private(set) var deviceConnectionState: ConnectionState = ConnectionState.disconnected
@Published var error: ErrorMessage? = nil

init() {
self.bluetoothPowerOn = api.isBlePowered
Expand Down Expand Up @@ -289,7 +291,7 @@ class ViewModel : ObservableObject {
NSLog(" PPI: \(item.ppInMs) sample.blockerBit: \(item.blockerBit) errorEstimate: \(item.ppErrorEstimate)")
}
case .error(let err):
NSLog("PPI stream failed: \(err)")
self.somethingFailed(text: "PPI stream failed: \(err)")
self.ppiEnabled = false
case .completed:
NSLog("PPI stream completed")
Expand All @@ -302,7 +304,7 @@ class ViewModel : ObservableObject {
ppiDisposable = nil
}
}

func sdkModeEnable() {
if case .connected(let deviceId) = deviceConnectionState {
_ = api.enableSDKMode(deviceId)
Expand All @@ -313,7 +315,7 @@ class ViewModel : ObservableObject {
NSLog("SDK mode enabled")
self.sdkModeEnabled = true
case .error(let err):
NSLog("SDK mode enable failed: \(err)")
self.somethingFailed(text: "SDK mode enable failed: \(err)")
}
}
}
Expand All @@ -329,7 +331,7 @@ class ViewModel : ObservableObject {
NSLog("SDK mode disabled")
self.sdkModeEnabled = false
case .error(let err):
NSLog("SDK mode disable failed: \(err)")
self.somethingFailed(text: "SDK mode disable failed: \(err)")
}
}
}
Expand All @@ -356,6 +358,7 @@ class ViewModel : ObservableObject {
func readExercise() {
if case .connected(let deviceId) = deviceConnectionState {
guard let e = exerciseEntry else {
somethingFailed(text: "No exercise to read, please list the exercises first")
return
}
_ = api.fetchExercise(deviceId, entry: e)
Expand All @@ -374,6 +377,7 @@ class ViewModel : ObservableObject {
func removeExercise() {
if case .connected(let deviceId) = deviceConnectionState {
guard let e = exerciseEntry else {
somethingFailed(text: "No exercise to read, please list the exercises first")
return
}
_ = api.removeExercise(deviceId, entry: e)
Expand Down Expand Up @@ -445,26 +449,33 @@ class ViewModel : ObservableObject {
case .completed:
NSLog("time set to device completed")
case .error(let err):
NSLog("time set failed: \(err)")
self.somethingFailed(text: "time set failed: \(err)")
}
}
}
}

private func somethingFailed(text: String) {
error = ErrorMessage(text:text)
NSLog("Error \(text)")
}
}

// MARK: - PolarBleApiPowerStateObserver
extension ViewModel : PolarBleApiPowerStateObserver {
extension PolarBleSdkManager : PolarBleApiPowerStateObserver {
func blePowerOn() {
NSLog("BLE ON")
bluetoothPowerOn = true
}

func blePowerOff() {
NSLog("BLE OFF")
bluetoothPowerOn = false
}
}

// MARK: - PolarBleApiObserver
extension ViewModel : PolarBleApiObserver {
extension PolarBleSdkManager : PolarBleApiObserver {
func deviceConnecting(_ polarDeviceInfo: PolarDeviceInfo) {
NSLog("DEVICE CONNECTING: \(polarDeviceInfo)")
deviceConnectionState = ConnectionState.connecting(polarDeviceInfo.deviceId)
Expand All @@ -483,7 +494,7 @@ extension ViewModel : PolarBleApiObserver {
}

// MARK: - PolarBleApiDeviceInfoObserver
extension ViewModel : PolarBleApiDeviceInfoObserver {
extension PolarBleSdkManager : PolarBleApiDeviceInfoObserver {
func batteryLevelReceived(_ identifier: String, batteryLevel: UInt) {
NSLog("battery level updated: \(batteryLevel)")
}
Expand All @@ -494,7 +505,7 @@ extension ViewModel : PolarBleApiDeviceInfoObserver {
}

// MARK: - PolarBleApiSdkModeFeatureObserver
extension ViewModel : PolarBleApiDeviceFeaturesObserver {
extension PolarBleSdkManager : PolarBleApiDeviceFeaturesObserver {
func hrFeatureReady(_ identifier: String) {
NSLog("HR ready")
}
Expand All @@ -511,27 +522,27 @@ extension ViewModel : PolarBleApiDeviceFeaturesObserver {
}

// MARK: - PolarBleApiSdkModeFeatureObserver
extension ViewModel : PolarBleApiSdkModeFeatureObserver {
extension PolarBleSdkManager : PolarBleApiSdkModeFeatureObserver {
func sdkModeFeatureAvailable(_ identifier: String) {
NSLog("SDK mode feature available. Device \(identifier)")
}
}

// MARK: - PolarBleApiDeviceHrObserver
extension ViewModel : PolarBleApiDeviceHrObserver {
extension PolarBleSdkManager : PolarBleApiDeviceHrObserver {
func hrValueReceived(_ identifier: String, data: PolarHrData) {
NSLog("(\(identifier)) HR value: \(data.hr) rrsMs: \(data.rrsMs) rrs: \(data.rrs) contact: \(data.contact) contact supported: \(data.contactSupported)")
}
}

// MARK: - PolarBleApiLogger
extension ViewModel : PolarBleApiLogger {
extension PolarBleSdkManager : PolarBleApiLogger {
func message(_ str: String) {
NSLog("Polar SDK log: \(str)")
}
}

extension ViewModel {
extension PolarBleSdkManager {
enum ConnectionState {
case disconnected
case connecting(String)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import SwiftUI

struct ContentView: View {
@StateObject var viewModel: ViewModel
@ObservedObject var viewModel: PolarBleSdkManager

var body: some View {
VStack {
Expand Down Expand Up @@ -100,20 +100,30 @@ struct ContentView: View {
Group {
Button("Set time", action: { viewModel.setTime()})
}
}
}.frame(maxWidth: .infinity)
}.frame(maxWidth: .infinity)
}
} else {
Text("Bluetooth OFF")
.bold()
.foregroundColor(.red)
Spacer()
}
}.alert(item: $viewModel.error) { message in
Alert(
title: Text(message.text),
dismissButton: .cancel()
)
}
}
}

struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView(viewModel: ViewModel())
ForEach(["iPhone 8", "iPAD Pro (12.9-inch)"], id: \.self) { deviceName in
ContentView(viewModel: PolarBleSdkManager())
.previewDevice(PreviewDevice(rawValue: deviceName))
.previewDisplayName(deviceName)
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,12 @@ import SwiftUI

@main
struct iosBleSdkTestApp: App {

@StateObject var viewModel = PolarBleSdkManager()

var body: some Scene {
WindowGroup {
ContentView(viewModel: ViewModel())
ContentView(viewModel: viewModel)
}
}
}

0 comments on commit 8d2ce46

Please sign in to comment.