Share
Embed
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
Transcript
19 slides
-
Bluetooth Low Energy A feature of Bluetooth 4.0 Low power,
low latency wireless applications Healthcare, fitness, security, home entertainment, home automation, proximity, toys, ... -
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 -
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 -
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; } -
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]; } -
CoreBluetooth #pragma mark - CBPeripheralDelegate - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)error
{ for (CBService *service in peripheral.services) { ! [peripheral discoverCharacteristics:nil forService:service]; ! } } } -
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]; } } } } -
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]; } } -
CBCentralManager CBPeripheral CBService CBCharacteristic NSData Central Scanning Peripheral Advertising Peripheral
Service Characteristic Value Heartrate BPM Location 85 “Chest” -
CoreBluetooth iOS Device can be a peripheral CBPeripheralManager, CBCentral CBMutableService,
CBMutableCharacteristic New in iOS 6 -
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 -
CoreBluetooth Session 703 “Core Bluetooth 101” Session 705 “Advanced Core
Bluetooth” https:/ /developer.apple.com/hardwaredrivers/ BluetoothDesignGuidelines.pdf WWDC 2012