Skip to content

Instantly share code, notes, and snippets.

@don
Last active November 5, 2015 14:21
Show Gist options
  • Save don/f8eeab2e524228fb2ec2 to your computer and use it in GitHub Desktop.
Save don/f8eeab2e524228fb2ec2 to your computer and use it in GitHub Desktop.
iOS Bluetooth Low Energy Peripheral
_peripheralManager = [[CBPeripheralManager alloc]
initWithDelegate:self
queue:nil];
- (void)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral {
if (peripheral.state == CBPeripheralManagerStatePoweredOn) {
// create service
// start advertising
}
}
_switchCharacteristic = [[CBMutableCharacteristic alloc]
initWithType:[CBUUID UUIDWithString:SWITCH_CHARACTERISTIC_UUID]
properties:CBCharacteristicPropertyWrite | CBCharacteristicPropertyRead
value:nil
permissions:CBAttributePermissionsWriteable | CBAttributePermissionsReadable];
descriptor = [[CBMutableDescriptor alloc]
initWithType: [CBUUID UUIDWithString:CBUUIDCharacteristicUserDescriptionString]
value:@"Switch"];
_switchCharacteristic.descriptors = @[descriptor];
_dimmerCharacteristic = [[CBMutableCharacteristic alloc]
initWithType:[CBUUID UUIDWithString:DIMMER_CHARACTERISTIC_UUID]
properties: CBCharacteristicPropertyRead | CBCharacteristicPropertyWrite
value:nil
permissions:CBAttributePermissionsWriteable | CBAttributePermissionsReadable];
descriptor = [[CBMutableDescriptor alloc]
initWithType: [CBUUID UUIDWithString:CBUUIDCharacteristicUserDescriptionString]
value:@"Dimmer"];
_dimmerCharacteristic.descriptors = @[descriptor];
CBMutableService *service = [[CBMutableService alloc]
initWithType:[CBUUID UUIDWithString:LED_SERVICE_UUID]
primary:YES];
service.characteristics = @[_switchCharacteristic, _dimmerCharacteristic];
[_peripheralManager addService:service];
[_peripheralManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey : @[service.UUID]}];
-(void)peripheralManager:(CBPeripheralManager *)peripheral
didReceiveWriteRequests:(NSArray *)requests
{
NSLog(@"Received %lu write request(s)", (unsigned long)[requests count]);
CBATTRequest *request = [requests firstObject];
if ([request.characteristic.UUID isEqual:_switchCharacteristic.UUID]) {
// ...
} else if ([request.characteristic.UUID isEqual:_dimmerCharacteristic.UUID]) {
// ...
}
[_peripheralManager respondToRequest:request withResult:CBATTErrorSuccess];
}
-(void)peripheralManager:(CBPeripheralManager *)peripheral
didReceiveReadRequest:(CBATTRequest *)request
{
NSLog(@"Received read request for %@", [request characteristic]);
if ([request.characteristic.UUID isEqual:_switchCharacteristic.UUID]) {
request.value = _switchCharacteristic.value
}
[_peripheralManager respondToRequest:request withResult:CBATTErrorSuccess];
}
@nissivm
Copy link

nissivm commented Nov 5, 2015

Hi don, very good gist =] I have a question: Do you know how can I create multiple instances of the same characteristic (for example, Automation IO characteristics)? I've put this question in StackOverflow (http://stackoverflow.com/questions/33546272/how-to-create-multiple-instances-of-the-same-cbcharacteristic)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment