Skip to content

Latest commit

 

History

History
206 lines (160 loc) · 6.88 KB

SwiftMeetup2015-01.md

File metadata and controls

206 lines (160 loc) · 6.88 KB

Simple Bike Computer1

doing something useful with BLTE as a geek

pull requests are welcome!

#about me @volkersfreunde github.com/falkorichter mobile software Ingenieur work@sensorberg about.me/falkorichter


#Content

  • What is BTLE
  • Simple Bike Computer
    • Hardware/setup - bike
    • Hardware - phone
  • Swift iOS/MacOSX
  • optional: Android

#BTLE Bluetooth Low Energy®, a.k.a. Bluetooth Smart®

  • simple low energy data transfer
  • send simple bits of data fast
  • don´t sent alot of data
  • easy binding

#BTLE

​​​To help consumers identify compatibility and ensure connectivity with products and applications incorporating Bluetooth®​​ Core Specification version 4.0 (or higher), the Bluetooth SIG has developed the Bluetooth Smart and Bluetooth Smart Ready trademarks.2


#BTLE basics Services

Services are collections of characteristics and relationships to other services that encapsulate the behavior of part of a device.3


#BTLE basics

Characteristics

Characteristics are defined attribute types that contain a single logical value.4


#BTLE basics

Descriptors

"Descriptors are defined attributes that describe a characteristic value."5


#Why a bike computer?

##I ride my bike ##It´s not too expensive ##I want to know how far I go each week/month/year ##health foo #ftw


#Ingredients Speed and Cadence Sensor

inline fillinline fill inline fill


#Ingredients inline 33%inline 33%


#Ingedients Amazon: "Geschwindigkeit und Trittfrequenz"

Any Mac / iPhone > 4S

Remove the label o the device with nail polish remover


#Speed and Cadence

It´s all nicely documented.


#Swift everything is a pretty easy stack (almost) identical on iOS & MacOSX (I had a compilation error on the same code) characteristic.value() vs characteristic.value


#Hello world of BTLE: heartRate org.bluetooth.service.heart_rate 1 Service 1 Value (the heart rate) -> simulate beeing a heart rate sensor -> simulate connecting to a heart rate sensor github.com/falkorichter/swift-simple-bike-computer


#Swift become a peripheral:67

func startBroadcasting(){
    heartRateService.characteristics = [hearRateChracteristic]
    infoService.characteristics = [infoNameCharacteristics]
    
    peripheralManager.addService(infoService)
    peripheralManager.addService(heartRateService)
    var advertisementData = [
        CBAdvertisementDataServiceUUIDsKey:[infoService.UUID, heartRateService.UUID],
        CBAdvertisementDataLocalNameKey : "mac of falko"
    ]
    peripheralManager.startAdvertising(advertisementData)   
}
[...]

#Swift get notified :1 8

let CSC_SERVICE = CBUUID(string: "1816")
let CSC_MEASUREMENT  = CBUUID(string: "2A5B")

func centralManagerDidUpdateState(central: CBCentralManager!){
    switch (central.state){
    case .PoweredOn:
        central.scanForPeripheralsWithServices([CSC_SERVICE], options: nil)
    default:
        println("not powered on")
    }
}

#Swift get notified :2

func centralManager(central: CBCentralManager!, didConnectPeripheral peripheral: CBPeripheral!){
    peripheral.discoverServices([CSC_SERVICE])
}

#Swift get notified :3

func peripheral(peripheral: CBPeripheral!, didDiscoverServices error: NSError!){
    if(error != nil) {
        for service in peripheral.services {
            if service.UUID == CSC_SERVICE {
                peripheral.discoverCharacteristics([CSC_MEASUREMENT], forService: service as CBService)
            }
        }
    }
}

#Swift get notified :4

func peripheral(peripheral: CBPeripheral!, didDiscoverCharacteristicsForService service: CBService!, error: NSError!) {
    if(error != nil){
        if service.UUID == CSC_SERVICE {
            for characteristic in service.characteristics{
                if (characteristic as CBCharacteristic).UUID == CSC_MEASUREMENT {
                    peripheral.setNotifyValue(true, forCharacteristic: characteristic as CBCharacteristic);
                }
            }
        }
    }
}

#Swift get notified :5

func peripheral(peripheral: CBPeripheral!, didUpdateValueForCharacteristic characteristic: CBCharacteristic!, error: NSError!) {
	//do your thing
}

Footnotes

  1. Presentation made with DecksetApp inline 8 %

  2. bluetooth.org/en-us/bluetooth-brand/how-to-use-smart-marks

  3. Android Version of this Presentation on youtube

  4. [developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicsHome.aspx)[https://developer.bluetooth.org/gatt/characteristics/Pages/CharacteristicsHome.aspx]

  5. developer.bluetooth.org/gatt/descriptors/Pages/DescriptorsHomePage.aspx

  6. done: github.com/deadfalkon/swift-simple-bike-computer/blob/master/Shared/HeartBeatPeripheral.swift

  7. needed: github.com/deadfalkon/swift-simple-bike-computer/blob/master/Shared/SpeedAndCadencePeripheral.swift

  8. https://github.com/deadfalkon/swift-simple-bike-computer/blob/master/Shared/CadenceConnector.swift