2828 * Body Sensor Location Char: 0x2A38 (Optional)
2929 */
3030
31- BLEClientService clientHRM (UUID16_SVC_HEART_RATE);
32- BLEClientCharacteristic clientMeasurement (UUID16_CHR_HEART_RATE_MEASUREMENT);
33- BLEClientCharacteristic clientSensorLocation (UUID16_CHR_BODY_SENSOR_LOCATION);
31+ BLEClientService hrms (UUID16_SVC_HEART_RATE);
32+ BLEClientCharacteristic hrmc (UUID16_CHR_HEART_RATE_MEASUREMENT);
33+ BLEClientCharacteristic bslc (UUID16_CHR_BODY_SENSOR_LOCATION);
3434
3535void setup ()
3636{
@@ -46,21 +46,22 @@ void setup()
4646 Bluefruit.setName (" Bluefruit52 Central" );
4747
4848 // Initialize HRM client
49- clientHRM .begin ();
49+ hrms .begin ();
5050
5151 // Initialize client characteristics of HRM.
52- // Note: Client Char will be added to the most recent Service that is initialized .
53- clientSensorLocation .begin ();
52+ // Note: Client Char will be added to the last service that is begin()ed .
53+ bslc .begin ();
5454
55- clientMeasurement.begin ();
56- clientMeasurement.setNotifyCallback (hrm_notify_callback); // set up callback for receiving measurement
55+ // set up callback for receiving measurement
56+ hrmc.setNotifyCallback (hrm_notify_callback);
57+ hrmc.begin ();
5758
5859 // Increase Blink rate to different from PrPh advertising mode
5960 Bluefruit.setConnLedInterval (250 );
6061
6162 // Callbacks for Central
62- Bluefruit.Central .setConnectCallback (connect_callback);
6363 Bluefruit.Central .setDisconnectCallback (disconnect_callback);
64+ Bluefruit.Central .setConnectCallback (connect_callback);
6465
6566 /* Start Central Scanning
6667 * - Enable auto scan if disconnected
@@ -72,7 +73,7 @@ void setup()
7273 Bluefruit.Scanner .setRxCallback (scan_callback);
7374 Bluefruit.Scanner .restartOnDisconnect (true );
7475 Bluefruit.Scanner .setInterval (160 , 80 ); // in unit of 0.625 ms
75- Bluefruit.Scanner .filterUuid (clientHRM .uuid );
76+ Bluefruit.Scanner .filterUuid (hrms .uuid );
7677 Bluefruit.Scanner .useActiveScan (false );
7778 Bluefruit.Scanner .start (0 ); // // 0 = Don't stop scanning after n seconds
7879}
@@ -102,7 +103,7 @@ void connect_callback(uint16_t conn_handle)
102103 Serial.print (" Discovering HRM Service ... " );
103104
104105 // If HRM is not found, disconnect and return
105- if ( !clientHRM .discover (conn_handle) )
106+ if ( !hrms .discover (conn_handle) )
106107 {
107108 Serial.println (" Found NONE" );
108109
@@ -112,18 +113,18 @@ void connect_callback(uint16_t conn_handle)
112113 return ;
113114 }
114115
115- // HRM is found
116+ // Once HRM service is found, we continue to discover its characteristic
116117 Serial.println (" Found it" );
117118 Serial.println (" Discovering Measurement and Body location characteristic ... " );
118119
119- uint8_t num_found_chr = Bluefruit.Discovery .discoverCharacteristic (conn_handle, clientMeasurement, clientSensorLocation );
120+ uint8_t num_found_chr = Bluefruit.Discovery .discoverCharacteristic (conn_handle, hrmc, bslc );
120121
121122 Serial.print (" Found " );
122123 Serial.print (num_found_chr);
123124 Serial.println (" Characteristics" );
124125
125126 // Measurement chr is mandatory, if it is not found (valid), then disconnect
126- if ( !clientMeasurement .isValid () )
127+ if ( !hrmc .isValid () )
127128 {
128129 Serial.println (" Measurement characteristic not found" );
129130 Bluefruit.Central .disconnect (conn_handle);
@@ -133,21 +134,21 @@ void connect_callback(uint16_t conn_handle)
133134
134135 // https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.body_sensor_location.xml
135136 // Body Sensor Location is optional, print its location (in text) if present
136- if ( clientSensorLocation .isValid () )
137+ if ( bslc .isValid () )
137138 {
138139 // Body sensor location value is 8 bit
139140 const char * body_str[] = { " Other" , " Chest" , " Wrist" , " Finger" , " Hand" , " Ear Lobe" , " Foot" };
140141
141142 uint8_t loc_value = 0 ;
142- if ( clientSensorLocation .read (&loc_value) )
143+ if ( bslc .read (&loc_value) )
143144 {
144145 Serial.print (" Body Location Sensor: " );
145146 Serial.println (body_str[loc_value]);
146147 }
147148 }
148149
149150 // Reaching here means we are ready to go, let's enable notification on measurement chr
150- if ( clientMeasurement .enableNotify () )
151+ if ( hrmc .enableNotify () )
151152 {
152153 Serial.println (" Ready to receive HRM Measurement value" );
153154 }else
@@ -173,7 +174,7 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
173174/* *
174175 * Hooked callback that triggered when a measurement value is sent from peripheral
175176 * @param chr Reference to client characteristic that even occurred,
176- * in this example it should be clientMeasurement
177+ * in this example it should be hrmc
177178 * @param data Pointer to received data
178179 * @param len Length of received data
179180 */
@@ -185,7 +186,6 @@ void hrm_notify_callback(BLEClientCharacteristic& chr, uint8_t* data, uint16_t l
185186
186187 Serial.print (" HRM Measurement: " );
187188
188-
189189 if ( data[0 ] & bit (0 ) )
190190 {
191191 uint16_t value;
0 commit comments