项目作者: PureSwift

项目描述 :
Bluetooth Generic Attribute Profile (GATT) for Swift (Supports Linux)
高级语言: Swift
项目地址: git://github.com/PureSwift/GATT.git
创建时间: 2016-04-01T23:20:46Z
项目社区:https://github.com/PureSwift/GATT

开源协议:MIT License

下载


GATT

Swift
Platform
Release
License

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:

  1. .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

  1. import Bluetooth
  2. #if canImport(Darwin)
  3. import DarwinGATT
  4. #elseif os(Linux)
  5. import BluetoothLinux
  6. #endif
  7. #if os(Linux)
  8. typealias LinuxPeripheral = GATTPeripheral<BluetoothLinux.HostController, BluetoothLinux.L2CAPSocket>
  9. guard let hostController = await HostController.default else {
  10. fatalError("No Bluetooth hardware connected")
  11. }
  12. let serverOptions = GATTPeripheralOptions(
  13. maximumTransmissionUnit: .max,
  14. maximumPreparedWrites: 1000
  15. )
  16. let peripheral = LinuxPeripheral(
  17. hostController: hostController,
  18. options: serverOptions,
  19. socket: BluetoothLinux.L2CAPSocket.self
  20. )
  21. #elseif canImport(Darwin)
  22. let peripheral = DarwinPeripheral()
  23. #else
  24. #error("Unsupported platform")
  25. #endif
  26. // start advertising
  27. try await peripheral.start()

Central

  1. import Bluetooth
  2. #if canImport(Darwin)
  3. import DarwinGATT
  4. #elseif os(Linux)
  5. import BluetoothLinux
  6. #endif
  7. #if os(Linux)
  8. typealias LinuxCentral = GATTCentral<BluetoothLinux.HostController, BluetoothLinux.L2CAPSocket>
  9. let hostController = await HostController.default
  10. let central = LinuxCentral(
  11. hostController: hostController,
  12. socket: BluetoothLinux.L2CAPSocket.self
  13. )
  14. #elseif canImport(Darwin)
  15. let central = DarwinCentral()
  16. #else
  17. #error("Unsupported platform")
  18. #endif
  19. // start scanning
  20. let stream = try await central.scan(filterDuplicates: true)
  21. for try await scanData in stream {
  22. print(scanData)
  23. stream.stop()
  24. }

Documentation

Read the documentation here.
Documentation can be generated with DocC.

License

GATT is released under the MIT license. See LICENSE for details.