This is a bare bones example of how to create a Bluetooth 4.0 (BLE) peripheral (aka GATT server) on Linux (e.g. on a Raspberry Pi) using Swift and the PureSwift BluetoothLinux library.
This example sets up a simple BLE peripheral with 2 services, each with a single characteristic.
For a more advanced example with nicer syntax, take a look at SwiftLinuxGATTServerExampleAdvanced
-
Device Information: an example of using a built-in GATT "Device Information" service (0x180A) with the built-in GATT "Manufacturer Name String" characteristic (0x2A29). Normally you'd have more characteristics for this service.
-
Example Service: a custom service with a single read/write characteristic. This shows how to handle write requests and how to update a characteristics value.
Note: I have no affiliation with the PureSwift project and this code may not be the recommended or canonical usage.
- Install Raspbian or Ubuntu on Raspberry Pi
- Install Swift 5 on Raspberry Pi using the prebuilt binary
- Checkout the SwiftLinuxGATTServerExample project
- Navigate to project directory and run:
sudo /home/pi/usr/bin/swift run
I'm using BlueSee BLE Debugger to test the BLE connection and verify that read/write works on the example customer characteristic.
- PureSwift/BluetoothLinux - Pure Swift Linux Bluetooth Stack
- PureSwift/GATT - Bluetooth Generic Attribute Profile (GATT) for Swift