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 Low Energy A feature of Bluetooth 4.0 Low power,

    low latency wireless applications Healthcare, fitness, security, home entertainment, home automation, proximity, toys, ...
  2. 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
  3. 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
  4. CoreBluetooth #import <CoreBluetooth/CoreBluetooth.h> - (void)scanForDevices { CBCentralManager *centralManager = [[CBCentralManager

    alloc] initWithDelegate:self queue:nil]; [centralManager scanForPeripheralsWithServices:@[@"180D"] // heart rate service options:nil]; self.centralManager = centralManager; }
  5. 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]; }
  6. CoreBluetooth #pragma mark - CBPeripheralDelegate - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error

    { for (CBService *service in peripheral.services) { ! [peripheral discoverCharacteristics:nil forService:service]; ! } } }
  7. 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]; } } } }
  8. 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]; } }
  9. 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
  10. CoreBluetooth Session 703 “Core Bluetooth 101” Session 705 “Advanced Core

    Bluetooth” https:/ /developer.apple.com/hardwaredrivers/ BluetoothDesignGuidelines.pdf WWDC 2012