pureswift/gatt
Bluetooth Generic Attribute Profile (GATT) for Swift
Installation
GATT is available as a Swift Package Manager package. To use it, add the following dependency in your Package.swift:
.package(url: "https://github.com/PureSwift/GATT.git", branch: "master"),and to your target, add GATT to your dependencies. You can then import GATT to get access to GATT functionality.
Platforms
| Platform | Roles | Backend | Library | | ---- | -------- | --- | ----------- | | macOS, iOS, watchOS, tvOS, visionOS | Central, Peripheral | CoreBluetooth | DarwinGATT | | Linux | Central, Peripheral | BlueZ | BluetoothLinux, GATT | Android | Central | Java Native Interface | AndroidBluetooth | WebAssembly | Central | Bluetooth Web API | BluetoothWeb | Pi Pico W | Peripheral | BlueKitchen BTStack | BTStack | ESP32 | Peripheral | Apache NimBLE | NimBLE | nRF52840 | Peripheral | Zephyr SDK | Zephyr
Usage
Peripheral
import Bluetooth
#if canImport(Darwin)
import DarwinGATT
#elseif os(Linux)
import BluetoothLinux
#endif
#if os(Linux)
typealias LinuxPeripheral = GATTPeripheral<BluetoothLinux.HostController, BluetoothLinux.L2CAPSocket>
guard let hostController = await HostController.default else {
fatalError("No Bluetooth hardware connected")
}
let serverOptions = GATTPeripheralOptions(
maximumTransmissionUnit: .max,
maximumPreparedWrites: 1000
)
let peripheral = LinuxPeripheral(
hostController: hostController,
options: serverOptions,
socket: BluetoothLinux.L2CAPSocket.self
)
#elseif canImport(Darwin)
let peripheral = DarwinPeripheral()
#else
#error("Unsupported platform")
#endif
// start advertising
try await peripheral.start()
Central
import Bluetooth
#if canImport(Darwin)
import DarwinGATT
#elseif os(Linux)
import BluetoothLinux
#endif
#if os(Linux)
typealias LinuxCentral = GATTCentral<BluetoothLinux.HostController, BluetoothLinux.L2CAPSocket>
let hostController = await HostController.default
let central = LinuxCentral(
hostController: hostController,
socket: BluetoothLinux.L2CAPSocket.self
)
#elseif canImport(Darwin)
let central = DarwinCentral()
#else
#error("Unsupported platform")
#endif
// start scanning
let stream = try await central.scan(filterDuplicates: true)
for try await scanData in stream {
print(scanData)
stream.stop()
}
Documentation
Read the documentation here. Documentation can be generated with DocC.
License
GATT is released under the MIT license. See LICENSE for details.
[swift-badge]: https://img.shields.io/badge/swift-6.0-F05138.svg "Swift 6.0" [swift-url]: https://swift.org [platform-badge]: https://img.shields.io/badge/platform-macOS%20%7C%20iOS%20%7C%20watchOS%20%7C%20tvOS%20%7C%20Linux%20%7C%20Android-lightgrey.svg [platform-url]: https://swift.org [mit-badge]: https://img.shields.io/badge/License-MIT-blue.svg?style=flat [mit-url]: https://tldrlegal.com/license/mit-license [build-status-badge]: https://github.com/PureSwift/GATT/workflows/Swift/badge.svg [build-status-url]: https://github.com/PureSwift/GATT/actions [release-badge]: https://img.shields.io/github/release/PureSwift/GATT.svg [release-url]: https://github.com/PureSwift/GATT/releases [docs-url]: http://pureswift.github.io/GATT/documentation/GATT/
Package Metadata
Repository: pureswift/gatt
Default branch: master
README: README.md