aaaa

Dependencies:   mbed

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

Committer:
fbdp1202
Date:
Thu Jun 29 06:17:05 2017 +0000
Revision:
34:b2d964ce740f
Parent:
33:d83bd71e5d82
Heart Rate scanner

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 13:c5f3a7d8764a 1 #include <events/mbed_events.h>
mbed_official 3:b1e385adea43 2 #include "mbed.h"
mbed_official 3:b1e385adea43 3 #include "ble/BLE.h"
fbdp1202 33:d83bd71e5d82 4 #include "ble/DiscoveredCharacteristic.h"
fbdp1202 33:d83bd71e5d82 5 #include "ble/DiscoveredService.h"
fbdp1202 33:d83bd71e5d82 6 #include "ble/services/HeartRateService.h"
mbed_official 3:b1e385adea43 7
fbdp1202 34:b2d964ce740f 8 DigitalOut led1(LED1, 1);
fbdp1202 34:b2d964ce740f 9 //I2C i2c(p27, p26); // sda, scl
fbdp1202 34:b2d964ce740f 10 Serial uart(p13, p14); // tx, rx
fbdp1202 33:d83bd71e5d82 11 static DiscoveredCharacteristic ledCharacteristic;
fbdp1202 33:d83bd71e5d82 12 static bool triggerLedCharacteristic;
fbdp1202 34:b2d964ce740f 13 static const char PEER_NAME[] = "HRM";
fbdp1202 34:b2d964ce740f 14 //const int address = 0x3C<<1;
mbed_official 3:b1e385adea43 15
fbdp1202 34:b2d964ce740f 16 static EventQueue eventQueue(/* event count */ 16 * EVENTS_EVENT_SIZE);
fbdp1202 34:b2d964ce740f 17 char HeartRate_Data = 0;
mbed_official 3:b1e385adea43 18
fbdp1202 34:b2d964ce740f 19 void periodicCallback(void) {
fbdp1202 34:b2d964ce740f 20 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
fbdp1202 33:d83bd71e5d82 21 }
mbed_official 3:b1e385adea43 22
mbed_official 3:b1e385adea43 23
fbdp1202 34:b2d964ce740f 24 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
fbdp1202 34:b2d964ce740f 25 // parse the advertising payload, looking for data type COMPLETE_LOCAL_NAME
fbdp1202 34:b2d964ce740f 26 // The advertising payload is a collection of key/value records where
fbdp1202 34:b2d964ce740f 27 // byte 0: length of the record excluding this byte
fbdp1202 34:b2d964ce740f 28 // byte 1: The key, it is the type of the data
fbdp1202 34:b2d964ce740f 29 // byte [2..N] The value. N is equal to byte0 - 1
fbdp1202 34:b2d964ce740f 30 printf("Hello:)\r\n");
fbdp1202 34:b2d964ce740f 31 printf(
fbdp1202 34:b2d964ce740f 32 "adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
fbdp1202 34:b2d964ce740f 33 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2],
fbdp1202 34:b2d964ce740f 34 params->peerAddr[1], params->peerAddr[0], params->rssi, params->isScanResponse, params->type
fbdp1202 34:b2d964ce740f 35 );
fbdp1202 33:d83bd71e5d82 36 for (uint8_t i = 0; i < params->advertisingDataLen; ++i) {
mbed_official 3:b1e385adea43 37
fbdp1202 33:d83bd71e5d82 38 const uint8_t record_length = params->advertisingData[i];
fbdp1202 33:d83bd71e5d82 39 if (record_length == 0) {
fbdp1202 33:d83bd71e5d82 40 continue;
fbdp1202 33:d83bd71e5d82 41 }
fbdp1202 33:d83bd71e5d82 42 const uint8_t type = params->advertisingData[i + 1];
fbdp1202 33:d83bd71e5d82 43 const uint8_t* value = params->advertisingData + i + 2;
fbdp1202 33:d83bd71e5d82 44 const uint8_t value_length = record_length - 1;
fbdp1202 33:d83bd71e5d82 45
fbdp1202 33:d83bd71e5d82 46
fbdp1202 33:d83bd71e5d82 47 if(type == GapAdvertisingData::COMPLETE_LOCAL_NAME) {
fbdp1202 33:d83bd71e5d82 48 if ((value_length == sizeof(PEER_NAME)) && (memcmp(value, PEER_NAME, value_length) == 0)) {
fbdp1202 33:d83bd71e5d82 49 printf(
fbdp1202 33:d83bd71e5d82 50 "adv peerAddr[%02x %02x %02x %02x %02x %02x] rssi %d, isScanResponse %u, AdvertisementType %u\r\n",
fbdp1202 33:d83bd71e5d82 51 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2],
fbdp1202 33:d83bd71e5d82 52 params->peerAddr[1], params->peerAddr[0], params->rssi, params->isScanResponse, params->type
fbdp1202 33:d83bd71e5d82 53 );
fbdp1202 33:d83bd71e5d82 54 BLE::Instance().gap().connect(params->peerAddr, Gap::ADDR_TYPE_RANDOM_STATIC, NULL, NULL);
fbdp1202 33:d83bd71e5d82 55 break;
fbdp1202 33:d83bd71e5d82 56 }
fbdp1202 33:d83bd71e5d82 57 }
fbdp1202 33:d83bd71e5d82 58 i += record_length;
fbdp1202 34:b2d964ce740f 59 }
fbdp1202 34:b2d964ce740f 60 }
fbdp1202 34:b2d964ce740f 61
fbdp1202 34:b2d964ce740f 62
fbdp1202 34:b2d964ce740f 63 void serviceDiscoveryCallback(const DiscoveredService *service) {
fbdp1202 34:b2d964ce740f 64 if (service->getUUID().shortOrLong() == UUID::UUID_TYPE_SHORT) {
fbdp1202 34:b2d964ce740f 65 printf("S UUID-%x attrs[%u %u]\r\n", service->getUUID().getShortUUID(), service->getStartHandle(), service->getEndHandle());
fbdp1202 34:b2d964ce740f 66 } else {
fbdp1202 34:b2d964ce740f 67 printf("S UUID-");
fbdp1202 34:b2d964ce740f 68 const uint8_t *longUUIDBytes = service->getUUID().getBaseUUID();
fbdp1202 34:b2d964ce740f 69 for (unsigned i = 0; i < UUID::LENGTH_OF_LONG_UUID; i++) {
fbdp1202 34:b2d964ce740f 70 printf("%02x", longUUIDBytes[i]);
fbdp1202 34:b2d964ce740f 71 }
fbdp1202 34:b2d964ce740f 72 printf(" attrs[%u %u]\r\n", service->getStartHandle(), service->getEndHandle());
fbdp1202 34:b2d964ce740f 73 }
fbdp1202 34:b2d964ce740f 74 }
fbdp1202 34:b2d964ce740f 75
fbdp1202 34:b2d964ce740f 76
fbdp1202 34:b2d964ce740f 77 void updateLedCharacteristic(void) {
fbdp1202 34:b2d964ce740f 78 if (!BLE::Instance().gattClient().isServiceDiscoveryActive()) {
fbdp1202 34:b2d964ce740f 79 ledCharacteristic.read();
fbdp1202 34:b2d964ce740f 80 }
fbdp1202 34:b2d964ce740f 81 }
fbdp1202 34:b2d964ce740f 82
fbdp1202 34:b2d964ce740f 83 void triggerToggledWrite(const GattReadCallbackParams *response) {
fbdp1202 34:b2d964ce740f 84 printf("triggerToggledWrite\r\n");
fbdp1202 34:b2d964ce740f 85 printf("triggerToggledWrite: handle %u, offset %u, len %u\r\n", response->handle, response->offset, response->len);
fbdp1202 34:b2d964ce740f 86 HeartRate_Data = response->data[1];
fbdp1202 34:b2d964ce740f 87 printf("HeartRate_Data = %d\r\n", HeartRate_Data);
fbdp1202 34:b2d964ce740f 88 uart.putc(HeartRate_Data);
fbdp1202 34:b2d964ce740f 89 ledCharacteristic.read();
fbdp1202 34:b2d964ce740f 90 }
fbdp1202 34:b2d964ce740f 91
fbdp1202 34:b2d964ce740f 92
fbdp1202 34:b2d964ce740f 93 void characteristicDiscoveryCallback(const DiscoveredCharacteristic *characteristicP) {
fbdp1202 34:b2d964ce740f 94 printf(" C UUID-%x valueAttr[%u] props[%x]\r\n", characteristicP->getUUID().getShortUUID(), characteristicP->getValueHandle(), (uint8_t)characteristicP->getProperties().broadcast());
fbdp1202 34:b2d964ce740f 95 if (characteristicP->getUUID().getShortUUID() == 0x2a37) { /* !ALERT! Alter this filter to suit your device. */
fbdp1202 34:b2d964ce740f 96 characteristicP->read(0);
fbdp1202 34:b2d964ce740f 97 ledCharacteristic = *characteristicP;
fbdp1202 34:b2d964ce740f 98 triggerLedCharacteristic = true;
fbdp1202 34:b2d964ce740f 99 }
fbdp1202 34:b2d964ce740f 100 }
fbdp1202 34:b2d964ce740f 101
mbed_official 3:b1e385adea43 102
fbdp1202 33:d83bd71e5d82 103 void discoveryTerminationCallback(Gap::Handle_t connectionHandle) {
fbdp1202 33:d83bd71e5d82 104 printf("terminated SD for handle %u\r\n", connectionHandle);
fbdp1202 33:d83bd71e5d82 105 if (triggerLedCharacteristic) {
fbdp1202 33:d83bd71e5d82 106 triggerLedCharacteristic = false;
fbdp1202 33:d83bd71e5d82 107 eventQueue.call(updateLedCharacteristic);
fbdp1202 33:d83bd71e5d82 108 }
fbdp1202 33:d83bd71e5d82 109 }
fbdp1202 33:d83bd71e5d82 110
fbdp1202 34:b2d964ce740f 111
fbdp1202 33:d83bd71e5d82 112 void connectionCallback(const Gap::ConnectionCallbackParams_t *params) {
fbdp1202 34:b2d964ce740f 113 printf("connectionCallback\r\n");
fbdp1202 34:b2d964ce740f 114 if (params->role == Gap::CENTRAL) {
fbdp1202 34:b2d964ce740f 115 printf("Gap::CENTRAL\r\n");
fbdp1202 34:b2d964ce740f 116 BLE &ble = BLE::Instance();
fbdp1202 34:b2d964ce740f 117 ble.gattClient().onServiceDiscoveryTermination(discoveryTerminationCallback);
fbdp1202 34:b2d964ce740f 118 ble.gattClient().launchServiceDiscovery(params->handle, serviceDiscoveryCallback, characteristicDiscoveryCallback/*, 0xa000, 0xa001*/);
fbdp1202 34:b2d964ce740f 119 }
fbdp1202 34:b2d964ce740f 120 }
fbdp1202 34:b2d964ce740f 121
fbdp1202 34:b2d964ce740f 122 void triggerRead(const GattWriteCallbackParams *response) {
fbdp1202 34:b2d964ce740f 123 printf("triggerRead\r\n");
fbdp1202 34:b2d964ce740f 124 if (response->handle == ledCharacteristic.getValueHandle()) {
fbdp1202 34:b2d964ce740f 125 ledCharacteristic.read();
fbdp1202 34:b2d964ce740f 126 }
fbdp1202 34:b2d964ce740f 127 }
fbdp1202 34:b2d964ce740f 128
fbdp1202 33:d83bd71e5d82 129
fbdp1202 34:b2d964ce740f 130 void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *) {
fbdp1202 34:b2d964ce740f 131 printf("disconnected\r\n");
fbdp1202 34:b2d964ce740f 132 /* Start scanning and try to connect again */
fbdp1202 34:b2d964ce740f 133 BLE::Instance().gap().startScan(advertisementCallback);
fbdp1202 34:b2d964ce740f 134 }
fbdp1202 34:b2d964ce740f 135
fbdp1202 34:b2d964ce740f 136
fbdp1202 34:b2d964ce740f 137 void onBleInitError(BLE &ble, ble_error_t error)
fbdp1202 34:b2d964ce740f 138 {
fbdp1202 34:b2d964ce740f 139 /* Initialization error handling should go here */
fbdp1202 34:b2d964ce740f 140 }
fbdp1202 34:b2d964ce740f 141
mbed_official 3:b1e385adea43 142
fbdp1202 34:b2d964ce740f 143 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
fbdp1202 34:b2d964ce740f 144 {
fbdp1202 34:b2d964ce740f 145 BLE& ble = params->ble;
fbdp1202 34:b2d964ce740f 146 ble_error_t error = params->error;
fbdp1202 34:b2d964ce740f 147
fbdp1202 34:b2d964ce740f 148 printf("Init_1\r\n");
mbed_official 3:b1e385adea43 149
fbdp1202 34:b2d964ce740f 150 if (error != BLE_ERROR_NONE) {
fbdp1202 34:b2d964ce740f 151 /* In case of error, forward the error handling to onBleInitError */
fbdp1202 34:b2d964ce740f 152 onBleInitError(ble, error);
fbdp1202 34:b2d964ce740f 153 return;
fbdp1202 34:b2d964ce740f 154 }
fbdp1202 34:b2d964ce740f 155
fbdp1202 34:b2d964ce740f 156 printf("Init_2\r\n");
mbed_official 3:b1e385adea43 157
fbdp1202 34:b2d964ce740f 158 /* Ensure that it is the default instance of BLE */
fbdp1202 34:b2d964ce740f 159 if (ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
fbdp1202 34:b2d964ce740f 160 return;
fbdp1202 34:b2d964ce740f 161 }
fbdp1202 34:b2d964ce740f 162
fbdp1202 34:b2d964ce740f 163
fbdp1202 34:b2d964ce740f 164 ble.gap().onDisconnection(disconnectionCallback);
fbdp1202 34:b2d964ce740f 165 ble.gap().onConnection(connectionCallback);
fbdp1202 34:b2d964ce740f 166
mbed_official 3:b1e385adea43 167
fbdp1202 34:b2d964ce740f 168 ble.gattClient().onDataRead(triggerToggledWrite);
fbdp1202 34:b2d964ce740f 169 ble.gattClient().onDataWrite(triggerRead);
fbdp1202 34:b2d964ce740f 170
mbed_official 3:b1e385adea43 171
fbdp1202 34:b2d964ce740f 172 // scan interval: 400ms and scan window: 400ms.
fbdp1202 34:b2d964ce740f 173 // Every 400ms the device will scan for 400ms
fbdp1202 34:b2d964ce740f 174 // This means that the device will scan continuously.
fbdp1202 34:b2d964ce740f 175 ble.gap().setScanParams(400, 400);
fbdp1202 34:b2d964ce740f 176 ble.gap().startScan(advertisementCallback);
fbdp1202 34:b2d964ce740f 177 }
fbdp1202 34:b2d964ce740f 178
mbed_official 3:b1e385adea43 179
fbdp1202 34:b2d964ce740f 180 void scheduleBleEventsProcessing(BLE::OnEventsToProcessCallbackContext* context) {
fbdp1202 34:b2d964ce740f 181 BLE &ble = BLE::Instance();
fbdp1202 34:b2d964ce740f 182 eventQueue.call(Callback<void()>(&ble, &BLE::processEvents));
fbdp1202 34:b2d964ce740f 183 }
fbdp1202 34:b2d964ce740f 184
mbed_official 3:b1e385adea43 185
fbdp1202 34:b2d964ce740f 186 int main()
fbdp1202 34:b2d964ce740f 187 {
fbdp1202 34:b2d964ce740f 188 uart.baud(115200);
fbdp1202 34:b2d964ce740f 189 printf("main_1\r\n");
fbdp1202 34:b2d964ce740f 190 triggerLedCharacteristic = false;
fbdp1202 34:b2d964ce740f 191 eventQueue.call_every(1000, periodicCallback);
fbdp1202 34:b2d964ce740f 192
fbdp1202 34:b2d964ce740f 193 printf("main_2\r\n");
fbdp1202 34:b2d964ce740f 194 BLE &ble = BLE::Instance();
fbdp1202 34:b2d964ce740f 195 ble.onEventsToProcess(scheduleBleEventsProcessing);
fbdp1202 34:b2d964ce740f 196 ble.init(bleInitComplete);
fbdp1202 34:b2d964ce740f 197 printf("main_3\r\n");
mbed_official 3:b1e385adea43 198
fbdp1202 34:b2d964ce740f 199 eventQueue.dispatch_forever();
mbed_official 3:b1e385adea43 200
fbdp1202 34:b2d964ce740f 201 printf("main_4\r\n");
fbdp1202 34:b2d964ce740f 202
fbdp1202 34:b2d964ce740f 203 return 0;
fbdp1202 34:b2d964ce740f 204 }