@@ -11,8 +11,9 @@ @implementation Bluetooth
1111@synthesize cbcManager;
1212
1313// Might have to change these values
14- #define ARDUINO_SHIELD_NAME @" ZBModlue"
15- #define SHIELD_RW_CHARACTERISTIC @" FFC1"
14+ #define ARDUINO_SHIELD_NAME @" ZBModlue" // Name of the BLE shield
15+ #define SHIELD_WRITE_CHARACTERISTIC @" FFC1" // Name of the write characteristic of the shield
16+ #define SHIELD_READ_CHARACTERISTIC @" FFC2" // Name of the read characteristic of the shield
1617
1718// Init the Bluetooth Service
1819-(void ) start {
@@ -61,11 +62,23 @@ - (void)peripheral:(CBPeripheral *)peripheral didDiscoverServices:(NSError *)err
6162// In the case of the ZBModule BLE Shield, the characteristic where values have to be sent is called FFC1
6263- (void )peripheral : (CBPeripheral *)peripheral didDiscoverCharacteristicsForService : (CBService *)service error : (NSError *)error {
6364 for (CBCharacteristic *characteristic in service.characteristics ) {
64- if ([characteristic.UUID.UUIDString isEqualToString: SHIELD_RW_CHARACTERISTIC]){
65- [peripheral setNotifyValue: YES forCharacteristic: characteristic];
66- NSLog (@" Found %@ characteristic" , SHIELD_RW_CHARACTERISTIC);
65+ if ([characteristic.UUID.UUIDString isEqualToString: SHIELD_WRITE_CHARACTERISTIC]){
66+ NSLog (@" Found %@ characteristic (write)" , SHIELD_WRITE_CHARACTERISTIC);
6767 self.characteristic = characteristic;
6868 }
69+ else if ([characteristic.UUID.UUIDString isEqualToString: SHIELD_READ_CHARACTERISTIC]){
70+ NSLog (@" Found %@ characteristic (read)" , SHIELD_READ_CHARACTERISTIC);
71+ // Set notifications for that characteristic
72+ [peripheral setNotifyValue: YES forCharacteristic: characteristic];
73+ }
74+ }
75+ }
76+
77+ // Step #5: Read notifications from the characteristics (when a value is received)
78+ - (void )peripheral : (CBPeripheral *)peripheral didUpdateValueForCharacteristic : (CBCharacteristic *)characteristic error : (NSError *)error {
79+ if (characteristic.value != nil ) {
80+ NSString *str = [[NSString alloc ] initWithData: characteristic.value encoding: NSUTF8StringEncoding];
81+ NSLog (@" Received value: %@ " , str);
6982 }
7083}
7184
0 commit comments