update central custom hrm
This commit is contained in:
1 parent
697bbcab18
commit
8883aa64a1
1 file changed
+19
-19
+19
-19
@@ -28,9 +28,9 @@
|
||||
* Body Sensor Location Char: 0x2A38 (Optional)
|
||||
*/
|
||||
|
||||
BLEClientService clientHRM(UUID16_SVC_HEART_RATE);
|
||||
BLEClientCharacteristic clientMeasurement (UUID16_CHR_HEART_RATE_MEASUREMENT);
|
||||
BLEClientCharacteristic clientSensorLocation(UUID16_CHR_BODY_SENSOR_LOCATION);
|
||||
BLEClientService hrms(UUID16_SVC_HEART_RATE);
|
||||
BLEClientCharacteristic hrmc(UUID16_CHR_HEART_RATE_MEASUREMENT);
|
||||
BLEClientCharacteristic bslc(UUID16_CHR_BODY_SENSOR_LOCATION);
|
||||
|
||||
void setup()
|
||||
{
|
||||
@@ -46,21 +46,22 @@ void setup()
|
||||
Bluefruit.setName("Bluefruit52 Central");
|
||||
|
||||
// Initialize HRM client
|
||||
clientHRM.begin();
|
||||
hrms.begin();
|
||||
|
||||
// Initialize client characteristics of HRM.
|
||||
// Note: Client Char will be added to the most recent Service that is initialized.
|
||||
clientSensorLocation.begin();
|
||||
// Note: Client Char will be added to the last service that is begin()ed.
|
||||
bslc.begin();
|
||||
|
||||
clientMeasurement.begin();
|
||||
clientMeasurement.setNotifyCallback(hrm_notify_callback); // set up callback for receiving measurement
|
||||
// set up callback for receiving measurement
|
||||
hrmc.setNotifyCallback(hrm_notify_callback);
|
||||
hrmc.begin();
|
||||
|
||||
// Increase Blink rate to different from PrPh advertising mode
|
||||
Bluefruit.setConnLedInterval(250);
|
||||
|
||||
// Callbacks for Central
|
||||
Bluefruit.Central.setConnectCallback(connect_callback);
|
||||
Bluefruit.Central.setDisconnectCallback(disconnect_callback);
|
||||
Bluefruit.Central.setConnectCallback(connect_callback);
|
||||
|
||||
/* Start Central Scanning
|
||||
* - Enable auto scan if disconnected
|
||||
@@ -72,7 +73,7 @@ void setup()
|
||||
Bluefruit.Scanner.setRxCallback(scan_callback);
|
||||
Bluefruit.Scanner.restartOnDisconnect(true);
|
||||
Bluefruit.Scanner.setInterval(160, 80); // in unit of 0.625 ms
|
||||
Bluefruit.Scanner.filterUuid(clientHRM.uuid);
|
||||
Bluefruit.Scanner.filterUuid(hrms.uuid);
|
||||
Bluefruit.Scanner.useActiveScan(false);
|
||||
Bluefruit.Scanner.start(0); // // 0 = Don't stop scanning after n seconds
|
||||
}
|
||||
@@ -102,7 +103,7 @@ void connect_callback(uint16_t conn_handle)
|
||||
Serial.print("Discovering HRM Service ... ");
|
||||
|
||||
// If HRM is not found, disconnect and return
|
||||
if ( !clientHRM.discover(conn_handle) )
|
||||
if ( !hrms.discover(conn_handle) )
|
||||
{
|
||||
Serial.println("Found NONE");
|
||||
|
||||
@@ -112,18 +113,18 @@ void connect_callback(uint16_t conn_handle)
|
||||
return;
|
||||
}
|
||||
|
||||
// HRM is found
|
||||
// Once HRM service is found, we continue to discover its characteristic
|
||||
Serial.println("Found it");
|
||||
Serial.println("Discovering Measurement and Body location characteristic ... ");
|
||||
|
||||
uint8_t num_found_chr = Bluefruit.Discovery.discoverCharacteristic(conn_handle, clientMeasurement, clientSensorLocation);
|
||||
uint8_t num_found_chr = Bluefruit.Discovery.discoverCharacteristic(conn_handle, hrmc, bslc);
|
||||
|
||||
Serial.print("Found ");
|
||||
Serial.print(num_found_chr);
|
||||
Serial.println("Characteristics");
|
||||
|
||||
// Measurement chr is mandatory, if it is not found (valid), then disconnect
|
||||
if ( !clientMeasurement.isValid() )
|
||||
if ( !hrmc.isValid() )
|
||||
{
|
||||
Serial.println("Measurement characteristic not found");
|
||||
Bluefruit.Central.disconnect(conn_handle);
|
||||
@@ -133,13 +134,13 @@ void connect_callback(uint16_t conn_handle)
|
||||
|
||||
// https://www.bluetooth.com/specifications/gatt/viewer?attributeXmlFile=org.bluetooth.characteristic.body_sensor_location.xml
|
||||
// Body Sensor Location is optional, print its location (in text) if present
|
||||
if ( clientSensorLocation.isValid() )
|
||||
if ( bslc.isValid() )
|
||||
{
|
||||
// Body sensor location value is 8 bit
|
||||
const char* body_str[] = { "Other", "Chest", "Wrist", "Finger", "Hand", "Ear Lobe", "Foot" };
|
||||
|
||||
uint8_t loc_value = 0;
|
||||
if ( clientSensorLocation.read(&loc_value) )
|
||||
if ( bslc.read(&loc_value) )
|
||||
{
|
||||
Serial.print("Body Location Sensor: ");
|
||||
Serial.println(body_str[loc_value]);
|
||||
@@ -147,7 +148,7 @@ void connect_callback(uint16_t conn_handle)
|
||||
}
|
||||
|
||||
// Reaching here means we are ready to go, let's enable notification on measurement chr
|
||||
if ( clientMeasurement.enableNotify() )
|
||||
if ( hrmc.enableNotify() )
|
||||
{
|
||||
Serial.println("Ready to receive HRM Measurement value");
|
||||
}else
|
||||
@@ -173,7 +174,7 @@ void disconnect_callback(uint16_t conn_handle, uint8_t reason)
|
||||
/**
|
||||
* Hooked callback that triggered when a measurement value is sent from peripheral
|
||||
* @param chr Reference to client characteristic that even occurred,
|
||||
* in this example it should be clientMeasurement
|
||||
* in this example it should be hrmc
|
||||
* @param data Pointer to received data
|
||||
* @param len Length of received data
|
||||
*/
|
||||
@@ -185,7 +186,6 @@ void hrm_notify_callback(BLEClientCharacteristic& chr, uint8_t* data, uint16_t l
|
||||
|
||||
Serial.print("HRM Measurement: ");
|
||||
|
||||
|
||||
if ( data[0] & bit(0) )
|
||||
{
|
||||
uint16_t value;
|
||||
|
||||
Reference in new issue
Block a user