sdasd
Dependencies: BLE_API mbed nRF51822
Fork of MEDRON_SNIFFER_ABCD-KIMLIKLI-OLANLARI-TARAMA-YAPAR_30062016 by
main.cpp@0:332983584a9c, 2015-05-12 (annotated)
- Committer:
- rgrover1
- Date:
- Tue May 12 10:28:21 2015 +0000
- Revision:
- 0:332983584a9c
- Child:
- 3:50a7d47912b2
initial attempt at a demo for a BLE observer. Uses the new scanning APIs.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rgrover1 | 0:332983584a9c | 1 | /* mbed Microcontroller Library |
rgrover1 | 0:332983584a9c | 2 | * Copyright (c) 2006-2015 ARM Limited |
rgrover1 | 0:332983584a9c | 3 | * |
rgrover1 | 0:332983584a9c | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
rgrover1 | 0:332983584a9c | 5 | * you may not use this file except in compliance with the License. |
rgrover1 | 0:332983584a9c | 6 | * You may obtain a copy of the License at |
rgrover1 | 0:332983584a9c | 7 | * |
rgrover1 | 0:332983584a9c | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
rgrover1 | 0:332983584a9c | 9 | * |
rgrover1 | 0:332983584a9c | 10 | * Unless required by applicable law or agreed to in writing, software |
rgrover1 | 0:332983584a9c | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
rgrover1 | 0:332983584a9c | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
rgrover1 | 0:332983584a9c | 13 | * See the License for the specific language governing permissions and |
rgrover1 | 0:332983584a9c | 14 | * limitations under the License. |
rgrover1 | 0:332983584a9c | 15 | */ |
rgrover1 | 0:332983584a9c | 16 | |
rgrover1 | 0:332983584a9c | 17 | #include "mbed.h" |
rgrover1 | 0:332983584a9c | 18 | #include "BLEDevice.h" |
rgrover1 | 0:332983584a9c | 19 | |
rgrover1 | 0:332983584a9c | 20 | BLEDevice ble; |
rgrover1 | 0:332983584a9c | 21 | DigitalOut led1(LED1); |
rgrover1 | 0:332983584a9c | 22 | |
rgrover1 | 0:332983584a9c | 23 | void periodicCallback(void) |
rgrover1 | 0:332983584a9c | 24 | { |
rgrover1 | 0:332983584a9c | 25 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
rgrover1 | 0:332983584a9c | 26 | } |
rgrover1 | 0:332983584a9c | 27 | |
rgrover1 | 0:332983584a9c | 28 | void advertisementCallback(const Gap::address_t peerAddr, |
rgrover1 | 0:332983584a9c | 29 | int8_t rssi, |
rgrover1 | 0:332983584a9c | 30 | bool isScanResponse, |
rgrover1 | 0:332983584a9c | 31 | Gap::AdvertisementType_t type, |
rgrover1 | 0:332983584a9c | 32 | uint8_t advertisingDataLen, |
rgrover1 | 0:332983584a9c | 33 | const uint8_t *advertisingData) { |
rgrover1 | 0:332983584a9c | 34 | printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n", |
rgrover1 | 0:332983584a9c | 35 | peerAddr[5], peerAddr[4], peerAddr[3], peerAddr[2], peerAddr[1], peerAddr[0], rssi, isScanResponse, type); |
rgrover1 | 0:332983584a9c | 36 | #if DUMP_ADV_DATA |
rgrover1 | 0:332983584a9c | 37 | unsigned index = 0; |
rgrover1 | 0:332983584a9c | 38 | for (; index < advertisingDataLen; index++) { |
rgrover1 | 0:332983584a9c | 39 | printf("%02x ", advertisingData[index]); |
rgrover1 | 0:332983584a9c | 40 | } |
rgrover1 | 0:332983584a9c | 41 | printf("\r\n"); |
rgrover1 | 0:332983584a9c | 42 | #endif /* DUMP_ADV_DATA */ |
rgrover1 | 0:332983584a9c | 43 | } |
rgrover1 | 0:332983584a9c | 44 | |
rgrover1 | 0:332983584a9c | 45 | int main(void) |
rgrover1 | 0:332983584a9c | 46 | { |
rgrover1 | 0:332983584a9c | 47 | led1 = 1; |
rgrover1 | 0:332983584a9c | 48 | Ticker ticker; |
rgrover1 | 0:332983584a9c | 49 | ticker.attach(periodicCallback, 1); |
rgrover1 | 0:332983584a9c | 50 | |
rgrover1 | 0:332983584a9c | 51 | ble.init(); |
rgrover1 | 0:332983584a9c | 52 | |
rgrover1 | 0:332983584a9c | 53 | ble.setScanParams(500 /* scan interval */, 200 /* scan window */); |
rgrover1 | 0:332983584a9c | 54 | ble.startScan(advertisementCallback); |
rgrover1 | 0:332983584a9c | 55 | |
rgrover1 | 0:332983584a9c | 56 | while (true) { |
rgrover1 | 0:332983584a9c | 57 | ble.waitForEvent(); |
rgrover1 | 0:332983584a9c | 58 | } |
rgrover1 | 0:332983584a9c | 59 | } |