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
Diff: source/main.cpp
- Revision:
- 67:6f3587a9594e
- Parent:
- 43:fb2855f7754b
- Child:
- 68:50923da435f2
--- a/source/main.cpp Wed Aug 29 14:45:32 2018 +0100 +++ b/source/main.cpp Sat Sep 01 12:20:56 2018 +0000 @@ -22,7 +22,7 @@ DigitalOut led1(LED1, 1); -const static char DEVICE_NAME[] = "HRM"; +const static char DEVICE_NAME[] = "VT_HRM"; static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE}; static uint8_t hrmCounter = 100; // init HRM to 100bps @@ -44,7 +44,7 @@ if (hrmCounter == 175) { hrmCounter = 100; } - + printf("Updating heart rate: %d\n", hrmCounter); hrServicePtr->updateHeartRate(hrmCounter); } @@ -76,7 +76,28 @@ } printf("%02x\r\n", address[0]); } +void connectionCallback(const Gap::ConnectionCallbackParams_t *params) +{ + printf("Connected!\r\n"); +} +void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) +{ + printf("Input passKey: "); + for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { + printf("%c ", passkey[i]); + } + printf("\r\n"); +} + +void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) +{ + if (status == SecurityManager::SEC_STATUS_SUCCESS) { + printf("Security success\r\n"); + } else { + printf("Security failed\r\n"); + } +} void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) { BLE& ble = params->ble; @@ -90,12 +111,19 @@ if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { return; } - + ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); + ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); ble.gap().onDisconnection(disconnectionCallback); + ble.gap().onConnection(connectionCallback); /* Setup primary service. */ hrServicePtr = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER); + /* Initialize BLE security */ + bool enableBonding = true; + bool requireMITM = true; + ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_DISPLAY_ONLY); + /* 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));