Bluetooth Generic Attribute Profile (GATT) for Swift
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()
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()
}
Read the documentation here. Documentation can be generated with DocC.
GATT is released under the MIT license. See LICENSE for details.
link |
Stars: 60 |
Last commit: 5 weeks ago |
DarwinCentral.name(for:)
Swiftpack is being maintained by Petr Pavlik | @ptrpavlik | @swiftpackco | API | Analytics