Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
main.cpp
00001 /* mbed Microcontroller Library 00002 * Copyright (c) 2006-2013 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 "mbed.h" 00018 #include "BLEDevice.h" 00019 #include "HeartRateSecSampleSrv.h" 00020 #include "DeviceInformationService.h" 00021 00022 /* Enable the following if you need to throttle the connection interval. This has 00023 * the effect of reducing energy consumption after a connection is made; 00024 * particularly for applications where the central may want a fast connection 00025 * interval.*/ 00026 #define UPDATE_PARAMS_FOR_LONGER_CONNECTION_INTERVAL 0 00027 00028 BLEDevice ble; 00029 DigitalOut led1(LED1); 00030 00031 const static char DEVICE_NAME[] = "HRM_SEC"; 00032 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE, 00033 GattService::UUID_DEVICE_INFORMATION_SERVICE}; 00034 static volatile bool triggerSensorPolling = false; 00035 00036 void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason) 00037 { 00038 printf("Disconnected!\r\n"); 00039 ble.startAdvertising(); // restart advertising 00040 } 00041 00042 00043 void periodicCallback(void) 00044 { 00045 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ 00046 00047 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do 00048 * heavy-weight sensor polling from the main thread. */ 00049 triggerSensorPolling = true; 00050 } 00051 00052 00053 void connectionCallback(Gap::Handle_t handle, Gap::addr_type_t peerAddrType, const Gap::Address_t peerAddr, 00054 Gap::addr_type_t ownAddrType, const Gap::Address_t ownAddr, 00055 const Gap::ConnectionParams_t * pParam) 00056 { 00057 printf("Connected!\r\n"); 00058 } 00059 00060 00061 void passkeyDisplayCallback(Gap::Handle_t handle, const Gap::Passkey_t passkey) 00062 { 00063 uint8_t i; 00064 00065 printf("Input passKey: "); 00066 for(i = 0; i < 6; i++) { 00067 printf("%c ", passkey[i]); 00068 } 00069 printf("\r\n"); 00070 } 00071 00072 00073 void securitySetupCompletedCallback(Gap::Handle_t handle, Gap::SecurityCompletionStatus_t status) 00074 { 00075 if(!status) { 00076 printf("Security success\r\n", status); 00077 } else { 00078 printf("Security failed\r\n", status); 00079 } 00080 } 00081 00082 00083 int main(void) 00084 { 00085 led1 = 1; 00086 Ticker ticker; 00087 ticker.attach(periodicCallback, 1); // blink LED every second 00088 printf("Started\r\n"); 00089 00090 /* Initialize BLE module */ 00091 ble.init(); 00092 00093 /* Initialize BLE security */ 00094 ble.initializeSecurity(true, true, Gap::IO_CAPS_DISPLAY_ONLY, NULL); 00095 00096 /* Set callback functions */ 00097 ble.onConnection(connectionCallback); 00098 ble.onDisconnection(disconnectionCallback); 00099 ble.onPasskeyDisplay(passkeyDisplayCallback); 00100 ble.onSecuritySetupCompleted(securitySetupCompletedCallback); 00101 00102 /* Setup primary service. */ 00103 uint8_t hrmCounter = 100; // init HRM to 100bps 00104 HeartRateSecService hrService(ble, hrmCounter, HeartRateSecService::LOCATION_FINGER ); 00105 00106 /* Setup auxiliary service. */ 00107 DeviceInformationService deviceInfo(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1"); 00108 00109 /* Setup advertising. */ 00110 ble.accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); 00111 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); 00112 ble.accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR); 00113 ble.accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); 00114 ble.setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); 00115 ble.setAdvertisingInterval(1000); 00116 00117 ble.startAdvertising(); 00118 00119 // infinite loop 00120 while (1) { 00121 // check for trigger from periodicCallback() 00122 if (triggerSensorPolling && ble.getGapState().connected) { 00123 triggerSensorPolling = false; 00124 00125 // Do blocking calls or whatever is necessary for sensor polling. 00126 // In our case, we simply update the HRM measurement. 00127 hrmCounter++; 00128 00129 // 100 <= HRM bps <=175 00130 if (hrmCounter == 175) { 00131 hrmCounter = 100; 00132 } 00133 00134 // update bps 00135 hrService.updateHeartRate(hrmCounter); 00136 } else { 00137 ble.waitForEvent(); // low power wait for event 00138 } 00139 } 00140 }
Generated on Sun Jul 24 2022 03:43:19 by
1.7.2