28
28
* Body Sensor Location Char: 0x2A38 (Optional)
29
29
*/
30
30
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);
34
34
35
35
void setup ()
36
36
{
@@ -46,21 +46,22 @@ void setup()
46
46
Bluefruit.setName (" Bluefruit52 Central" );
47
47
48
48
// Initialize HRM client
49
- clientHRM .begin ();
49
+ hrms .begin ();
50
50
51
51
// 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 ();
54
54
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 ();
57
58
58
59
// Increase Blink rate to different from PrPh advertising mode
59
60
Bluefruit.setConnLedInterval (250 );
60
61
61
62
// Callbacks for Central
62
- Bluefruit.Central .setConnectCallback (connect_callback);
63
63
Bluefruit.Central .setDisconnectCallback (disconnect_callback);
64
+ Bluefruit.Central .setConnectCallback (connect_callback);
64
65
65
66
/* Start Central Scanning
66
67
* - Enable auto scan if disconnected
@@ -72,7 +73,7 @@ void setup()
72
73
Bluefruit.Scanner .setRxCallback (scan_callback);
73
74
Bluefruit.Scanner .restartOnDisconnect (true );
74
75
Bluefruit.Scanner .setInterval (160 , 80 ); // in unit of 0.625 ms
75
- Bluefruit.Scanner .filterUuid (clientHRM .uuid );
76
+ Bluefruit.Scanner .filterUuid (hrms .uuid );
76
77
Bluefruit.Scanner .useActiveScan (false );
77
78
Bluefruit.Scanner .start (0 ); // // 0 = Don't stop scanning after n seconds
78
79
}
@@ -102,7 +103,7 @@ void connect_callback(uint16_t conn_handle)
102
103
Serial.print (" Discovering HRM Service ... " );
103
104
104
105
// If HRM is not found, disconnect and return
105
- if ( !clientHRM .discover (conn_handle) )
106
+ if ( !hrms .discover (conn_handle) )
106
107
{
107
108
Serial.println (" Found NONE" );
108
109
@@ -112,18 +113,18 @@ void connect_callback(uint16_t conn_handle)
112
113
return ;
113
114
}
114
115
115
- // HRM is found
116
+ // Once HRM service is found, we continue to discover its characteristic
116
117
Serial.println (" Found it" );
117
118
Serial.println (" Discovering Measurement and Body location characteristic ... " );
118
119
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 );
120
121
121
122
Serial.print (" Found " );
122
123
Serial.print (num_found_chr);
123
124
Serial.println (" Characteristics" );
124
125
125
126
// Measurement chr is mandatory, if it is not found (valid), then disconnect
126
- if ( !clientMeasurement .isValid () )
127
+ if ( !hrmc .isValid () )
127
128
{
128
129
Serial.println (" Measurement characteristic not found" );
129
130
Bluefruit.Central .disconnect (conn_handle);
@@ -133,21 +134,21 @@ void connect_callback(uint16_t conn_handle)
133
134
134
135
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.body_sensor_location.xml
135
136
// Body Sensor Location is optional, print its location (in text) if present
136
- if ( clientSensorLocation .isValid () )
137
+ if ( bslc .isValid () )
137
138
{
138
139
// Body sensor location value is 8 bit
139
140
const char * body_str[] = { " Other" , " Chest" , " Wrist" , " Finger" , " Hand" , " Ear Lobe" , " Foot" };
140
141
141
142
uint8_t loc_value = 0 ;
142
- if ( clientSensorLocation .read (&loc_value) )
143
+ if ( bslc .read (&loc_value) )
143
144
{
144
145
Serial.print (" Body Location Sensor: " );
145
146
Serial.println (body_str[loc_value]);
146
147
}
147
148
}
148
149
149
150
// Reaching here means we are ready to go, let's enable notification on measurement chr
150
- if ( clientMeasurement .enableNotify () )
151
+ if ( hrmc .enableNotify () )
151
152
{
152
153
Serial.println (" Ready to receive HRM Measurement value" );
153
154
}else
@@ -173,7 +174,7 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
173
174
/* *
174
175
* Hooked callback that triggered when a measurement value is sent from peripheral
175
176
* @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
177
178
* @param data Pointer to received data
178
179
* @param len Length of received data
179
180
*/
@@ -185,7 +186,6 @@ void hrm_notify_callback(BLEClientCharacteristic& chr, uint8_t* data, uint16_t l
185
186
186
187
Serial.print (" HRM Measurement: " );
187
188
188
-
189
189
if ( data[0 ] & bit (0 ) )
190
190
{
191
191
uint16_t value;
0 commit comments