BLE Observer example for nucleo platform
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
main.cpp@0:39b311448c9e, 2015-12-04 (annotated)
- Committer:
- vcoubard
- Date:
- Fri Dec 04 12:48:00 2015 +0000
- Revision:
- 0:39b311448c9e
- Child:
- 1:6ec60260b682
BLE_Observer example for NUCLEO platform
Who changed what in which revision?
User | Revision | Line number | New 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 | */ |
vcoubard | 0:39b311448c9e | 16 | |
vcoubard | 0:39b311448c9e | 17 | #include "mbed.h" |
vcoubard | 0:39b311448c9e | 18 | #include "BLE.h" |
vcoubard | 0:39b311448c9e | 19 | |
vcoubard | 0:39b311448c9e | 20 | BLE ble; |
vcoubard | 0:39b311448c9e | 21 | DigitalOut led1(LED1); |
vcoubard | 0:39b311448c9e | 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 | } |
vcoubard | 0:39b311448c9e | 27 | |
vcoubard | 0:39b311448c9e | 28 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
vcoubard | 0:39b311448c9e | 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); |
vcoubard | 0:39b311448c9e | 33 | #if DUMP_ADV_DATA |
vcoubard | 0:39b311448c9e | 34 | for (unsigned index = 0; index < params->advertisingDataLen; index++) { |
vcoubard | 0:39b311448c9e | 35 | printf("%02x ", params->advertisingData[index]); |
vcoubard | 0:39b311448c9e | 36 | } |
vcoubard | 0:39b311448c9e | 37 | printf("\r\n"); |
vcoubard | 0:39b311448c9e | 38 | #endif /* DUMP_ADV_DATA */ |
vcoubard | 0:39b311448c9e | 39 | } |
vcoubard | 0:39b311448c9e | 40 | |
vcoubard | 0:39b311448c9e | 41 | int main(void) |
vcoubard | 0:39b311448c9e | 42 | { |
vcoubard | 0:39b311448c9e | 43 | led1 = 1; |
vcoubard | 0:39b311448c9e | 44 | Ticker ticker; |
vcoubard | 0:39b311448c9e | 45 | ticker.attach(periodicCallback, 1); |
vcoubard | 0:39b311448c9e | 46 | |
vcoubard | 0:39b311448c9e | 47 | ble.init(); |
vcoubard | 0:39b311448c9e | 48 | |
vcoubard | 0:39b311448c9e | 49 | ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */); |
vcoubard | 0:39b311448c9e | 50 | ble.gap().startScan(advertisementCallback); |
vcoubard | 0:39b311448c9e | 51 | |
vcoubard | 0:39b311448c9e | 52 | while (true) { |
vcoubard | 0:39b311448c9e | 53 | ble.waitForEvent(); |
vcoubard | 0:39b311448c9e | 54 | } |
vcoubard | 0:39b311448c9e | 55 | } |