Prueba LoRa

Dependencies:   BLE_API SX1276Lib mbed nRF51822

Fork of BLE_Observer by Bluetooth Low Energy

Committer:
andresag
Date:
Tue Jan 12 11:00:02 2016 +0000
Revision:
7:88f50499af9a
Parent:
5:103717ce54e5
Child:
8:3b30027c7e8f
Update example to latest BLE API.

Who changed what in which revision?

UserRevisionLine numberNew 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
rgrover1 0:332983584a9c 30 printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n",
rgrover1 3:50a7d47912b2 31 params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0],
rgrover1 3:50a7d47912b2 32 params->rssi, params->isScanResponse, params->type);
rgrover1 0:332983584a9c 33 #if DUMP_ADV_DATA
rgrover1 3:50a7d47912b2 34 for (unsigned index = 0; index < params->advertisingDataLen; index++) {
rgrover1 3:50a7d47912b2 35 printf("%02x ", params->advertisingData[index]);
rgrover1 0:332983584a9c 36 }
rgrover1 0:332983584a9c 37 printf("\r\n");
rgrover1 0:332983584a9c 38 #endif /* DUMP_ADV_DATA */
rgrover1 0:332983584a9c 39 }
rgrover1 0:332983584a9c 40
andresag 7:88f50499af9a 41 /**
andresag 7:88f50499af9a 42 * This function is called when the ble initialization process has failed
andresag 7:88f50499af9a 43 */
andresag 7:88f50499af9a 44 void onBleInitError(BLE &ble, ble_error_t error)
andresag 7:88f50499af9a 45 {
andresag 7:88f50499af9a 46 /* Initialization error handling should go here */
andresag 7:88f50499af9a 47 }
andresag 7:88f50499af9a 48
andresag 7:88f50499af9a 49 /**
andresag 7:88f50499af9a 50 * Callback triggered when the ble initialization process has finished
andresag 7:88f50499af9a 51 */
andresag 7:88f50499af9a 52 void bleInitComplete(BLE::InitializationCompleteCallbackContext *params)
andresag 7:88f50499af9a 53 {
andresag 7:88f50499af9a 54 BLE& ble = params->ble;
andresag 7:88f50499af9a 55 ble_error_t error = params->error;
andresag 7:88f50499af9a 56
andresag 7:88f50499af9a 57 if (error != BLE_ERROR_NONE) {
andresag 7:88f50499af9a 58 /* In case of error, forward the error handling to onBleInitError */
andresag 7:88f50499af9a 59 onBleInitError(ble, error);
andresag 7:88f50499af9a 60 return;
andresag 7:88f50499af9a 61 }
andresag 7:88f50499af9a 62
andresag 7:88f50499af9a 63 /* Ensure that it is the default instance of BLE */
andresag 7:88f50499af9a 64 if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) {
andresag 7:88f50499af9a 65 return;
andresag 7:88f50499af9a 66 }
andresag 7:88f50499af9a 67
andresag 7:88f50499af9a 68 ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */);
andresag 7:88f50499af9a 69 ble.gap().startScan(advertisementCallback);
andresag 7:88f50499af9a 70 }
andresag 7:88f50499af9a 71
rgrover1 0:332983584a9c 72 int main(void)
rgrover1 0:332983584a9c 73 {
rgrover1 0:332983584a9c 74 ticker.attach(periodicCallback, 1);
rgrover1 0:332983584a9c 75
andresag 7:88f50499af9a 76 BLE &ble = BLE::Instance();
andresag 7:88f50499af9a 77 ble.init(bleInitComplete);
rgrover1 0:332983584a9c 78
rgrover1 0:332983584a9c 79 while (true) {
rgrover1 0:332983584a9c 80 ble.waitForEvent();
rgrover1 0:332983584a9c 81 }
rgrover1 0:332983584a9c 82 }