Connect to dive computers from Swift.
A Swift package that downloads dives from a dive computer over Bluetooth Low Energy.
- Language
- Swift 5.7+
- Platforms
- iOS 14+, macOS 11+, Linux
- Install
- Swift Package Manager
- Transport
- Bluetooth Low Energy
Install
Add the package
Swift Package Manager compiles libdivecomputer from source with the rest of the package. The build needs no prebuilt binary, no framework, and no script phase.
The license gives you access to the package.
dependencies: [
.package(
url: "https://github.com/<org>/<package>.git",
from: "0.13.1"
)
] Download
Scan, connect, and download
The scan suspends and returns what is in range. The download is an async sequence of dives, and a progress handler reports the transfer while it runs.
The app reads DiveComputer.bluetoothAuthorization before CoreBluetooth activates. The system prompt then belongs to a scan that the diver starts.
import DiveComputerKit
let diveComputer = try DiveComputer(logLevel: .info)
let devices = await diveComputer.scanDevices(timeout: 10)
for device in devices {
print(device.vendor, device.product, device.name)
} let connected = try await diveComputer.connect(to: devices[0])
// The fingerprint stops the download at the newest dive you hold.
let dives = connected.downloadDives(fingerprint: last) { progress in
ui.show(progress.percent)
}
for try await dive in dives {
store.insert(dive) // typed: depth, samples, gases
if let raw = dive.rawData { archive.write(raw) }
} What the package does
- The scan reports each dive computer in range with its vendor and its product.
- A fingerprint stops the download at the newest dive that you hold.
- Each dive carries typed samples and the original device bytes.
- The parse path reads a stored blob without a device.
Parse
Parse without a device
The parse path is a separate target. It depends on the C library and on Foundation only, so it builds for Linux. A server parses an archived blob with the same code as the app.
A parse that the bytes do not support fails with an error. The library never returns a partial dive.
import DiveParsingKit
// The parse path needs no device.
let dive = try diveComputer.parseDive(
rawData: try Data(contentsOf: fileURL),
parserType: "shearwater_petrel"
)
for sample in dive.samples {
guard sample.decoType == .decoStop else { continue }
print(sample.decoDepth, sample.decoTts)
} # Parse a raw dive to JSON. Only JSON goes to stdout.
libdc-parse dive.bin --parser shearwater_perdix --json
# Write a self-describing .dive envelope.
libdc-parse dive.bin --parser shearwater_perdix --envelope
# List the device descriptors, for a model picker.
libdc-parse --list-parsers License the Swift package
A license covers the Swift package, one license per app. Tell us what you build. You get a quote, the terms, and access to the package.