BLE Observer example for nucleo platform

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Committer:
erikamorv
Date:
Wed Nov 09 12:49:37 2016 +0000
Revision:
1:6ec60260b682
Parent:
0:39b311448c9e
Child:
2:b34944e028f9
2016.11.09

Who changed what in which revision?

UserRevisionLine numberNew contents of line
vcoubard 0:39b311448c9e 1 /* mbed Microcontroller Library
vcoubard 0:39b311448c9e 2 * Copyright (c) 2006-2015 ARM Limited
vcoubard 0:39b311448c9e 3 *
vcoubard 0:39b311448c9e 4 * Licensed under the Apache License, Version 2.0 (the "License");
vcoubard 0:39b311448c9e 5 * you may not use this file except in compliance with the License.
vcoubard 0:39b311448c9e 6 * You may obtain a copy of the License at
vcoubard 0:39b311448c9e 7 *
vcoubard 0:39b311448c9e 8 * http://www.apache.org/licenses/LICENSE-2.0
vcoubard 0:39b311448c9e 9 *
vcoubard 0:39b311448c9e 10 * Unless required by applicable law or agreed to in writing, software
vcoubard 0:39b311448c9e 11 * distributed under the License is distributed on an "AS IS" BASIS,
vcoubard 0:39b311448c9e 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
vcoubard 0:39b311448c9e 13 * See the License for the specific language governing permissions and
vcoubard 0:39b311448c9e 14 * limitations under the License.
vcoubard 0:39b311448c9e 15 */
erikamorv 1:6ec60260b682 16
vcoubard 0:39b311448c9e 17 #include "mbed.h"
vcoubard 0:39b311448c9e 18 #include "BLE.h"
erikamorv 1:6ec60260b682 19
vcoubard 0:39b311448c9e 20 BLE ble;
vcoubard 0:39b311448c9e 21 DigitalOut led1(LED1);
erikamorv 1:6ec60260b682 22
vcoubard 0:39b311448c9e 23 void periodicCallback(void)
vcoubard 0:39b311448c9e 24 {
vcoubard 0:39b311448c9e 25 led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */
vcoubard 0:39b311448c9e 26 }
erikamorv 1:6ec60260b682 27
vcoubard 0:39b311448c9e 28 void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) {
erikamorv 1:6ec60260b682 29
vcoubard 0:39b311448c9e 30 printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n",
vcoubard 0:39b311448c9e 31 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
vcoubard 0:39b311448c9e 32 params->rssi, params->isScanResponse, params->type);
erikamorv 1:6ec60260b682 33
erikamorv 1:6ec60260b682 34 unsigned index;
erikamorv 1:6ec60260b682 35
vcoubard 0:39b311448c9e 36 #if DUMP_ADV_DATA
erikamorv 1:6ec60260b682 37
erikamorv 1:6ec60260b682 38 for (index = 0; index < params->advertisingDataLen; index++) {
vcoubard 0:39b311448c9e 39 printf("%02x ", params->advertisingData[index]);
vcoubard 0:39b311448c9e 40 }
vcoubard 0:39b311448c9e 41 printf("\r\n");
erikamorv 1:6ec60260b682 42
vcoubard 0:39b311448c9e 43 #endif /* DUMP_ADV_DATA */
erikamorv 1:6ec60260b682 44
erikamorv 1:6ec60260b682 45 // RSSI info
erikamorv 1:6ec60260b682 46
erikamorv 1:6ec60260b682 47 printf("\nRSSI = %d", params -> rssi);
erikamorv 1:6ec60260b682 48
erikamorv 1:6ec60260b682 49
erikamorv 1:6ec60260b682 50 // TxPower info
erikamorv 1:6ec60260b682 51 uint16_t txpower;
erikamorv 1:6ec60260b682 52 txpower = params -> advertisingData[index-1];
erikamorv 1:6ec60260b682 53 printf(" TxPower = %u \n\r", txpower);
erikamorv 1:6ec60260b682 54
erikamorv 1:6ec60260b682 55 // Elaborate distance
erikamorv 1:6ec60260b682 56
vcoubard 0:39b311448c9e 57 }
erikamorv 1:6ec60260b682 58
vcoubard 0:39b311448c9e 59 int main(void)
vcoubard 0:39b311448c9e 60 {
vcoubard 0:39b311448c9e 61 led1 = 1;
vcoubard 0:39b311448c9e 62 Ticker ticker;
vcoubard 0:39b311448c9e 63 ticker.attach(periodicCallback, 1);
erikamorv 1:6ec60260b682 64
vcoubard 0:39b311448c9e 65 ble.init();
erikamorv 1:6ec60260b682 66
vcoubard 0:39b311448c9e 67 ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */);
vcoubard 0:39b311448c9e 68 ble.gap().startScan(advertisementCallback);
erikamorv 1:6ec60260b682 69
vcoubard 0:39b311448c9e 70 while (true) {
vcoubard 0:39b311448c9e 71 ble.waitForEvent();
vcoubard 0:39b311448c9e 72 }
erikamorv 1:6ec60260b682 73 }