BLE testing connecting

Dependencies:   BLE_API mbed nRF51822

Fork of BLE_HeartRate by Bluetooth Low Energy

Committer:
titusfong
Date:
Fri Aug 12 21:42:25 2016 +0000
Revision:
78:cc60d57d699d
Parent:
75:8128a06c0a21
ble testing for chip

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ktownsend 0:87a7fc231fae 1 /* mbed Microcontroller Library
rgrover1 67:b2d2dee347c0 2 * Copyright (c) 2006-2015 ARM Limited
ktownsend 0:87a7fc231fae 3 *
ktownsend 0:87a7fc231fae 4 * Licensed under the Apache License, Version 2.0 (the "License");
ktownsend 0:87a7fc231fae 5 * you may not use this file except in compliance with the License.
ktownsend 0:87a7fc231fae 6 * You may obtain a copy of the License at
ktownsend 0:87a7fc231fae 7 *
ktownsend 0:87a7fc231fae 8 * http://www.apache.org/licenses/LICENSE-2.0
ktownsend 0:87a7fc231fae 9 *
ktownsend 0:87a7fc231fae 10 * Unless required by applicable law or agreed to in writing, software
ktownsend 0:87a7fc231fae 11 * distributed under the License is distributed on an "AS IS" BASIS,
ktownsend 0:87a7fc231fae 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
ktownsend 0:87a7fc231fae 13 * See the License for the specific language governing permissions and
ktownsend 0:87a7fc231fae 14 * limitations under the License.
ktownsend 0:87a7fc231fae 15 */
ktownsend 0:87a7fc231fae 16
titusfong 78:cc60d57d699d 17 #if NEED_CONSOLE_OUTPUT
titusfong 78:cc60d57d699d 18 #define DEBUG(STR) { if (uart) uart->write(STR, strlen(STR)); }
titusfong 78:cc60d57d699d 19 #else
titusfong 78:cc60d57d699d 20 #define DEBUG(...) /* nothing */
titusfong 78:cc60d57d699d 21 #endif /* #if NEED_CONSOLE_OUTPUT */
titusfong 78:cc60d57d699d 22
ktownsend 0:87a7fc231fae 23 #include "mbed.h"
rgrover1 67:b2d2dee347c0 24 #include "ble/BLE.h"
rgrover1 67:b2d2dee347c0 25 #include "ble/services/HeartRateService.h"
rgrover1 67:b2d2dee347c0 26 #include "ble/services/BatteryService.h"
rgrover1 67:b2d2dee347c0 27 #include "ble/services/DeviceInformationService.h"
titusfong 78:cc60d57d699d 28 #include "UARTService.h"
titusfong 78:cc60d57d699d 29
ktownsend 0:87a7fc231fae 30
rgrover1 47:430545f41113 31 DigitalOut led1(LED1);
ktownsend 0:87a7fc231fae 32
mbedAustin 55:3a7d497a3e03 33 const static char DEVICE_NAME[] = "HRM1";
rgrover1 42:06ebef2e0e44 34 static const uint16_t uuid16_list[] = {GattService::UUID_HEART_RATE_SERVICE,
rgrover1 42:06ebef2e0e44 35 GattService::UUID_DEVICE_INFORMATION_SERVICE};
rgrover1 39:6390604f904c 36 static volatile bool triggerSensorPolling = false;
Rohit Grover 36:ea2a1b4f51c1 37
rgrover1 72:99c283dfe28d 38 uint8_t hrmCounter = 100; // init HRM to 100bps
rgrover1 72:99c283dfe28d 39
rgrover1 72:99c283dfe28d 40 HeartRateService *hrService;
rgrover1 72:99c283dfe28d 41 DeviceInformationService *deviceInfo;
titusfong 78:cc60d57d699d 42 UARTService *uart;
titusfong 78:cc60d57d699d 43
rgrover1 72:99c283dfe28d 44
rgrover1 71:469dbde1a238 45 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params)
ktownsend 0:87a7fc231fae 46 {
rgrover1 73:49b6090478e2 47 BLE::Instance(BLE::DEFAULT_INSTANCE).gap().startAdvertising(); // restart advertising
rgrover1 7:daab8ba5139e 48 }
Rohit Grover 3:24e2b056d229 49
Rohit Grover 11:1d9aafee4984 50 void periodicCallback(void)
Rohit Grover 11:1d9aafee4984 51 {
rgrover1 47:430545f41113 52 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
rgrover1 47:430545f41113 53
rgrover1 39:6390604f904c 54 /* Note that the periodicCallback() executes in interrupt context, so it is safer to do
rgrover1 39:6390604f904c 55 * heavy-weight sensor polling from the main thread. */
rgrover1 39:6390604f904c 56 triggerSensorPolling = true;
Rohit Grover 11:1d9aafee4984 57 }
Rohit Grover 11:1d9aafee4984 58
rgrover1 74:c9d58e7847c4 59 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
ktownsend 0:87a7fc231fae 60 {
rgrover1 74:c9d58e7847c4 61 BLE &ble = params->ble;
rgrover1 74:c9d58e7847c4 62 ble_error_t error = params->error;
rgrover1 74:c9d58e7847c4 63
rgrover1 72:99c283dfe28d 64 if (error != BLE_ERROR_NONE) {
rgrover1 72:99c283dfe28d 65 return;
rgrover1 72:99c283dfe28d 66 }
ktownsend 0:87a7fc231fae 67
rgrover1 65:cb76569f74f6 68 ble.gap().onDisconnection(disconnectionCallback);
ktownsend 0:87a7fc231fae 69
rgrover1 45:98c5a34b07a4 70 /* Setup primary service. */
rgrover1 72:99c283dfe28d 71 hrService = new HeartRateService(ble, hrmCounter, HeartRateService::LOCATION_FINGER);
rgrover1 45:98c5a34b07a4 72
mbedAustin 55:3a7d497a3e03 73 /* Setup auxiliary service. */
rgrover1 72:99c283dfe28d 74 deviceInfo = new DeviceInformationService(ble, "ARM", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");
rgrover1 45:98c5a34b07a4 75
rgrover1 45:98c5a34b07a4 76 /* Setup advertising. */
rgrover1 65:cb76569f74f6 77 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
rgrover1 65:cb76569f74f6 78 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
rgrover1 65:cb76569f74f6 79 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::GENERIC_HEART_RATE_SENSOR);
rgrover1 65:cb76569f74f6 80 ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
rgrover1 65:cb76569f74f6 81 ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
titusfong 78:cc60d57d699d 82 ble.gap().setAdvertisingInterval(1500); /* 1000ms */
rgrover1 65:cb76569f74f6 83 ble.gap().startAdvertising();
rgrover1 72:99c283dfe28d 84 }
rgrover1 72:99c283dfe28d 85
rgrover1 72:99c283dfe28d 86 int main(void)
rgrover1 72:99c283dfe28d 87 {
rgrover1 72:99c283dfe28d 88 led1 = 1;
rgrover1 72:99c283dfe28d 89 Ticker ticker;
rgrover1 72:99c283dfe28d 90 ticker.attach(periodicCallback, 1); // blink LED every second
rgrover1 72:99c283dfe28d 91
rgrover1 73:49b6090478e2 92 BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);
rgrover1 72:99c283dfe28d 93 ble.init(bleInitComplete);
titusfong 78:cc60d57d699d 94 uart = new UARTService(ble);
titusfong 78:cc60d57d699d 95
Rohit Grover 3:24e2b056d229 96
rgrover1 73:49b6090478e2 97 /* SpinWait for initialization to complete. This is necessary because the
rgrover1 73:49b6090478e2 98 * BLE object is used in the main loop below. */
vcoubard 75:8128a06c0a21 99 while (ble.hasInitialized() == false) { /* spin loop */ }
rgrover1 73:49b6090478e2 100
mbedAustin 55:3a7d497a3e03 101 // infinite loop
mbedAustin 55:3a7d497a3e03 102 while (1) {
mbedAustin 55:3a7d497a3e03 103 // check for trigger from periodicCallback()
rgrover1 50:477004d54431 104 if (triggerSensorPolling && ble.getGapState().connected) {
Rohit Grover 36:ea2a1b4f51c1 105 triggerSensorPolling = false;
titusfong 78:cc60d57d699d 106 DEBUG("ping\r\n");
titusfong 78:cc60d57d699d 107 wait_ms(500);
mbedAustin 55:3a7d497a3e03 108 // Do blocking calls or whatever is necessary for sensor polling.
rgrover1 67:b2d2dee347c0 109 // In our case, we simply update the HRM measurement.
titusfong 78:cc60d57d699d 110 /* hrmCounter++;
rgrover1 72:99c283dfe28d 111 if (hrmCounter == 175) { // 100 <= HRM bps <=175
Rohit Grover 36:ea2a1b4f51c1 112 hrmCounter = 100;
Rohit Grover 36:ea2a1b4f51c1 113 }
titusfong 78:cc60d57d699d 114 */
titusfong 78:cc60d57d699d 115
titusfong 78:cc60d57d699d 116
rgrover1 67:b2d2dee347c0 117
rgrover1 72:99c283dfe28d 118 hrService->updateHeartRate(hrmCounter);
Rohit Grover 36:ea2a1b4f51c1 119 } else {
mbedAustin 55:3a7d497a3e03 120 ble.waitForEvent(); // low power wait for event
Rohit Grover 36:ea2a1b4f51c1 121 }
ktownsend 0:87a7fc231fae 122 }
ktownsend 0:87a7fc231fae 123 }