Locator mobile firmware
Dependencies: BLE_API mbed nRF51822
Fork of BLE_Observer by
main.cpp@8:1b030068f28c, 2017-04-30 (annotated)
- Committer:
- PostaL
- Date:
- Sun Apr 30 08:41:06 2017 +0000
- Revision:
- 8:1b030068f28c
- Parent:
- 7:88f50499af9a
- Child:
- 9:3e967b414bd5
init
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" |
andresag | 7:88f50499af9a | 18 | #include "ble/BLE.h" |
rgrover1 | 0:332983584a9c | 19 | |
andresag | 7:88f50499af9a | 20 | DigitalOut led1(LED1, 1); |
andresag | 7:88f50499af9a | 21 | Ticker ticker; |
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 | 3:50a7d47912b2 | 28 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
rgrover1 | 3:50a7d47912b2 | 29 | |
PostaL | 8:1b030068f28c | 30 | if (params->type == 3) { |
PostaL | 8:1b030068f28c | 31 | printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n", |
PostaL | 8:1b030068f28c | 32 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
PostaL | 8:1b030068f28c | 33 | params->rssi, params->isScanResponse, params->type); |
PostaL | 8:1b030068f28c | 34 | |
PostaL | 8:1b030068f28c | 35 | for (unsigned index = 0; index < params->advertisingDataLen; index++) { |
PostaL | 8:1b030068f28c | 36 | printf("%02x ", params->advertisingData[index]); |
PostaL | 8:1b030068f28c | 37 | } |
PostaL | 8:1b030068f28c | 38 | printf("\r\n"); |
rgrover1 | 0:332983584a9c | 39 | } |
rgrover1 | 0:332983584a9c | 40 | } |
rgrover1 | 0:332983584a9c | 41 | |
andresag | 7:88f50499af9a | 42 | /** |
andresag | 7:88f50499af9a | 43 | * This function is called when the ble initialization process has failed |
andresag | 7:88f50499af9a | 44 | */ |
andresag | 7:88f50499af9a | 45 | void onBleInitError(BLE &ble, ble_error_t error) |
andresag | 7:88f50499af9a | 46 | { |
andresag | 7:88f50499af9a | 47 | /* Initialization error handling should go here */ |
andresag | 7:88f50499af9a | 48 | } |
andresag | 7:88f50499af9a | 49 | |
andresag | 7:88f50499af9a | 50 | /** |
andresag | 7:88f50499af9a | 51 | * Callback triggered when the ble initialization process has finished |
andresag | 7:88f50499af9a | 52 | */ |
andresag | 7:88f50499af9a | 53 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
andresag | 7:88f50499af9a | 54 | { |
andresag | 7:88f50499af9a | 55 | BLE& ble = params->ble; |
andresag | 7:88f50499af9a | 56 | ble_error_t error = params->error; |
andresag | 7:88f50499af9a | 57 | |
andresag | 7:88f50499af9a | 58 | if (error != BLE_ERROR_NONE) { |
andresag | 7:88f50499af9a | 59 | /* In case of error, forward the error handling to onBleInitError */ |
andresag | 7:88f50499af9a | 60 | onBleInitError(ble, error); |
andresag | 7:88f50499af9a | 61 | return; |
andresag | 7:88f50499af9a | 62 | } |
andresag | 7:88f50499af9a | 63 | |
andresag | 7:88f50499af9a | 64 | /* Ensure that it is the default instance of BLE */ |
andresag | 7:88f50499af9a | 65 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
andresag | 7:88f50499af9a | 66 | return; |
andresag | 7:88f50499af9a | 67 | } |
andresag | 7:88f50499af9a | 68 | |
andresag | 7:88f50499af9a | 69 | ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */); |
andresag | 7:88f50499af9a | 70 | ble.gap().startScan(advertisementCallback); |
andresag | 7:88f50499af9a | 71 | } |
andresag | 7:88f50499af9a | 72 | |
rgrover1 | 0:332983584a9c | 73 | int main(void) |
rgrover1 | 0:332983584a9c | 74 | { |
rgrover1 | 0:332983584a9c | 75 | ticker.attach(periodicCallback, 1); |
rgrover1 | 0:332983584a9c | 76 | |
andresag | 7:88f50499af9a | 77 | BLE &ble = BLE::Instance(); |
andresag | 7:88f50499af9a | 78 | ble.init(bleInitComplete); |
rgrover1 | 0:332983584a9c | 79 | |
rgrover1 | 0:332983584a9c | 80 | while (true) { |
rgrover1 | 0:332983584a9c | 81 | ble.waitForEvent(); |
rgrover1 | 0:332983584a9c | 82 | } |
rgrover1 | 0:332983584a9c | 83 | } |