Skip to content

Instantly share code, notes, and snippets.

@z3rg
Forked from nolili/gist:a583ea045dafafebb17f
Created March 28, 2016 03:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z3rg/468677242a43a4c7e8dd to your computer and use it in GitHub Desktop.
Save z3rg/468677242a43a4c7e8dd to your computer and use it in GitHub Desktop.
Heart Rate Monitor Example(Swift)
import UIKit
import CoreBluetooth
class ViewController: UIViewController, CBCentralManagerDelegate, CBPeripheralDelegate {
let centralManager:CBCentralManager!
var connectingPeripheral:CBPeripheral!
required init(coder aDecoder: NSCoder) {
super.init(coder: aDecoder)
centralManager = CBCentralManager(delegate: self, queue: dispatch_get_main_queue())
}
func centralManagerDidUpdateState(central: CBCentralManager!){
switch central.state{
case .PoweredOn:
println("poweredOn")
let serviceUUIDs:[AnyObject] = [CBUUID(string: "180D")]
let lastPeripherals = centralManager.retrieveConnectedPeripheralsWithServices(serviceUUIDs)
if lastPeripherals.count > 0{
let device = lastPeripherals.last as CBPeripheral;
connectingPeripheral = device;
centralManager.connectPeripheral(connectingPeripheral, options: nil)
}
else {
centralManager.scanForPeripheralsWithServices(serviceUUIDs, options: nil)
}
default:
println(central.state)
}
}
func centralManager(central: CBCentralManager!, didDiscoverPeripheral peripheral: CBPeripheral!, advertisementData: [NSObject : AnyObject]!, RSSI: NSNumber!) {
connectingPeripheral = peripheral
connectingPeripheral.delegate = self
centralManager.connectPeripheral(connectingPeripheral, options: nil)
}
func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!) {
peripheral.discoverServices(nil)
}
func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!) {
if let actualError = error{
}
else {
for service in peripheral.services as [CBService]!{
peripheral.discoverCharacteristics(nil, forService: service)
}
}
}
func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {
if let actualError = error{
}
else {
if service.UUID == CBUUID(string: "180D"){
for characteristic in service.characteristics as [CBCharacteristic]{
switch characteristic.UUID.UUIDString{
case "2A37":
// Set notification on heart rate measurement
println("Found a Heart Rate Measurement Characteristic")
peripheral.setNotifyValue(true, forCharacteristic: characteristic)
case "2A38":
// Read body sensor location
println("Found a Body Sensor Location Characteristic")
peripheral.readValueForCharacteristic(characteristic)
case "2A39":
// Write heart rate control point
println("Found a Heart Rate Control Point Characteristic")
var rawArray:[UInt8] = [0x01];
let data = NSData(bytes: &rawArray, length: rawArray.count)
peripheral.writeValue(data, forCharacteristic: characteristic, type: CBCharacteristicWriteType.WithoutResponse)
default:
println()
}
}
}
}
}
func update(#heartRateData:NSData){
var buffer = [UInt8](count: heartRateData.length, repeatedValue: 0x00)
heartRateData.getBytes(&buffer, length: buffer.count)
var bpm:UInt16?
if (buffer.count >= 2){
if (buffer[0] & 0x01 == 0){
bpm = UInt16(buffer[1]);
}else {
bpm = UInt16(buffer[1]) << 8
bpm = bpm! | UInt16(buffer[2])
}
}
if let actualBpm = bpm{
println(actualBpm)
}else {
println(bpm)
}
}
func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
if let actualError = error{
}else {
switch characteristic.UUID.UUIDString{
case "2A37":
update(heartRateData:characteristic.value)
default:
println()
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment