Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

CoffeeAddict (demo)

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

Bluetooth Low Energy

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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]; }

Slide 9

Slide 9 text

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

Slide 10

Slide 10 text

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]; } } } }

Slide 11

Slide 11 text

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]; } }

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

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

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

TI SensorTag

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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