Skip to content

Instantly share code, notes, and snippets.

@don
Last active August 29, 2015 14:24
Show Gist options
  • Save don/d1c727144c3dc6f5dd0e to your computer and use it in GitHub Desktop.
Save don/d1c727144c3dc6f5dd0e to your computer and use it in GitHub Desktop.
iOS Bluetooth Low Energy Central
_central = [[CBCentralManager alloc]
initWithDelegate:self
queue:nil
options:nil];
- (void)centralManagerDidUpdateState:(CBCentralManager *)central {
if ([central state] == CBCentralManagerStatePoweredOn) {
[_central scanForPeripheralsWithServices:@[serviceUUID]
options:nil];
}
}
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);
}
[_central connectPeripheral:_peripheral options:nil];
- (void)centralManager:(CBCentralManager *)central
didConnectPeripheral:(CBPeripheral *)peripheral {
peripheral.delegate = self;
[peripheral discoverServices:@[serviceUUID]];
}
[peripheral discoverServices:nil];
[peripheral discoverServices:@[serviceUUID]];
- (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];
}
}
- (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;
}
}
}
[peripheral readValueForCharacteristic:_switchCharacteristic];
- (void)peripheral:(CBPeripheral *)peripheral
didUpdateValueForCharacteristic:(CBCharacteristic *)characteristic
error:(NSError *)error {
if ([characteristic isEqual: _switchCharacteristic]) {
// get data
}
}
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]);
}
NSData *data = someData;
[_peripheral writeValue:data
forCharacteristic:_switchCharacteristic
type:CBCharacteristicWriteWithoutResponse];
[peripheral setNotifyValue:YES forCharacteristic:characteristic];
- (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];
}
}
}
- (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