SpeziBluetooth

1.0.2

Connect and communicate with Bluetooth devices.
StanfordSpezi/SpeziBluetooth

What's New

1.0.2

2024-04-09T01:52:19Z

What's Changed

  • Move ByteCodable to SpeziFileFormats package by @Supereg in #19
  • Update to release version of SpeziFileFormats by @Supereg in #22

Full Changelog: 1.0.1...1.0.2

SpeziBluetooth

Build and Test codecov DOI

Connect and communicate with Bluetooth devices using modern programming paradigms.

Overview

The Spezi Bluetooth module provides a convenient way to handle state management with a Bluetooth device, retrieve data from different services and characteristics, and write data to a combination of services and characteristics.

This package uses Apples CoreBluetooth framework under the hood.

Note

You will need a basic understanding of the Bluetooth Terminology and the underlying software model to understand the structure and API of the Spezi Bluetooth module. You can find a good overview in the Wikipedia Bluetooth Low Energy (LE) Software Model section or the Developer’s Guide to Bluetooth Technology.

Setup

Add Spezi Bluetooth as a Dependency

You need to add the Spezi Bluetooth Swift package to your app in Xcode or Swift package.

Important

If your application is not yet configured to use Spezi, follow the Spezi setup article to set up the core Spezi infrastructure.

Register the Module

The Bluetooth module needs to be registered in a Spezi-based application using the configuration in a SpeziAppDelegate:

class ExampleAppDelegate: SpeziAppDelegate {
    override var configuration: Configuration {
        Configuration {
            Bluetooth {
                // discover devices ...
            }
        }
    }
}

Note

You can learn more about a Module in the Spezi documentation.

Example

Create your Bluetooth device

The Bluetooth module allows to declarative define your Bluetooth device using a BluetoothDevice implementation and property wrappers like Service and Characteristic.

The below code examples demonstrate how you can implement your own Bluetooth device.

First of all we define our Bluetooth service by implementing a BluetoothService. We use the Characteristic property wrapper to declare its characteristics. Note that the value types needs to be optional and conform to ByteEncodable, ByteDecodable or ByteCodable respectively.

class DeviceInformationService: BluetoothService {
    @Characteristic(id: "2A29")
    var manufacturer: String?
    @Characteristic(id: "2A26")
    var firmwareRevision: String?
}

We can use this Bluetooth service now in the MyDevice implementation as follows.

Tip: We use the DeviceState and DeviceAction property wrappers to get access to the device state and its actions. Those two property wrappers can also be used within a BluetoothService type.

class MyDevice: BluetoothDevice {
    @DeviceState(\.id)
    var id: UUID
    @DeviceState(\.name)
    var name: String?
    @DeviceState(\.state)
    var state: PeripheralState

    @Service var deviceInformation = DeviceInformationService()

    @DeviceAction(\.connect)
    var connect
    @DeviceAction(\.disconnect)
    var disconnect

    init() {} // required initializer
}

Configure the Bluetooth Module

We use the above BluetoothDevice implementation to configure the Bluetooth module within the SpeziAppDelegate.

import Spezi

class ExampleDelegate: SpeziAppDelegate {
    override var configuration: Configuration {
        Configuration {
            Bluetooth {
                // Define which devices type to discover by what criteria .
                // In this case we search for some custom FFF0 service that is advertised.
                Discover(MyDevice.self, by: .advertisedService("FFF0"))
            }
        }
    }
}

Using the Bluetooth Module

Once you have the Bluetooth module configured within your Spezi app, you can access the module within your Environment.

You can use the scanNearbyDevices(enabled:with:autoConnect:) and autoConnect(enabled:with:) modifiers to scan for nearby devices and/or auto connect to the first available device. Otherwise, you can also manually start and stop scanning for nearby devices using scanNearbyDevices(autoConnect:) and stopScanning().

To retrieve the list of nearby devices you may use nearbyDevices(for:).

Tip: To easily access the first connected device, you can just query the SwiftUI Environment for your BluetoothDevice type. Make sure to declare the property as optional using the respective Environment(_:) initializer.

The below code example demonstrates all these steps of retrieving the Bluetooth module from the environment, listing all nearby devices, auto connecting to the first one and displaying some basic information of the currently connected device.

import SpeziBluetooth
import SwiftUI

struct MyView: View {
    @Environment(Bluetooth.self)
    var bluetooth
    @Environment(MyDevice.self)
    var myDevice: MyDevice?

    var body: some View {
        List {
            if let myDevice {
                Section {
                    Text("Device")
                    Spacer()
                    Text("\(myDevice.state.description)")
                }
            }

            Section {
                ForEach(bluetooth.nearbyDevices(for: MyDevice.self), id: \.id) { device in
                    Text("\(device.name ?? "unknown")")
                }
            } header: {
                HStack {
                    Text("Devices")
                        .padding(.trailing, 10)
                    if bluetooth.isScanning {
                        ProgressView()
                    }
                }
            }
        }
            .scanNearbyDevices(with: bluetooth, autoConnect: true)
    }
}

For more information, please refer to the API documentation.

Contributing

Contributions to this project are welcome. Please make sure to read the contribution guidelines and the contributor covenant code of conduct first.

License

This project is licensed under the MIT License. See Licenses for more information.

Spezi Footer Spezi Footer

Description

  • Swift Tools 5.9.0
View More Packages from this Author

Dependencies

Last updated: Mon May 06 2024 01:31:04 GMT-0900 (Hawaii-Aleutian Daylight Time)