Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Bluetooth LE with CoreBluetooth

Chris Miles
February 07, 2013

Bluetooth LE with CoreBluetooth

My talk introducing Bluetooth Low Energy and Apple's CoreBluetooth API for iOS and OS X.

Presented at Melbourne Cocoaheads, Feb 2013.

Chris Miles

February 07, 2013
Tweet

More Decks by Chris Miles

Other Decks in Programming

Transcript

  1. Bluetooth LE
    with CoreBluetooth
    Chris Miles
    http:/
    /chrismiles.info/
    @chrismiles
    Melbourne CocoaHeads – Feb 2013

    View Slide

  2. CoffeeAddict
    (demo)

    View Slide

  3. Bluetooth Low Energy
    A feature of Bluetooth 4.0
    Low power, low latency wireless applications
    Healthcare, fitness, security, home
    entertainment, home automation, proximity,
    toys, ...

    View Slide

  4. Bluetooth Low Energy

    View Slide

  5. Bluetooth Low Energy
    No system-level pairing
    Device “driver” embedded in app
    No need for MFI Licensing
    “Indies” can build both the hardware and
    software

    View Slide

  6. Bluetooth LE
    iPhone 4S onwards
    iPad 3rd gen onwards
    iPad Mini
    iPod Touch 5th Gen
    Recent MacBook Pros and Airs and Mac Mini
    iOS Simulator

    View Slide

  7. CoreBluetooth
    #import
    - (void)scanForDevices
    {
    CBCentralManager *centralManager = [[CBCentralManager alloc] initWithDelegate:self
    queue:nil];
    [centralManager scanForPeripheralsWithServices:@[@"180D"] // heart rate service
    options:nil];
    self.centralManager = centralManager;
    }

    View Slide

  8. CoreBluetooth
    #pragma mark - CBCentralManagerDelegate
    - (void)centralManager:(CBCentralManager *)central didDiscoverPeripheral:(CBPeripheral
    *)peripheral advertisementData:(NSDictionary *)advertisementData RSSI:(NSNumber *)RSSI
    {
    NSString *localName = [advertisementData objectForKey:CBAdvertisementDataLocalNameKey];
    if (localName && [localName rangeOfString:@"HRM"].location != NSNotFound) {
    ! // Found heart rate monitor
    !
    ! [self stopScanningForDevices];
    !
    ! self.wahooHRMPeripheral = peripheral; // must retain it
    ! peripheral.delegate = self;
    !
    ! [central connectPeripheral:peripheral options:nil];
    }
    }
    - (void)centralManager:(CBCentralManager *)central didConnectPeripheral:(CBPeripheral
    *)peripheral
    {
    NSArray *services = @[ [CBUUID UUIDWithString:@"180D"] ];
    [peripheral discoverServices:services];
    }

    View Slide

  9. CoreBluetooth
    #pragma mark - CBPeripheralDelegate
    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
    {
    for (CBService *service in peripheral.services) {
    ! [peripheral discoverCharacteristics:nil
    forService:service];
    ! }
    }
    }

    View Slide

  10. CoreBluetooth
    - (void)peripheral:(CBPeripheral *)peripheral didDiscoverCharacteristicsForService:(CBService
    *)service error:(NSError *)error
    {
    if ([service.UUID isEqual:[CBUUID UUIDWithString:@"180D"]]) {
    for (CBCharacteristic *characteristic in service.characteristics) {
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A39"]]) {
    // Enable heart rate sensor
    UInt8 value = 0x01;
    NSData *data = [NSData dataWithBytes:&value
    length:sizeof(value)];
    [peripheral writeValue:data
    forCharacteristic:characteristic
    type:CBCharacteristicWriteWithResponse];
    }
    else if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A37"]]) {
    // Request heart rate notifications
    [peripheral setNotifyValue:YES
    forCharacteristic:characteristic];
    }
    }
    }
    }

    View Slide

  11. CoreBluetooth
    - (void)peripheral:(CBPeripheral *)peripheral didUpdateValueForCharacteristic:
    (CBCharacteristic *)characteristic error:(NSError *)error
    {
    if ([characteristic.UUID isEqual:[CBUUID UUIDWithString:@"2A37"]]) {
    // BPM
    ! NSData *data = [characteristic value];
    !
    ! const uint8_t *reportData = [data bytes];
    ! uint16_t bpm = 0;
    !
    ! if ((reportData[0] & 0x01) == 0) {
    ! /* uint8 bpm */
    ! bpm = reportData[1];
    ! }
    ! else {
    ! /* uint16 bpm */
    ! bpm = CFSwapInt16LittleToHost(*(uint16_t *)(&reportData[1]));
    ! }
    !
    ! [self didReceiveHeartrate:bpm];
    }
    }

    View Slide

  12. CBCentralManager
    CBPeripheral
    CBService
    CBCharacteristic
    NSData
    Central Scanning
    Peripheral Advertising
    Peripheral
    Service
    Characteristic
    Value
    Heartrate
    BPM
    Location
    85 “Chest”

    View Slide

  13. CoreBluetooth
    iOS Device can be a peripheral
    CBPeripheralManager, CBCentral
    CBMutableService, CBMutableCharacteristic
    New in iOS 6

    View Slide

  14. CoreBluetooth
    Event-based peripherals
    App can be suspended, is woken up on
    event
    Session-based peripherals
    Full access to peripheral in background
    Requires info.plist entry
    iOS Background Modes

    View Slide

  15. CoreBluetooth
    Session 703 “Core Bluetooth 101”
    Session 705 “Advanced Core Bluetooth”
    https:/
    /developer.apple.com/hardwaredrivers/
    BluetoothDesignGuidelines.pdf
    WWDC 2012

    View Slide

  16. TI SensorTag
    Accelerometer
    Gyroscope
    Magnetometer
    IR Temperature + ambient
    Barometric pressure
    Humidity

    View Slide

  17. TI SensorTag

    View Slide

  18. TI SensorTag
    Texas Instruments
    US$25 !!
    http:/
    /www.ti.com/sensortag
    SensorTag iOS app in App Store

    View Slide

  19. Chris Miles
    Freelance iOS Specialist
    Software Engineer
    http:/
    /chrismiles.info/
    @chrismiles

    View Slide