Bluetooth LE with CoreBluetooth

3e9b0b47a98cdc664bcf58fa3ff5a758?s=47 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.

3e9b0b47a98cdc664bcf58fa3ff5a758?s=128

Chris Miles

February 07, 2013
Tweet

Transcript

  1. Bluetooth LE with CoreBluetooth Chris Miles http:/ /chrismiles.info/ @chrismiles Melbourne

    CocoaHeads – Feb 2013
  2. CoffeeAddict (demo)

  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, ...
  4. Bluetooth Low Energy

  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
  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
  7. 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; }
  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]; }
  9. CoreBluetooth #pragma mark - CBPeripheralDelegate - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error

    { for (CBService *service in peripheral.services) { ! [peripheral discoverCharacteristics:nil forService:service]; ! } } }
  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]; } } } }
  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]; } }
  12. CBCentralManager CBPeripheral CBService CBCharacteristic NSData Central Scanning Peripheral Advertising Peripheral

    Service Characteristic Value Heartrate BPM Location 85 “Chest”
  13. CoreBluetooth iOS Device can be a peripheral CBPeripheralManager, CBCentral CBMutableService,

    CBMutableCharacteristic New in iOS 6
  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
  15. CoreBluetooth Session 703 “Core Bluetooth 101” Session 705 “Advanced Core

    Bluetooth” https:/ /developer.apple.com/hardwaredrivers/ BluetoothDesignGuidelines.pdf WWDC 2012
  16. TI SensorTag Accelerometer Gyroscope Magnetometer IR Temperature + ambient Barometric

    pressure Humidity
  17. TI SensorTag

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

    app in App Store
  19. Chris Miles Freelance iOS Specialist Software Engineer http:/ /chrismiles.info/ @chrismiles