MoBetterBluetooth 0.0.4

MoBetterBluetooth 0.0.4

测试已测试
语言语言 SwiftSwift
许可 MIT
发布上次发布2020年7月
SPM支持 SPM

Robert Vaessen 维护。



  • Robert Vaessen

Mo Better Bluetooth

更简单、更直观的 iOS Core Bluetooth 功能使用方法。

MoBetterBluetooth 是一个正在开发中的项目。它目前提供了一种创建中心管理器的更好方式。外围管理器即将推出。信标支持正在开发中。当 MoBetterBluetooth 达到版本 1.0.0 时,它将准备好投入生产。

下面是一些示例代码。有关完整示例,请参见 ButtonLed

import UIKit
import CoreBluetooth
import MoBetterBluetooth

class ViewController: UIViewController, CentralManagerTypesFactory {

    private var manager: CentralManager!

    override func viewDidLoad() {
        super.viewDidLoad()

        // Subscribe to a service that provides access to an LED and a push button.

        let ledCharacteristicId = CentralManager.Identifier(uuid: CBUUID(string: "DCBA1523-1212-EFDE-1523-785FEF13D123"), name: "LED")
        let ledSubscription = CentralManager.CharacteristicSubscription(id: ledCharacteristicId, discoverDescriptors: false)

        let buttonCharacteristicId = CentralManager.Identifier(uuid: CBUUID(string: "DCBA1524-1212-EFDE-1523-785FEF13D123"), name: "Button")
        let buttonSubscription = CentralManager.CharacteristicSubscription(id: buttonCharacteristicId, discoverDescriptors: false)

        let buttonLedServiceId = CentralManager.Identifier(uuid: CBUUID(string: "DCBA3154-1212-EFDE-1523-785FEF13D123"), name: "ButtonLed")
        let buttonLedSubscription = CentralManager.ServiceSubscription(id: buttonLedServiceId, characteristics: [buttonSubscription, ledSubscription])

        let peripheralSubscription = CentralManager.PeripheralSubscription(services: [buttonLedSubscription])

        // Obtain a manager for the subscription
        manager = CentralManager(subscription: peripheralSubscription, factory: self) { event in

            switch event { // Respond to the manager's events

                case .managerReady: // Core Bluetooth is available and ready to use

                    do {
                        try self.manager.startScanning()
                    }
                    catch {
                        print("Cannot start scanning: \(error).")
                    }

                case .peripheralReady(let peripheral): // A peripheral matching the subscription has been found

                    self.manager.stopScanning()

                    let buttonLedService = peripheral[buttonLedServiceId]!
                    let ledCharacteristic = buttonLedService[ledCharacteristicId]!
                    let buttonCharacteristic = buttonLedService[buttonCharacteristicId]!

                    var ledOn = false

                    do { // Enable button press notifications
                        try buttonCharacteristic.notify(enabled: true) { result in

                            switch result { // Respond to a button press by toggling the LED.

                                case .success:
                                    do {
                                        ledOn = !ledOn
                                        try ledCharacteristic.write(Data([ledOn ? 1 : 0])) { result in
                                            switch result {

                                                case .success:
                                                    print("The LED was toggled \(ledOn ? "on" : "off")")

                                                case .failure(let error):
                                                    print("The LED could not be toggled: \(error)")
                                            }
                                        }
                                    }
                                    catch {
                                        print("Cannot write the LED:  \(error)")
                                    }

                                case .failure(let error):
                                    print("Button notifications produced an error: \(error).")
                            }
                        }
                    }
                    catch {
                        print("Cannot enable button notifications: \(error).")
                    }

                default:
                    print("Central Manager Event - \(event).")
            }
        }
    }
}

注意

  • 要将 MoBetterBluetooth 包含到您的项目中

    • Carthage - 在您的项目 Cartfile 中添加以下两行
      • github "verticon/VerticonsToolbox"
      • github "verticon/MoBetterBluetooth"
    • CocoaPods - 在您的项目 Podfile 中添加以下两行
      • pod 'VerticonsToolbox'
      • pod 'MoBetterBluetooth'
  • 要构建 MoBeterBluetooth

    1. 克隆仓库或下载 zip 存档。
    2. 执行提供的 invokeCarthage shell 脚本来构建 VerticonsToolbox。
    3. 将 VerticonsToolbox.framework 从 ../MoBetterBluetooth/Carthage/Build/iOS 复制到 ../MoBetterBluetooth。
    4. 打开 MoBetterBluetooth.xcodeproj 编译。

Carthage compatible