$30 off During Our Annual Pro Sale. View Details »

Conquering CoreBluetooth, a Survival Guide

Conquering CoreBluetooth, a Survival Guide

Dan will dive into how Square Cash utilizes CoreBluetooth to allow their customers to send money to people who are nearby without exchanging a phone number, email address, or username, and without the recipient having to take their phone out of their pocket.

By Dan Federman https://www.linkedin.com/in/dsfed

Powered by http://xebia.com

do{iOS} conference

November 09, 2015
Tweet

More Decks by do{iOS} conference

Other Decks in Programming

Transcript

  1. Conquering CoreBluetooth
    A Survival Guide
    Dan Federman – @TheFederman

    View Slide

  2. Why BLE?

    View Slide

  3. Run Sharing App
    • Username
    • Current Heart Rate
    • Average Heart Rate
    • Distance
    • Elevation Gain
    • Average Speed

    View Slide

  4. Peripheral

    View Slide

  5. Services
    Heart Rate Run
    Peripheral
    Username

    View Slide

  6. Heart Rate
    Current Heart Rate Average Heart Rate
    Peripheral

    View Slide

  7. Current Heart Rate Average Heart Rate
    Characteristics
    Peripheral

    View Slide

  8. Run
    Distance
    Elevation Gain Average Speed
    Peripheral

    View Slide

  9. Current Heart Rate Average Heart Rate
    Characteristics
    Distance
    Elevation Gain Average Speed
    Peripheral
    Username

    View Slide

  10. Peripheral – Setup
    [[CBPeripheralManager alloc] initWithDelegate:queue:options:]
    peripheralManagerDidUpdateState:
    addService:
    startAdvertising:

    View Slide

  11. Peripheral – Setup
    [[CBPeripheralManager alloc] initWithDelegate:queue:options:]
    CBPeripheralManagerOptionRestoreIdentifierKey
    CBPeripheralManagerOptionShowPowerAlertKey

    View Slide

  12. Peripheral – Setup
    [[CBPeripheralManager alloc] initWithDelegate:queue:options:]
    peripheralManagerDidUpdateState:
    addService:
    startAdvertising:

    View Slide

  13. Peripheral – Setup
    addService:
    [[CBMutableService alloc] initWithType:primary:]
    service.characteristics = @[ characteristic1, … ]

    View Slide

  14. Peripheral – Setup
    addService:
    [[CBMutableService alloc] initWithType:primary:]
    service.characteristics = @[ characteristic1, … ]
    [[CBCharacteristic alloc] initWithType:
    properties:
    value:
    permissions:

    View Slide

  15. Peripheral – Setup
    [[CBPeripheralManager alloc] initWithDelegate:queue:options:]
    peripheralManagerDidUpdateState:
    addService:
    startAdvertising:

    View Slide

  16. Peripheral – Setup
    startAdvertising:
    CBAdvertisementDataServiceUUIDsKey

    View Slide

  17. Peripheral – Sending Data
    peripheralManager:didReceiveReadRequest:
    respondToRequest:withResult:
    request.characteristic
    request.offset
    request.value
    CBATTErrorSuccess
    CBATTErrorAttributeNotFound
    CBATTErrorInvalidOffset

    View Slide

  18. Peripheral – Sending Data
    request.value = abcdefghijklmnopqrstuvwxyz
    request.offset == 0
    request.value = klmnopqrstuvwxyz
    request.offset == 10
    request.value = uvwxyz
    request.offset == 20

    View Slide

  19. Peripheral – Restoration
    peripheralManager:willRestoreState:

    View Slide

  20. Central

    View Slide

  21. Central – Setup
    [[CBCentralManager alloc] initWithDelegate:queue:options:]
    centralManagerDidUpdateState:
    scanForPeripheralsWithServices:options:

    View Slide

  22. BLE Advertising Packet
    Preamble
    (1 byte)
    Packet Type
    (4 bytes)
    Checksum
    (3 bytes)
    Header Payload
    16 bytes 6 - 37 bytes

    View Slide

  23. Central – Reading Data
    centralManager:didDiscoverPeripheral:advertisementData:RSSI:
    centralManager:didConnectPeripheral:error:
    peripheral:didDiscoverServices:
    peripheral:didDiscoverCharacteristicsForService:error:
    peripheral:didUpdateValueForCharacteristic:error:

    View Slide

  24. Central – Reading Data
    centralManager:didDiscoverPeripheral:advertisementData:RSSI:
    Store peripheral
    connectPeripheral:options:

    View Slide

  25. Central – Reading Data
    centralManager:didDiscoverPeripheral:advertisementData:RSSI:
    centralManager:didConnectPeripheral:error:
    peripheral:didDiscoverServices:
    peripheral:didDiscoverCharacteristicsForService:error:
    peripheral:didUpdateValueForCharacteristic:error:

    View Slide

  26. Central – Reading Data
    centralManager:didConnectPeripheral:error:
    setDelegate:
    discoverServices:

    View Slide

  27. Central – Reading Data
    centralManager:didDiscoverPeripheral:advertisementData:RSSI:
    centralManager:didConnectPeripheral:error:
    peripheral:didDiscoverServices:
    peripheral:didDiscoverCharacteristicsForService:error:
    peripheral:didUpdateValueForCharacteristic:error:

    View Slide

  28. Central – Reading Data
    peripheral:didDiscoverServices:
    discoverCharacteristics:forService:

    View Slide

  29. Central – Reading Data
    centralManager:didDiscoverPeripheral:advertisementData:RSSI:
    centralManager:didConnectPeripheral:error:
    peripheral:didDiscoverServices:
    peripheral:didDiscoverCharacteristicsForService:error:
    peripheral:didUpdateValueForCharacteristic:error:

    View Slide

  30. Central – Reading Data
    peripheral:didDiscoverCharacteristicsForService:error:
    readValueForCharacteristic:

    View Slide

  31. Central – Reading Data
    centralManager:didDiscoverPeripheral:advertisementData:RSSI:
    centralManager:didConnectPeripheral:error:
    peripheral:didDiscoverServices:
    peripheral:didDiscoverCharacteristicsForService:error:
    peripheral:didUpdateValueForCharacteristic:error:

    View Slide

  32. Central – Reading Data
    peripheral:didUpdateValueForCharacteristic:error:
    characteristic.value
    disconnectPeripheral:

    View Slide

  33. Central – Updating Data

    View Slide

  34. iOS Support
    7 8 9

    View Slide

  35. iOS Support
    7 8 9

    View Slide

  36. iOS Support
    7 8 9

    View Slide

  37. Open Source
    Spring 2016

    View Slide

  38. Open Source
    github.com/square/aardvark
    github.com/square/valet

    View Slide