Last active
August 29, 2015 14:24
-
-
Save don/d1c727144c3dc6f5dd0e to your computer and use it in GitHub Desktop.
iOS Bluetooth Low Energy Central
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
_central = [[CBCentralManager alloc] | |
initWithDelegate:self | |
queue:nil | |
options:nil]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)centralManagerDidUpdateState:(CBCentralManager *)central { | |
if ([central state] == CBCentralManagerStatePoweredOn) { | |
[_central scanForPeripheralsWithServices:@[serviceUUID] | |
options:nil]; | |
} | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSArray* services = @[serviceUUID]; | |
[_central scanForPeripheralsWithServices:services options:nil]; | |
- (void)centralManager:(CBCentralManager *)central | |
didDiscoverPeripheral:(CBPeripheral *)peripheral | |
advertisementData:(NSDictionary *)advertisementData | |
RSSI:(NSNumber *)RSSI { | |
NSLog(@"Discovered %@", peripheral.name); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[_central connectPeripheral:_peripheral options:nil]; | |
- (void)centralManager:(CBCentralManager *)central | |
didConnectPeripheral:(CBPeripheral *)peripheral { | |
peripheral.delegate = self; | |
[peripheral discoverServices:@[serviceUUID]]; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[peripheral discoverServices:nil]; | |
[peripheral discoverServices:@[serviceUUID]]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)peripheral:(CBPeripheral *)peripheral | |
didDiscoverServices:(NSError *)error { | |
for (CBService *service in peripheral.services) { | |
NSLog(@"Discovered service %@", service); | |
NSArray* characteristics = @[switchUUID, dimmerUUID]; | |
[peripheral discoverCharacteristics:characteristics | |
forService:service]; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)peripheral:(CBPeripheral *)peripheral | |
didDiscoverCharacteristicsForService:(CBService *)service | |
error:(NSError *)error { | |
for (CBCharacteristic *characteristic in service.characteristics) { | |
NSLog(@"Discovered characteristic %@", characteristic); | |
if ([characteristic.UUID isEqual: switchUUID]) { | |
_switchCharacteristic = characteristic; | |
} else if ([characteristic.UUID isEqual: dimmerUUID]) { | |
_dimmerCharacteristic = characteristic; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[peripheral readValueForCharacteristic:_switchCharacteristic]; | |
- (void)peripheral:(CBPeripheral *)peripheral | |
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic | |
error:(NSError *)error { | |
if ([characteristic isEqual: _switchCharacteristic]) { | |
// get data | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSData *data = someData; | |
[_peripheral writeValue:data | |
forCharacteristic:_switchCharacteristic | |
type:CBCharacteristicWriteWithResponse]; | |
-(void) peripheral:(CBPeripheral *)peripheral | |
didWriteValueForCharacteristic:(CBCharacteristic *)characteristic | |
error:(NSError *)error { | |
NSLog(@"Wrote value for %@", [characteristic UUID]); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
NSData *data = someData; | |
[_peripheral writeValue:data | |
forCharacteristic:_switchCharacteristic | |
type:CBCharacteristicWriteWithoutResponse]; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[peripheral setNotifyValue:YES forCharacteristic:characteristic]; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)peripheral:(CBPeripheral *)peripheral | |
didDiscoverCharacteristicsForService:(CBService *)service | |
error:(NSError *)error { | |
for (CBCharacteristic *characteristic in service.characteristics) { | |
NSLog(@"Discovered characteristic %@", characteristic); | |
if ([characteristic.UUID isEqual: temperatureUUID]) { | |
// subscribe for notifications | |
[peripheral setNotifyValue:YES forCharacteristic:characteristic]; | |
// read the current value, so we can update the UI | |
[peripheral readValueForCharacteristic: characteristic]; | |
} | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (void)peripheral:(CBPeripheral *)peripheral | |
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic | |
error:(NSError *)error { | |
if ([characteristic.UUID isEqual: temperatureUUID]) { | |
NSData *data = characteristic.value; | |
// process | |
[_temperatureLabel setText:message]; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment