Latest 0.3.0
Homepage https://github.com/CatchZeng/SwiftBluetooth
License MIT
Platforms ios 8.0
Frameworks UIKit, CoreBluetooth
Authors

A simple framework for building BLE apps.

Version
License
Platform

Logo

Example

To run the example project, clone the repo, and run pod install from the Example directory first.

Usage

Central

Init SBCentralManager

let central = SBCentralManager()
//or
let central = SBCentralManager.shared

Start with listener or not

central.start(listener: self)
//or
central.start()

Properties

state
isConnected
isConnecting
connectedPeripherals
foundPeripherals
isBluetoothAvailable
......

Implement SBCenterListener

func central(_ central: SBCentralManager, available: Bool) {
}

func central(_ central: SBCentralManager, didChangeState: CenterState) {
}

func central(_ central: SBCentralManager, didConnect device: Peripheral) {
}

func central(_ central: SBCentralManager, onDisconnecting device: Peripheral) {
}

func central(_ central: SBCentralManager, onConnecting device: Peripheral) {
}

func central(_ central: SBCentralManager, didDisconnect device: Peripheral, error: Error?) {
}

func central(_ central: SBCentralManager, peripheral: BLEPeripheral, didRSSIChanged RSSI: NSNumber) {
}

func central(_ central: SBCentralManager, device: BLEPeripheral, characteristic: CBCharacteristic, didReceive data: Result<Data>) {
}

Log config

SBCentralManager.enableLog = true

Scan

// !!! Scan will fail beofre bluetooth available.

central.scan(serviceUUIDs: nil,
                             allowDuplicates: false,
                             filter: { (peripheral) -> (Bool) in
                             // You can filter peripheral by name or other properties.
                             // return peripheral.name.contains("your custom bluetooth name.")
                             return true
}, sorter: { (peripheral1, peripheral2) -> (Bool) in
    // You can sort peripheral by rssi or other properties.
    return peripheral1.rssi < peripheral2.rssi
}) { (result) in
    switch result {
    case .success(let discoverPeripheral, let foundPeripheralList):
    break
    case .cancelled:
    print("Scan cancelled.")
    case .failure(let error):
    print("Scan error: (error?.localizedDescription ?? "")")
    }
}

central.stopScan()

Connection

central.connect(peripheral: discoverPeripheral,
                timeoutInterval: 10,
                callback: { (result) in
                switch result {
                case .success(let peripheral):
                    print("Connect periphera:(peripheral.name).")
                case .cancelled:
                    print("Connect cancelled.")
                case .failure(let error):
                    print("Connect error: (error?.localizedDescription ?? "")")
                }
})

central.cancelConnecting()

central.disConnect(peripheral: peripheral) { (result) in
                switch result {
                case .success(let peripheral):
                    print("Disconnect periphera:(peripheral.name).")
                case .cancelled:
                    print("Disconnect cancelled.")
                case .failure(let error):
                    print("Disconnect error: (error?.localizedDescription ?? "")")
                }
}

Peripheral

Properties

name
uuid
rssi
......

Service

peripheral.discoverServices(serviceUUIDs: nil, callback: { (result) in
    switch result {
    case .success(let services):
        print("DiscoverServices:(services).")
    case .cancelled:
        print("DiscoverServices cancelled.")
    case .failure(let error):
        print("DiscoverServices error: (error?.localizedDescription ?? "")")
    }
})

Characteristic

peripheral.discoverCharacteristics(characteristicUUIDs: nil, service: service) { (result) in
    switch result {
    case .success(let characteristics):
        print("DiscoverCharacteristics:(String(describing: characteristics?.count)).")
    case .cancelled:
        print("Characteristics cancelled.")
    case .failure(let error):
        print("Characteristics error: (error?.localizedDescription ?? "")")
    }
}

WriteValue

peripheral.writeValue(data: data,
                      characteristic: characteristic,
                      type: .withResponse)

ReadRSSI

peripheral.readRSSI()

Installation

SwiftBluetooth is available through CocoaPods. To install
it, simply add the following line to your Podfile:

pod 'SwiftBluetooth'

Author

CatchZeng, [email protected]

License

SwiftBluetooth is available under the MIT license. See the LICENSE file for more info.

Latest podspec

{
    "name": "SwiftBluetooth",
    "version": "0.3.0",
    "summary": "A simple framework for building BLE apps.",
    "homepage": "https://github.com/CatchZeng/SwiftBluetooth",
    "license": {
        "type": "MIT",
        "file": "LICENSE"
    },
    "authors": {
        "CatchZeng": "[email protected]"
    },
    "source": {
        "git": "https://github.com/CatchZeng/SwiftBluetooth.git",
        "tag": "0.3.0"
    },
    "platforms": {
        "ios": "8.0"
    },
    "source_files": "SwiftBluetooth/Classes/**/*",
    "frameworks": [
        "UIKit",
        "CoreBluetooth"
    ]
}

Pin It on Pinterest

Share This