Skip to content

Repository files navigation

GATT

SwiftPlatformReleaseLicense

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.

Package Traits

GATT requires Swift 6.2 and uses package traits to gate the pure Swift GATT stack:

TraitDefaultDescription
BluetoothGATTDisabledImports the BluetoothGATT module, enabling the pure Swift GATT server (GATTPeripheral, PeripheralManager) and making the central use the BluetoothGATT types (GATTCharacteristicProperties, ATTAttributePermissions, ATTMaximumTransmissionUnit) instead of the lightweight shims.

By default the BluetoothGATT trait is disabled, so GATT builds against the lightweight shim types. This suits central-only clients built on a platform Bluetooth stack (for example CoreBluetooth on iOS or the Android Bluetooth API), where the platform provides the GATT server and the shims keep the dependency footprint small.

Enable the trait when you need a GATT server (peripheral). This applies to both the pure Swift GATTPeripheral and the CoreBluetooth-backed DarwinPeripheral; the shim build provides the central role only. On platforms without threading (Embedded Swift targets like Pi Pico W, ESP32 or nRF52840 running BTStack, NimBLE or Zephyr), GATTPeripheral is driven by calling its non-blocking run() method from the platform's run loop instead of background threads.

.package(
url:"https://github.com/PureSwift/GATT.git",
branch:"master",
traits:["BluetoothGATT"]),

Platforms

PlatformRolesBackendLibrary
macOS, iOS, watchOS, tvOS, visionOSCentral, PeripheralCoreBluetoothDarwinGATT
LinuxCentral, PeripheralBlueZBluetoothLinux, GATT
AndroidCentralJava Native InterfaceAndroidBluetooth
WebAssemblyCentralBluetooth Web APIBluetoothWeb
Pi Pico WPeripheralBlueKitchen BTStackBTStack
ESP32PeripheralApache NimBLENimBLE
nRF52840PeripheralZephyr SDKZephyr

Usage

Peripheral

import Bluetooth
#if canImport(Darwin)import DarwinGATT
#elseif os(Linux)import BluetoothLinux
#endif#if os(Linux)typealiasLinuxPeripheral=GATTPeripheral<BluetoothLinux.HostController,BluetoothLinux.L2CAPSocket>guardlet hostController =awaitHostController.default else{fatalError("No Bluetooth hardware connected")}letserverOptions=GATTPeripheralOptions(
maximumTransmissionUnit:.max,
maximumPreparedWrites:1000)letperipheral=LinuxPeripheral(
hostController: hostController,
options: serverOptions,
socket:BluetoothLinux.L2CAPSocket.self
)#elseif canImport(Darwin)letperipheral=DarwinPeripheral()#else#error("Unsupported platform")#endif
// start advertising
tryawait peripheral.start()

Central

import Bluetooth
#if canImport(Darwin)import DarwinGATT
#elseif os(Linux)import BluetoothLinux
#endif#if os(Linux)typealiasLinuxCentral=GATTCentral<BluetoothLinux.HostController,BluetoothLinux.L2CAPSocket>lethostController=awaitHostController.default
letcentral=LinuxCentral(
hostController: hostController,
socket:BluetoothLinux.L2CAPSocket.self
)#elseif canImport(Darwin)letcentral=DarwinCentral()#else#error("Unsupported platform")#endif
// start scanning
letstream=tryawait central.scan(filterDuplicates:true)fortryawaitscanDatain 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.

About

Bluetooth Generic Attribute Profile (GATT) for Swift

Topics

Resources

Stars

71 stars

Watchers

3 watching

Forks

Releases

Used by

Contributors

Languages