reactivecorebluetooth

ReactiveCoreBluetooth

Introduction

Welcome to the documentation page for ReactiveCoreBluetooth, a powerful library for working with CoreBluetooth in a reactive way.

About

ReactiveCoreBluetooth is designed to simplify and streamline the process of interacting with Bluetooth Low Energy (BLE) devices using CoreBluetooth framework in iOS development. It provides a reactive and functional programming approach, allowing developers to handle asynchronous events and data streams with ease.

Features

  • Reactive programming model for handling CoreBluetooth events and data streams
  • Simple and intuitive API for discovering, connecting to, and communicating with BLE devices
  • Support for both central (client) and peripheral (server) modes of operation
  • Built-in support for common BLE use cases, such as device scanning, service discovery, characteristic interaction, etc.
  • Ability to handle errors and handle retries in a reactive manner
  • Compatible with iOS 9.0+ and Swift 5

Installation

To install ReactiveCoreBluetooth, you can either manually integrate the source files into your project or use a dependency manager like CocoaPods.

Manual Integration

1. Download the latest version of ReactiveCoreBluetooth from the GitHub repository.

2. Drag and drop the required source files (typically found in the ‘Source’ folder) into your Xcode project.

3. Make sure to select ‘Copy items if needed’ and include the files in your project’s main target.

4. Import ReactiveCoreBluetooth in your code using import ReactiveCoreBluetooth.

CocoaPods

1. Add the following line to your Podfile:

pod 'ReactiveCoreBluetooth'

2. Run pod install from the command line in your project directory.

3. Import ReactiveCoreBluetooth in your code using import ReactiveCoreBluetooth.

Usage

Below is a guide on how to use ReactiveCoreBluetooth for common tasks:

Scanning and Discovering Devices

To scan for nearby BLE devices and discover their services and characteristics, use the BLEManager class:

// Example code for scanning BLE devices

import ReactiveCoreBluetooth

let bleManager = BLEManager()

// Set up handlers for discovered devices
bleManager.discoveredDevices
    .sink { discoveredDevice in
        print("Discovered device: \(discoveredDevice)")
    }
    .store(in: &cancellables)

// Start scanning for devices
bleManager.startScanning()

Connecting to Devices

You can connect to a discovered device using the connectDevice function:

// Example code for connecting to a device

bleManager.connectDevice(discoveredDevice)
    .sink { connectedDevice in
        print("Connected to device: \(connectedDevice)")
    }
    .store(in: &cancellables)

Interacting with Characteristics

Once connected, you can interact with the characteristics of a device’s services:

// Example code for interacting with a characteristic

guard let connectedDevice = bleManager.connectedDevice else {
    return
}

let selectedService = connectedDevice.services[0]
let selectedCharacteristic = selectedService.characteristics[0]

// Read value from characteristic
bleManager.readValue(from: selectedCharacteristic)
    .sink { value in
        print("Read value from characteristic: \(value)")
    }
    .store(in: &cancellables)

// Write value to characteristic
let dataToWrite = "Hello, BLE!".data(using: .utf8)!
bleManager.writeValue(dataToWrite, to: selectedCharacteristic)
    .sink { _ in
        print("Value written to characteristic")
    }
    .store(in: &cancellables)

Error Handling

ReactiveCoreBluetooth provides error handling and retry functionality:

Error Handling

Use the handleError operator to handle errors:

bleManager.readValue(from: selectedCharacteristic)
    .handleError { error in
        print("Error reading value: \(error)")
    }
    .sink { value in
        print("Read value from characteristic: \(value)")
    }
    .store(in: &cancellables)

Retry Mechanism

Use the retryOnError operator to automatically retry a failed operation:

bleManager.readValue(from: selectedCharacteristic)
    .retryOnError(maxAttempts: 3, delay: .milliseconds(500))
    .sink { value in
        print("Read value from characteristic: \(value)")
    }
    .store(in: &cancellables)

Further Resources

For more information and detailed usage examples, refer to the official GitHub repository and the documentation within the source code of ReactiveCoreBluetooth.