BLE Heart Rate Sample Program for HRM1017 which is using Nordic nRF51822 confirmed the connection of nRFToolbox on Android.
Dependencies: BLE_API mbed nRF51822 color_pixels
Fork of BLE_HTM_HRM1017 by
main.cpp
- Committer:
- YoshinoTaro
- Date:
- 2016-08-22
- Revision:
- 10:8a67578c3ef0
- Parent:
- 9:554af3c63d0c
- Child:
- 11:d32f4f43161d
File content as of revision 10:8a67578c3ef0:
#include "mbed.h" #include "BLE.h" #include <math.h> #define NEED_DEBUG 1 #if NEED_DEBUG #define DEBUG(...) { printf(__VA_ARGS__); } #else #define DEBUG(...) /* nothing */ #endif const static char DEVICE_NAME[] = "mbed HRM1017"; static volatile bool triggerSensorPolling = false; BLEDevice ble; /* Heart Rate Service */ static uint8_t hrmCounter = 100; static uint8_t bpm[2] = {0x00, hrmCounter}; GattCharacteristic hrmChar(GattCharacteristic::UUID_HEART_RATE_MEASUREMENT_CHAR ,bpm, sizeof(bpm) ,sizeof(bpm) ,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); static uint8_t location = 0x05; /* Ear Lobe */ GattCharacteristic hrmLocation(GattCharacteristic::UUID_BODY_SENSOR_LOCATION_CHAR ,(uint8_t *)&location ,sizeof(location) ,sizeof(location) ,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); GattCharacteristic *hrmChars[] = {&hrmChar, &hrmLocation,}; GattService hrmService(GattService::UUID_HEART_RATE_SERVICE ,hrmChars ,sizeof(hrmChars)/sizeof(GattCharacteristic *)); /* Battery Level Service */ static uint8_t batt = 100; GattCharacteristic battLevel(GattCharacteristic::UUID_BATTERY_LEVEL_CHAR ,(uint8_t *)&batt ,sizeof(batt) ,sizeof(batt) ,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY); GattCharacteristic *battChars[] = {&battLevel,}; GattService battService(GattService::UUID_BATTERY_SERVICE ,battChars ,sizeof(battChars)/sizeof(GattCharacteristic *)); /* Device Information service */ static uint8_t deviceName[] = {'H', 'R', 'M', '1', '0', '1', '7'}; GattCharacteristic deviceManufacturer(GattCharacteristic::UUID_MANUFACTURER_NAME_STRING_CHAR ,(uint8_t *)deviceName ,sizeof(deviceName) ,sizeof(deviceName) ,GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_READ); GattCharacteristic *devInfoChars[] = {&deviceManufacturer,}; GattService deviceInformationService(GattService::UUID_DEVICE_INFORMATION_SERVICE ,devInfoChars ,sizeof(devInfoChars)/sizeof(GattCharacteristic *)); static uint16_t uuid16_list[] = { GattService::UUID_HEART_RATE_SERVICE ,GattService::UUID_BATTERY_SERVICE ,GattService::UUID_DEVICE_INFORMATION_SERVICE }; void updateServiceValues(void); static Gap::ConnectionParams_t connectionParams; void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { DEBUG("Disconnected handle %u, reason %u\r\n", params->handle, params->reason); DEBUG("Restarting the advertising process\r\n"); ble.gap().startAdvertising(); } void onConnectionCallback(const Gap::ConnectionCallbackParams_t *params) //Mod { DEBUG("connected. Got handle %u\r\n", params->handle); connectionParams.slaveLatency = 1; if (ble.gap().updateConnectionParams(params->handle, &connectionParams) != BLE_ERROR_NONE) { DEBUG("failed to update connection paramter\r\n"); } } void periodicCallback(void) { triggerSensorPolling = true; } int main(void) { Ticker ticker; ticker.attach(periodicCallback, 1); DEBUG("Initialising the nRF51822\r\n"); ble.init(); DEBUG("Init done\r\n"); ble.gap().onDisconnection(disconnectionCallback); ble.gap().onConnection(onConnectionCallback); ble.gap().getPreferredConnectionParams(&connectionParams); /* setup advertising */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t*)uuid16_list, sizeof(uuid16_list)); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); ble.gap().setAdvertisingInterval(160); /* 100ms; in multiples of 0.625ms. */ ble.gap().startAdvertising(); DEBUG("Start Advertising\r\n"); ble.gattServer().addService(hrmService); ble.gattServer().addService(battService); ble.gattServer().addService(deviceInformationService); DEBUG("Add Service\r\n"); while (true) { if (triggerSensorPolling) { triggerSensorPolling = false; updateServiceValues(); } else { ble.waitForEvent(); } } } void updateServiceValues(void) { /* Decrement the battery level. */ batt <= 50 ? batt = 100 : batt--; ble.gattServer().write(battLevel.getValueAttribute().getHandle(), (uint8_t *)&batt, sizeof(batt)); /* Randomize the heart rate. */ hrmCounter = (rand() % 150) + 30; bpm[1] = hrmCounter; ble.gattServer().write(hrmChar.getValueAttribute().getHandle(), bpm, sizeof(bpm)); }