Skip to content

Commit c6ff2c0

Browse files
committed
- Fixed non-ASCII writes to PineTime -- non-ASCII writes to notifications return void (aka do nothing), and non-ASCII music app updates write an empty string instead to remove the "not playing" text from InfiniTime music app.
- Moved currentTime function from BLEManager extension to its own class. - Added error handling for currentTime, as there was a TestFlight crash report from the current time being an unexpected nil value during setup. This should keep the app from crashing, but doesn't tell the user what's happening yet.
1 parent fe133b3 commit c6ff2c0

4 files changed

Lines changed: 74 additions & 44 deletions

File tree

Infini-iOS.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
263B20BC26CDF20400676BF0 /* BatteryChart.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263B20BB26CDF20400676BF0 /* BatteryChart.swift */; };
1414
263B20C526CEB46800676BF0 /* SwiftUICharts in Frameworks */ = {isa = PBXBuildFile; productRef = 263B20C426CEB46800676BF0 /* SwiftUICharts */; };
1515
263B20C726CF07BB00676BF0 /* DFUProgressBar.swift in Sources */ = {isa = PBXBuildFile; fileRef = 263B20C626CF07BB00676BF0 /* DFUProgressBar.swift */; };
16+
2644261226DC093D009BD54A /* SetTime.swift in Sources */ = {isa = PBXBuildFile; fileRef = 2644261126DC093D009BD54A /* SetTime.swift */; };
1617
264BFE4226BC51CE0050A223 /* Infini_iOSApp.swift in Sources */ = {isa = PBXBuildFile; fileRef = 264BFE4126BC51CE0050A223 /* Infini_iOSApp.swift */; };
1718
264BFE4426BC51CE0050A223 /* ContentView.swift in Sources */ = {isa = PBXBuildFile; fileRef = 264BFE4326BC51CE0050A223 /* ContentView.swift */; };
1819
264BFE4626BC51CF0050A223 /* Assets.xcassets in Resources */ = {isa = PBXBuildFile; fileRef = 264BFE4526BC51CF0050A223 /* Assets.xcassets */; };
@@ -65,6 +66,7 @@
6566
263B20B926CDF13400676BF0 /* HeartChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = HeartChart.swift; sourceTree = "<group>"; };
6667
263B20BB26CDF20400676BF0 /* BatteryChart.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatteryChart.swift; sourceTree = "<group>"; };
6768
263B20C626CF07BB00676BF0 /* DFUProgressBar.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DFUProgressBar.swift; sourceTree = "<group>"; };
69+
2644261126DC093D009BD54A /* SetTime.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = SetTime.swift; sourceTree = "<group>"; };
6870
264BFE3E26BC51CE0050A223 /* Infini-iOS.app */ = {isa = PBXFileReference; explicitFileType = wrapper.application; includeInIndex = 0; path = "Infini-iOS.app"; sourceTree = BUILT_PRODUCTS_DIR; };
6971
264BFE4126BC51CE0050A223 /* Infini_iOSApp.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = Infini_iOSApp.swift; sourceTree = "<group>"; };
7072
264BFE4326BC51CE0050A223 /* ContentView.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ContentView.swift; sourceTree = "<group>"; };
@@ -260,6 +262,7 @@
260262
264BFE7326BC526C0050A223 /* BLEManager.swift */,
261263
26D7816426C9EB3800BBF555 /* BLEDelegates.swift */,
262264
26D7816626C9EC3100BBF555 /* BLEManagerExtensions.swift */,
265+
2644261126DC093D009BD54A /* SetTime.swift */,
263266
);
264267
path = BLE;
265268
sourceTree = "<group>";
@@ -449,6 +452,7 @@
449452
26D7817326CAB27800BBF555 /* WatchNotificationsView.swift in Sources */,
450453
26D7817526CAB79200BBF555 /* PhoneNotificationsView.swift in Sources */,
451454
263B20BC26CDF20400676BF0 /* BatteryChart.swift in Sources */,
455+
2644261226DC093D009BD54A /* SetTime.swift in Sources */,
452456
263B20B526CD673900676BF0 /* StatusViewTabs.swift in Sources */,
453457
26D7816E26CA003B00BBF555 /* SettingsFunctions.swift in Sources */,
454458
26A6316326C4C6D6005AE404 /* BLEConnectView.swift in Sources */,

Infini-iOS/BLE/BLEDelegates.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ extension BLEManager: CBPeripheralDelegate {
8686

8787
case timeCBUUID:
8888
// convert string with hex value of time to actual hex data, then write to PineTime
89-
peripheral.writeValue(currentTime().hexData, for: characteristic, type: .withResponse)
89+
peripheral.writeValue(SetTime().currentTime().hexData /*currentTime().hexData*/, for: characteristic, type: .withResponse)
9090

9191
case firmwareCBUUID:
9292
firmwareVersion = String(decoding: characteristic.value!, as: UTF8.self)

Infini-iOS/BLE/BLEManagerExtensions.swift

Lines changed: 8 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,14 @@ import SwiftUICharts
1414
extension BLEManager {
1515

1616
// this function converts string to ascii and writes to the selected characteristic. Used for notifications and music app
17-
func writeASCIIToPineTime(message: String, characteristic: CBCharacteristic) {
18-
let writeData = message.data(using: .ascii)!
17+
func writeASCIIToPineTime(message: String, characteristic: CBCharacteristic) -> Void {
18+
guard let writeData = message.data(using: .ascii) else {
19+
if characteristic == musicChars.artist || characteristic == musicChars.track {
20+
// TODO: for music app, this sends an empty string to not display anything if this is non-ascii. This string can be changed to a "cannot display song title" or whatever but that seems a lot more annoying than just displaying nothing.
21+
infiniTime.writeValue("".data(using: .ascii)!, for: characteristic, type: .withResponse)
22+
}
23+
return
24+
}
1925
infiniTime.writeValue(writeData, for: characteristic, type: .withResponse)
2026
}
2127

@@ -40,45 +46,4 @@ extension BLEManager {
4046
dateFormat.dateFormat = "H:mm:ss"
4147
return LineChartDataPoint(value: data, xAxisLabel: "Time", description: dateFormat.string(from: Date()), date: Date())
4248
}
43-
44-
// this function pulls date from phone, shuffles it into the correct order, and then hex-encodes it to a format that InfiniTime can understand
45-
func currentTime() -> String {
46-
let now = Date() // current time
47-
48-
// formatting setup for the date, not including the year because we have to reformat the year hex to match what the PT expects
49-
let dateFormatter = DateFormatter()
50-
dateFormatter.dateFormat = "MM dd H m s e SSSS"
51-
52-
// prepare formatting for year
53-
let yearFormatter = DateFormatter()
54-
yearFormatter.dateFormat = "y"
55-
let yearString = yearFormatter.string(from: now)
56-
let intYear = Int(yearString)
57-
58-
// convert year string to hex-encoded string. conditionally prepend 0 in case by some miracle this application and your watch is still functional in the year 4096
59-
var hexYear = String (format: "%02X", intYear!)
60-
if hexYear.count == 3 {
61-
hexYear.insert("0", at: hexYear.startIndex)
62-
}
63-
64-
// infinitime (and BLE in general? I dunno...) requires the MSB first, so we have to switch the year from XXYY to YYXX
65-
var revYearChars = hexYear.suffix(2)
66-
revYearChars += hexYear.prefix(2)
67-
68-
var fullDateString = String(revYearChars)
69-
70-
let dateString = dateFormatter.string(from: now)
71-
let dateParts = dateString.components(separatedBy: " ")
72-
73-
// convert the rest of the date parts to hex, and append them to the date string
74-
75-
for part in dateParts {
76-
let intPart = Int(part)
77-
let hex = String(format: "%02X", intPart!)
78-
fullDateString.append(hex)
79-
}
80-
81-
// print(fullDateString) // debug
82-
return fullDateString
83-
}
8449
}

Infini-iOS/BLE/SetTime.swift

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// SetTime.swift
3+
// Infini-iOS
4+
//
5+
// Created by Alex Emry on 8/29/21.
6+
//
7+
//
8+
9+
10+
import Foundation
11+
12+
class SetTime {
13+
14+
enum SetTimeError: Error {
15+
case nilValue
16+
}
17+
18+
// this function pulls date from phone, shuffles it into the correct order, and then hex-encodes it to a format that InfiniTime can understand
19+
func currentTime() throws -> String {
20+
let now = Date() // current time
21+
22+
// formatting setup for the date, not including the year because we have to reformat the year hex to match what the PT expects
23+
let dateFormatter = DateFormatter()
24+
dateFormatter.dateFormat = "MM dd H m s e SSSS"
25+
26+
// prepare formatting for year
27+
let yearFormatter = DateFormatter()
28+
yearFormatter.dateFormat = "y"
29+
let yearString = yearFormatter.string(from: now)
30+
let intYear = Int(yearString)
31+
32+
// convert year string to hex-encoded string. conditionally prepend 0 in case by some miracle this application and your watch is still functional in the year 4096
33+
var hexYear = String (format: "%02X", intYear!)
34+
if hexYear.count == 3 {
35+
hexYear.insert("0", at: hexYear.startIndex)
36+
}
37+
38+
// infinitime (and BLE in general? I dunno...) requires the MSB first, so we have to switch the year from XXYY to YYXX
39+
var revYearChars = hexYear.suffix(2)
40+
revYearChars += hexYear.prefix(2)
41+
42+
var fullDateString = String(revYearChars)
43+
44+
let dateString = dateFormatter.string(from: now)
45+
//let dateParts = dateString.components(separatedBy: " ")
46+
47+
// convert the rest of the date parts to hex, and append them to the date string
48+
49+
for part in dateString.components(separatedBy: " ") {
50+
guard let intPart = Int(part) else {
51+
throw SetTimeError.nilValue
52+
}
53+
let hex = String(format: "%02X", intPart)
54+
fullDateString.append(hex)
55+
56+
}
57+
58+
// print(fullDateString) // debug
59+
return fullDateString
60+
}
61+
}

0 commit comments

Comments
 (0)