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
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2015 ARM Limited 00003 * 00004 * Licensed under the Apache License, Version 2.0 (the "License"); 00005 * you may not use this file except in compliance with the License. 00006 * You may obtain a copy of the License at 00007 * 00008 * http://www.apache.org/licenses/LICENSE-2.0 00009 * 00010 * Unless required by applicable law or agreed to in writing, software 00011 * distributed under the License is distributed on an "AS IS" BASIS, 00012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 00013 * See the License for the specific language governing permissions and 00014 * limitations under the License. 00015 */ 00016 00017 #include <events/mbed_events.h> 00018 #include <mbed.h> 00019 #include "ble/BLE.h" 00020 #include "ble/Gap.h" 00021 #include "HeartRateService.h" 00022 00023 DigitalOut led1(LED1, 1); 00024 00025 const static char DEVICE_NAME[] = "VT_HRM"; 00026 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE}; 00027 static uint16_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}; 00028 static uint16_t counter = 0; 00029 static uint16_t hrmCounter = 800; // init 00030 00031 static HeartRateService *hrServicePtr; 00032 00033 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE); 00034 00035 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) 00036 { 00037 BLE::Instance().gap().startAdvertising(); // restart advertising 00038 } 00039 00040 void updateSensorValue() { 00041 // Do blocking calls or whatever is necessary for sensor polling. 00042 // In our case, we simply update the HRM measurement. 00043 hrmCounter = rriValues[counter]; 00044 counter++; 00045 00046 // There are x RRIs to cycle through (usually 1024) 00047 if (counter == 100) { 00048 counter = 0; 00049 } 00050 hrServicePtr->updateHeartRate(hrmCounter); 00051 } 00052 00053 void periodicCallback(void) 00054 { 00055 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 00056 00057 if (BLE::Instance().getGapState().connected) { 00058 eventQueue.call(updateSensorValue); 00059 } 00060 } 00061 00062 void onBleInitError(BLE &ble, ble_error_t error) 00063 { 00064 (void)ble; 00065 (void)error; 00066 /* Initialization error handling should go here */ 00067 } 00068 00069 void printMacAddress() 00070 { 00071 /* Print out device MAC address to the console*/ 00072 Gap::AddressType_t addr_type; 00073 Gap::Address_t address; 00074 BLE::Instance().gap().getAddress(&addr_type, address); 00075 printf("DEVICE MAC ADDRESS: "); 00076 for (int i = 5; i >= 1; i--){ 00077 printf("%02x:", address[i]); 00078 } 00079 printf("%02x\r\n", address[0]); 00080 } 00081 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) 00082 { 00083 printf("Connected!\r\n"); 00084 } 00085 00086 void passkeyDisplayCallback(Gap::Handle_t handle, const SecurityManager::Passkey_t passkey) 00087 { 00088 printf("Input passKey: "); 00089 for (unsigned i = 0; i < Gap::ADDR_LEN; i++) { 00090 printf("%c ", passkey[i]); 00091 } 00092 printf("\r\n"); 00093 } 00094 00095 void securitySetupCompletedCallback(Gap::Handle_t handle, SecurityManager::SecurityCompletionStatus_t status) 00096 { 00097 if (status == SecurityManager::SEC_STATUS_SUCCESS) { 00098 printf("Security success\r\n"); 00099 } else { 00100 printf("Security failed\r\n"); 00101 } 00102 } 00103 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) 00104 { 00105 BLE& ble = params->ble; 00106 ble_error_t error = params->error; 00107 00108 if (error != BLE_ERROR_NONE) { 00109 onBleInitError(ble, error); 00110 return; 00111 } 00112 00113 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { 00114 return; 00115 } 00116 ble.securityManager().onPasskeyDisplay(passkeyDisplayCallback); 00117 ble.securityManager().onSecuritySetupCompleted(securitySetupCompletedCallback); 00118 ble.gap().onDisconnection(disconnectionCallback); 00119 ble.gap().onConnection(connectionCallback); 00120 00121 /* Setup primary service. */ 00122 hrServicePtr = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_CHEST ); 00123 00124 /* Initialize BLE security */ 00125 bool enableBonding = true; 00126 bool requireMITM = true; 00127 ble.securityManager().init(enableBonding, requireMITM, SecurityManager::IO_CAPS_DISPLAY_ONLY); 00128 00129 /* Setup advertising. */ 00130 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00131 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00132 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); 00133 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00134 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00135 ble.gap().setAdvertisingInterval(1000); /* 1000ms */ 00136 ble.gap().startAdvertising(); 00137 00138 printMacAddress(); 00139 } 00140 00141 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) { 00142 BLE &ble = BLE::Instance(); 00143 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents)); 00144 } 00145 00146 int main() 00147 { 00148 eventQueue.call_every(500, periodicCallback); 00149 00150 BLE &ble = BLE::Instance(); 00151 ble.onEventsToProcess(scheduleBleEventsProcessing); 00152 ble.init(bleInitComplete); 00153 00154 eventQueue.dispatch_forever(); 00155 00156 return 0; 00157 }
Generated on Fri Jul 15 2022 01:51:31 by 1.7.2