Implementation of Heart Rate Service on nRF52-DK with security (bonding/pairing procedure). Correct key for smartphone to enter is printed on serial. Baud rate 9600, 8 bits + 1 stop bit + no parity bit.

Fork of mbed-os-example-ble-HeartRate by mbed-os-examples

Committer:
marcusjzw
Date:
Tue Oct 09 23:03:58 2018 +0000
Revision:
69:f48bb19847e3
Parent:
68:50923da435f2
Child:
70:cae5b05ed2b9
added 100 values to stop internal errors hopefully

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 1:72c60abef7e7 1 /* mbed Microcontroller Library
mbed_official 1:72c60abef7e7 2 * Copyright (c) 2006-2015 ARM Limited
mbed_official 1:72c60abef7e7 3 *
mbed_official 1:72c60abef7e7 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 1:72c60abef7e7 5 * you may not use this file except in compliance with the License.
mbed_official 1:72c60abef7e7 6 * You may obtain a copy of the License at
mbed_official 1:72c60abef7e7 7 *
mbed_official 1:72c60abef7e7 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 1:72c60abef7e7 9 *
mbed_official 1:72c60abef7e7 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 1:72c60abef7e7 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 1:72c60abef7e7 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 1:72c60abef7e7 13 * See the License for the specific language governing permissions and
mbed_official 1:72c60abef7e7 14 * limitations under the License.
mbed_official 1:72c60abef7e7 15 */
mbed_official 1:72c60abef7e7 16
mbed_official 10:ac3615194d04 17 #include <events/mbed_events.h>
mbed_official 1:72c60abef7e7 18 #include <mbed.h>
mbed_official 1:72c60abef7e7 19 #include "ble/BLE.h"
mbed_official 1:72c60abef7e7 20 #include "ble/Gap.h"
marcusjzw 68:50923da435f2 21 #include "HeartRateService.h"
mbed_official 1:72c60abef7e7 22
mbed_official 1:72c60abef7e7 23 DigitalOut led1(LED1, 1);
mbed_official 1:72c60abef7e7 24
marcusjzw 67:6f3587a9594e 25 const static char DEVICE_NAME[] = "VT_HRM";
mbed_official 1:72c60abef7e7 26 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE};
marcusjzw 69:f48bb19847e3 27 static uint8_t rriValues[100] = {880,860,860,860,870,880,890,910,920,930,940,940,950,940,950,940,950,950,950,950,530,1350,950,930,910,920,940,940,950,940,950,940,930,940,950,950,950,960,960,960,950,960,950,960,950,960,960,950,960,960,950,950,940,950,950,940,930,930,920,920,910,920,910,900,670,1130,910,880,880,870,660,1100,890,880,880,880,660,1110,910,890,900,900,690,1110,910,900,910,900,900,910,930,920,940,940,940,950,960,950,950,960};
marcusjzw 69:f48bb19847e3 28 static uint16_t counter = 0;
marcusjzw 69:f48bb19847e3 29 static uint8_t hrmCounter = 800; // init
mbed_official 1:72c60abef7e7 30
mbed_official 1:72c60abef7e7 31 static HeartRateService *hrServicePtr;
mbed_official 1:72c60abef7e7 32
mbed_official 26:d7dd71a8aea1 33 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
mbed_official 1:72c60abef7e7 34
mbed_official 1:72c60abef7e7 35 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
mbed_official 1:72c60abef7e7 36 {
mbed_official 1:72c60abef7e7 37 BLE::Instance().gap().startAdvertising(); // restart advertising
mbed_official 1:72c60abef7e7 38 }
mbed_official 1:72c60abef7e7 39
mbed_official 1:72c60abef7e7 40 void updateSensorValue() {
mbed_official 1:72c60abef7e7 41 // Do blocking calls or whatever is necessary for sensor polling.
mbed_official 1:72c60abef7e7 42 // In our case, we simply update the HRM measurement.
marcusjzw 69:f48bb19847e3 43 hrmCounter = rriValues[counter];
marcusjzw 69:f48bb19847e3 44 counter++;
mbed_official 1:72c60abef7e7 45
marcusjzw 69:f48bb19847e3 46 // There are x RRIs to cycle through (usually 1024)
marcusjzw 69:f48bb19847e3 47 if (counter == 100) {
marcusjzw 69:f48bb19847e3 48 counter = 0;
mbed_official 1:72c60abef7e7 49 }
mbed_official 1:72c60abef7e7 50 hrServicePtr->updateHeartRate(hrmCounter);
mbed_official 1:72c60abef7e7 51 }
mbed_official 1:72c60abef7e7 52
mbed_official 1:72c60abef7e7 53 void periodicCallback(void)
mbed_official 1:72c60abef7e7 54 {
mbed_official 1:72c60abef7e7 55 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
mbed_official 1:72c60abef7e7 56
mbed_official 1:72c60abef7e7 57 if (BLE::Instance().getGapState().connected) {
mbed_official 10:ac3615194d04 58 eventQueue.call(updateSensorValue);
mbed_official 1:72c60abef7e7 59 }
mbed_official 1:72c60abef7e7 60 }
mbed_official 1:72c60abef7e7 61
mbed_official 1:72c60abef7e7 62 void onBleInitError(BLE &ble, ble_error_t error)
mbed_official 1:72c60abef7e7 63 {
mbed_official 1:72c60abef7e7 64 (void)ble;
mbed_official 1:72c60abef7e7 65 (void)error;
mbed_official 1:72c60abef7e7 66 /* Initialization error handling should go here */
mbed_official 1:72c60abef7e7 67 }
mbed_official 1:72c60abef7e7 68
mbed_official 43:fb2855f7754b 69 void printMacAddress()
mbed_official 43:fb2855f7754b 70 {
mbed_official 43:fb2855f7754b 71 /* Print out device MAC address to the console*/
mbed_official 43:fb2855f7754b 72 Gap::AddressType_t addr_type;
mbed_official 43:fb2855f7754b 73 Gap::Address_t address;
mbed_official 43:fb2855f7754b 74 BLE::Instance().gap().getAddress(&addr_type, address);
mbed_official 43:fb2855f7754b 75 printf("DEVICE MAC ADDRESS: ");
mbed_official 43:fb2855f7754b 76 for (int i = 5; i >= 1; i--){
mbed_official 43:fb2855f7754b 77 printf("%02x:", address[i]);
mbed_official 43:fb2855f7754b 78 }
mbed_official 43:fb2855f7754b 79 printf("%02x\r\n", address[0]);
mbed_official 43:fb2855f7754b 80 }
marcusjzw 67:6f3587a9594e 81 void connectionCallback(const Gap::ConnectionCallbackParams_t *params)
marcusjzw 67:6f3587a9594e 82 {
marcusjzw 67:6f3587a9594e 83 printf("Connected!\r\n");
marcusjzw 67:6f3587a9594e 84 }
mbed_official 43:fb2855f7754b 85
marcusjzw 67:6f3587a9594e 86 void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey)
marcusjzw 67:6f3587a9594e 87 {
marcusjzw 67:6f3587a9594e 88 printf("Input passKey: ");
marcusjzw 67:6f3587a9594e 89 for (unsigned i = 0; i < Gap::ADDR_LEN; i++) {
marcusjzw 67:6f3587a9594e 90 printf("%c ", passkey[i]);
marcusjzw 67:6f3587a9594e 91 }
marcusjzw 67:6f3587a9594e 92 printf("\r\n");
marcusjzw 67:6f3587a9594e 93 }
marcusjzw 67:6f3587a9594e 94
marcusjzw 67:6f3587a9594e 95 void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status)
marcusjzw 67:6f3587a9594e 96 {
marcusjzw 67:6f3587a9594e 97 if (status == SecurityManager::SEC_STATUS_SUCCESS) {
marcusjzw 67:6f3587a9594e 98 printf("Security success\r\n");
marcusjzw 67:6f3587a9594e 99 } else {
marcusjzw 67:6f3587a9594e 100 printf("Security failed\r\n");
marcusjzw 67:6f3587a9594e 101 }
marcusjzw 67:6f3587a9594e 102 }
mbed_official 1:72c60abef7e7 103 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
mbed_official 1:72c60abef7e7 104 {
mbed_official 1:72c60abef7e7 105 BLE& ble = params->ble;
mbed_official 1:72c60abef7e7 106 ble_error_t error = params->error;
mbed_official 1:72c60abef7e7 107
mbed_official 1:72c60abef7e7 108 if (error != BLE_ERROR_NONE) {
mbed_official 1:72c60abef7e7 109 onBleInitError(ble, error);
mbed_official 1:72c60abef7e7 110 return;
mbed_official 1:72c60abef7e7 111 }
mbed_official 1:72c60abef7e7 112
mbed_official 1:72c60abef7e7 113 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
mbed_official 1:72c60abef7e7 114 return;
mbed_official 1:72c60abef7e7 115 }
marcusjzw 67:6f3587a9594e 116 ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback);
marcusjzw 67:6f3587a9594e 117 ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback);
mbed_official 1:72c60abef7e7 118 ble.gap().onDisconnection(disconnectionCallback);
marcusjzw 67:6f3587a9594e 119 ble.gap().onConnection(connectionCallback);
mbed_official 1:72c60abef7e7 120
mbed_official 1:72c60abef7e7 121 /* Setup primary service. */
mbed_official 1:72c60abef7e7 122 hrServicePtr = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
mbed_official 1:72c60abef7e7 123
marcusjzw 67:6f3587a9594e 124 /* Initialize BLE security */
marcusjzw 67:6f3587a9594e 125 bool enableBonding = true;
marcusjzw 67:6f3587a9594e 126 bool requireMITM = true;
marcusjzw 67:6f3587a9594e 127 ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_DISPLAY_ONLY);
marcusjzw 67:6f3587a9594e 128
mbed_official 1:72c60abef7e7 129 /* Setup advertising. */
mbed_official 1:72c60abef7e7 130 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
mbed_official 1:72c60abef7e7 131 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
mbed_official 1:72c60abef7e7 132 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
mbed_official 1:72c60abef7e7 133 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
mbed_official 1:72c60abef7e7 134 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
mbed_official 1:72c60abef7e7 135 ble.gap().setAdvertisingInterval(1000); /* 1000ms */
mbed_official 1:72c60abef7e7 136 ble.gap().startAdvertising();
mbed_official 43:fb2855f7754b 137
mbed_official 43:fb2855f7754b 138 printMacAddress();
mbed_official 1:72c60abef7e7 139 }
mbed_official 1:72c60abef7e7 140
mbed_official 1:72c60abef7e7 141 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
mbed_official 1:72c60abef7e7 142 BLE &ble = BLE::Instance();
mbed_official 10:ac3615194d04 143 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
mbed_official 1:72c60abef7e7 144 }
mbed_official 1:72c60abef7e7 145
mbed_official 1:72c60abef7e7 146 int main()
mbed_official 1:72c60abef7e7 147 {
mbed_official 10:ac3615194d04 148 eventQueue.call_every(500, periodicCallback);
mbed_official 1:72c60abef7e7 149
mbed_official 1:72c60abef7e7 150 BLE &ble = BLE::Instance();
mbed_official 1:72c60abef7e7 151 ble.onEventsToProcess(scheduleBleEventsProcessing);
mbed_official 1:72c60abef7e7 152 ble.init(bleInitComplete);
mbed_official 1:72c60abef7e7 153
mbed_official 10:ac3615194d04 154 eventQueue.dispatch_forever();
mbed_official 1:72c60abef7e7 155
mbed_official 1:72c60abef7e7 156 return 0;
mbed_official 1:72c60abef7e7 157 }