Skip to content

HarveyHu/BLEHelper

Repository files navigation

BLEHelper

BLEHelper is an elegant way to deal with your Bluetooth Low Energy device. It supports your iDevice to manipulate multiple BLE devices simultaneously.

Travis CICarthage compatible

Installation

###CocoaPods

Specify it in your Podfile:

source 'https://github.com/CocoaPods/Specs.git'
platform :ios, '8.0'
use_frameworks!

pod 'BLEHelper', '~> 1.0'

Then, run the following command:

$ pod install

###Carthage

Just add the following to your project Cartfile:

github "HarveyHu/BLEHelper" ~> 1.0.0

Then, run the following command:

$ carthage update --platform iOS

Usage

Use by including the following import:

import BLEHelper

And init it as a property of your class:

let bleHelper = BLECentralHelper()

Scan

To scan devices nearby, and the completion is on the end:

bleHelper.scan(1.0, serviceUUID: nil) { (devices) -> (Void) in
            //TODO: show your devices
        }

Connect

To connect with a device by object:

bleHelper.connect(yourPeripheral) { (peripheral, error) -> (Void) in
            //TODO: do something when connected
        }

To connect with a device by deviceUUID string (peripheral.identifier):

self.bleHelper.retrieve(deviceUUIDs: [deviceUUIDString], completion: {(peripheral, error) -> (Void) in
            if error != nil {
                prettyLog("error: \(error?.description)")
                completion?(success: false)
                return
            }
            prettyLog("connect with \(peripheral)")
        })

Operation

To read:

bleHelper.readValue("yourDeviceUUID", serviceUUID: "yourServiceUUID", characteristicUUID: "youCharacteristicUUID") { (success) -> (Void) in
            prettyLog("is read success: \(success)")
    }

To enable notification:

bleHelper.enableNotification(true, deviceUUID: "yourDeviceUUID", serviceUUID: "yourServiceUUID", characteristicUUID: "youCharacteristicUUID") { (success) -> (Void) in
        prettyLog("set notify success: \(success)")
    }

To write:

let command = "yourCommand"
if let data = command.dataUsingEncoding(NSUTF8StringEncoding) {
        bleHelper.writeValue(data, deviceUUID: "yourDeviceUUID", serviceUUID: "yourServiceUUID", characteristicUUID: "youCharacteristicUUID", withResponse: true) { (success) -> (Void) in
            prettyLog("is write success: \(success)")
        }
    }

Delegate

There are only two functions of its delegate. In the beginning, you must declare your class obeying the protocal: "BLECentralHelperDelegate."

Being called when disconnected from peripheral:

func bleDidDisconnectFromPeripheral(peripheral: CBPeripheral) {
	//TODO: do something...
}

Being called when received data from peripheral:

func bleCentralDidReceiveData(data: NSData?, peripheral: CBPeripheral, characteristic: CBCharacteristic) {
	//TODO: do something...
}

Example

Open BLEHelper.xcworkspace with your xcode, and select the Scheme named "BLEHelperExample."

Run it on your iPhone or iPad!

License

BLEHelper is released under a MIT License. See LICENSE file for details.

About

An elegant way to deal with your BLE devices.

Resources

License

Stars

Watchers

Forks

Packages

No packages published