Last active
November 5, 2015 14:21
-
-
Save don/f8eeab2e524228fb2ec2 to your computer and use it in GitHub Desktop.
iOS Bluetooth Low Energy Peripheral
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
_peripheralManager = [[CBPeripheralManager alloc] | |
initWithDelegate:self | |
queue: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)peripheralManagerDidUpdateState:(CBPeripheralManager *)peripheral { | |
if (peripheral.state == CBPeripheralManagerStatePoweredOn) { | |
// create service | |
// start advertising | |
} | |
} |
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
_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]; |
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
_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]; |
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
CBMutableService *service = [[CBMutableService alloc] | |
initWithType:[CBUUID UUIDWithString:LED_SERVICE_UUID] | |
primary:YES]; | |
service.characteristics = @[_switchCharacteristic, _dimmerCharacteristic]; | |
[_peripheralManager addService:service]; | |
[_peripheralManager startAdvertising:@{CBAdvertisementDataServiceUUIDsKey : @[service.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
-(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]; | |
} |
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)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]; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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)