Skip to content

Commit ad3c3b1

Browse files
committed
Fix crashes
1 parent e8d9399 commit ad3c3b1

3 files changed

Lines changed: 19 additions & 2 deletions

File tree

InfiniLink/BLE/BLEManager.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
294294
isConnecting = false
295295
isScanning = false
296296

297+
if !isBluetoothOn {
298+
disconnect()
299+
}
300+
297301
if isBluetoothOn && !isConnectedToPinetime {
298302
startScanning()
299303
}
@@ -324,6 +328,11 @@ class BLEManager: NSObject, ObservableObject, CBCentralManagerDelegate, CBPeriph
324328
}
325329

326330
func peripheral(_ peripheral: CBPeripheral, didUpdateValueFor characteristic: CBCharacteristic, error: Error?) {
331+
guard error == nil else {
332+
log(error!.localizedDescription, caller: "didUpdateValueFor characteristic", target: .ble)
333+
return
334+
}
335+
327336
deviceManager.updateInfo(characteristic: characteristic)
328337
characteristicHandler.handleUpdates(characteristic: characteristic, peripheral: peripheral)
329338
}

InfiniLink/BLE/DeviceManager.swift

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -225,8 +225,12 @@ extension DeviceManager {
225225
case cbuuids.manufacturer:
226226
bleManager.pairedDevice.manufacturer = String(data: value, encoding: .utf8) ?? ""
227227
case cbuuids.blefsVersion:
228-
let byteArray = [UInt8](characteristic.value!)
229-
bleManager.pairedDevice.blefsVersion = String(Int(byteArray[1])) + String(Int(byteArray[0]))
228+
let byteArray = [UInt8](value)
229+
if byteArray.count >= 2 {
230+
bleManager.pairedDevice.blefsVersion = "\(Int(byteArray[1]))\(Int(byteArray[0]))"
231+
} else {
232+
bleManager.pairedDevice.blefsVersion = "00" // or some fallback
233+
}
230234
default:
231235
break
232236
}

InfiniLink/Utils/MusicController.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,10 @@ class MusicController {
120120
}
121121

122122
func convertTime(value: Double) -> [UInt8] {
123+
guard value.isFinite && !value.isNaN else {
124+
return [0, 0, 0, 0]
125+
}
126+
123127
let val32: UInt32 = UInt32(floor(value))
124128

125129
let byte1 = UInt8(val32 & 0x000000FF)

0 commit comments

Comments
 (0)