https://github.com/fitnesskit/fitdataprotocol
Swift Implementation the Garmin Flexible and Interoperable Data Transfer Protocol.
https://github.com/fitnesskit/fitdataprotocol
fit garmin-fit spm swift swift-package-manager
Last synced: 2 months ago
JSON representation
Swift Implementation the Garmin Flexible and Interoperable Data Transfer Protocol.
- Host: GitHub
- URL: https://github.com/fitnesskit/fitdataprotocol
- Owner: FitnessKit
- License: mit
- Created: 2018-01-28T19:38:43.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2023-09-30T20:12:58.000Z (over 1 year ago)
- Last Synced: 2024-11-06T13:57:09.963Z (8 months ago)
- Topics: fit, garmin-fit, spm, swift, swift-package-manager
- Language: Swift
- Homepage: https://fitnesskit.github.io/FitDataProtocol/
- Size: 68.6 MB
- Stars: 49
- Watchers: 10
- Forks: 15
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# FitDataProtocol
[](https://developer.apple.com/swift)
[](http://cocoapods.org/pods/FitDataProtocol)
[](http://cocoapods.org/pods/FitDataProtocol)
[](http://cocoapods.org/pods/FitDataProtocol)Swift Version of the Garmin Flexible and Interoperable Data Transfer Protocol.
Supports SDK Revision 21.16.0
## Installation
FitDataProtocol is available through [CocoaPods](http://cocoapods.org). To install it, simply add the following line to your Podfile:
```ruby
pod 'FitDataProtocol'
```Swift Package Manager:
```swift
dependencies: [
.package(url: "https://github.com/FitnessKit/FitDataProtocol", from: "2.1.4")
]
```## How to Use
### Decoding FIT Files
```swift
let fileUrl = URL(fileURLWithPath: "WeightScaleMultiUser" + ".fit")
let fileData = try? Data(contentsOf: fileUrl)if let fileData = fileData {
var decoder = FitFileDecoder(crcCheckingStrategy: .throws)do {
try decoder.decode(data: fileData,
messages: FitFileDecoder.defaultMessages,
decoded: { (message: FitMessage) inprint("Got Message: \(message)")
if let message = message as? FileIdMessage {
print("mssage", message.deviceSerialNumber)
}if let message = message as? RecordMessage {
records.append(message)
}if let message = message as? SportMessage {
sports.append(message)
}if let message = message as? ActivityMessage {
activity.append(message)
}
})} catch {
print(error)
}
}
```### Encoding FIT Files
As part of the Encoding of the FIT Files you can check for Validity of the data you are encoding.
The options are
* none - No Validity Checks are done
* fileType - Based on the File Type, checks will be done to insure correct fields are included
* garminConnect - Special Check for creating FIT files for GarminConnectExample:
``` swift
let activity = ActivityMessage(timeStamp: FitTime(date: Date()),
totalTimerTime: nil,
localTimeStamp: nil,
numberOfSessions: nil,
activity: Activity.multisport,
event: nil,
eventType: nil,
eventGroup: nil)let fieldId = FileIdMessage(deviceSerialNumber: nil,
fileCreationDate: time,
manufacturer: Manufacturer.garmin,
product: nil,
fileNumber: nil,
fileType: FileType.activity,
productName: nil)let encoder = FitFileEncoder(dataValidityStrategy: .none)
let result = encoder.encode(fildIdMessage: fiel, messages: [activity])
switch result {
case .success(let encodedData):
print(encodedData as NSData)
/// you can save off the file data
case .failure(let error):
print(error.localizedDescription)
}///
/// You can still use doCatch
///
do {
let encoder = FitFileEncoder(dataValidityStrategy: .none)let data = try encoder.encode(fildIdMessage: fieldId, messages: [activity]).get()
print(data as NSData)/// you can save off the file data
} catch {
print(error)
}
```## Author
This package is developed and maintained by Kevin A. Hoogheem
## License
FitDataProtocol is available under the [MIT license](http://opensource.org/licenses/MIT)