Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: BLE_API mbed nRF51822
Fork of BLE_GAP_Example by
main.cpp@16:1e6fdee20db9, 2016-04-25 (annotated)
- Committer:
- mbedAustin
- Date:
- Mon Apr 25 19:59:12 2016 +0000
- Revision:
- 16:1e6fdee20db9
- Parent:
- 15:7e06fce6e4f8
- Child:
- 17:322f13ea91c1
Modified code to change name of beacon to be dependent on NAME define.;
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| andresag | 15:7e06fce6e4f8 | 1 | /* mbed Microcontroller Library |
| andresag | 15:7e06fce6e4f8 | 2 | * Copyright (c) 2006-2015 ARM Limited |
| andresag | 15:7e06fce6e4f8 | 3 | * |
| andresag | 15:7e06fce6e4f8 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| andresag | 15:7e06fce6e4f8 | 5 | * you may not use this file except in compliance with the License. |
| andresag | 15:7e06fce6e4f8 | 6 | * You may obtain a copy of the License at |
| andresag | 15:7e06fce6e4f8 | 7 | * |
| andresag | 15:7e06fce6e4f8 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| andresag | 15:7e06fce6e4f8 | 9 | * |
| andresag | 15:7e06fce6e4f8 | 10 | * Unless required by applicable law or agreed to in writing, software |
| andresag | 15:7e06fce6e4f8 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| andresag | 15:7e06fce6e4f8 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| andresag | 15:7e06fce6e4f8 | 13 | * See the License for the specific language governing permissions and |
| andresag | 15:7e06fce6e4f8 | 14 | * limitations under the License. |
| andresag | 15:7e06fce6e4f8 | 15 | */ |
| andresag | 15:7e06fce6e4f8 | 16 | |
| mbedAustin | 0:5375be4301ed | 17 | #include "mbed.h" |
| andresag | 14:1c15d473b42f | 18 | #include "ble/BLE.h" |
| andresag | 14:1c15d473b42f | 19 | |
| mbedAustin | 16:1e6fdee20db9 | 20 | #define NAME "spiffy" |
| mbedAustin | 16:1e6fdee20db9 | 21 | |
| andresag | 14:1c15d473b42f | 22 | /* Optional: Device Name, add for human read-ability */ |
| mbedAustin | 16:1e6fdee20db9 | 23 | const static char DEVICE_NAME[] = NAME; |
| andresag | 14:1c15d473b42f | 24 | |
| andresag | 14:1c15d473b42f | 25 | /* You have up to 26 bytes of advertising data to use. */ |
| andresag | 14:1c15d473b42f | 26 | const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05}; /* Example of hex data */ |
| andresag | 14:1c15d473b42f | 27 | //const static uint8_t AdvData[] = {"ChangeThisData"}; /* Example of character data */ |
| mbedAustin | 0:5375be4301ed | 28 | |
| andresag | 14:1c15d473b42f | 29 | /* Optional: Restart advertising when peer disconnects */ |
| andresag | 14:1c15d473b42f | 30 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
| andresag | 14:1c15d473b42f | 31 | { |
| andresag | 14:1c15d473b42f | 32 | BLE::Instance().gap().startAdvertising(); |
| andresag | 14:1c15d473b42f | 33 | } |
| andresag | 14:1c15d473b42f | 34 | /** |
| andresag | 14:1c15d473b42f | 35 | * This function is called when the ble initialization process has failed |
| andresag | 14:1c15d473b42f | 36 | */ |
| andresag | 14:1c15d473b42f | 37 | void onBleInitError(BLE &ble, ble_error_t error) |
| andresag | 14:1c15d473b42f | 38 | { |
| andresag | 14:1c15d473b42f | 39 | /* Avoid compiler warnings */ |
| andresag | 14:1c15d473b42f | 40 | (void) ble; |
| andresag | 14:1c15d473b42f | 41 | (void) error; |
| andresag | 14:1c15d473b42f | 42 | |
| andresag | 14:1c15d473b42f | 43 | /* Initialization error handling should go here */ |
| andresag | 14:1c15d473b42f | 44 | } |
| mbedAustin | 0:5375be4301ed | 45 | |
| andresag | 14:1c15d473b42f | 46 | /** |
| andresag | 14:1c15d473b42f | 47 | * Callback triggered when the ble initialization process has finished |
| andresag | 14:1c15d473b42f | 48 | */ |
| andresag | 14:1c15d473b42f | 49 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) |
| andresag | 14:1c15d473b42f | 50 | { |
| andresag | 14:1c15d473b42f | 51 | BLE& ble = params->ble; |
| andresag | 14:1c15d473b42f | 52 | ble_error_t error = params->error; |
| andresag | 14:1c15d473b42f | 53 | |
| andresag | 14:1c15d473b42f | 54 | if (error != BLE_ERROR_NONE) { |
| andresag | 14:1c15d473b42f | 55 | /* In case of error, forward the error handling to onBleInitError */ |
| andresag | 14:1c15d473b42f | 56 | onBleInitError(ble, error); |
| andresag | 14:1c15d473b42f | 57 | return; |
| andresag | 14:1c15d473b42f | 58 | } |
| mbedAustin | 5:fff16d283dcf | 59 | |
| andresag | 14:1c15d473b42f | 60 | /* Ensure that it is the default instance of BLE */ |
| andresag | 14:1c15d473b42f | 61 | if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { |
| andresag | 14:1c15d473b42f | 62 | return; |
| andresag | 14:1c15d473b42f | 63 | } |
| andresag | 14:1c15d473b42f | 64 | |
| andresag | 14:1c15d473b42f | 65 | /* Set device name characteristic data */ |
| mbedAustin | 16:1e6fdee20db9 | 66 | ble.gap().setDeviceName(reinterpret_cast<const uint8_t *>(&DEVICE_NAME)); |
| andresag | 14:1c15d473b42f | 67 | |
| andresag | 14:1c15d473b42f | 68 | /* Optional: add callback for disconnection */ |
| andresag | 14:1c15d473b42f | 69 | ble.gap().onDisconnection(disconnectionCallback); |
| mbedAustin | 1:0692bee84264 | 70 | |
| andresag | 14:1c15d473b42f | 71 | /* Sacrifice 3B of 31B to Advertising Flags */ |
| andresag | 14:1c15d473b42f | 72 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); |
| andresag | 14:1c15d473b42f | 73 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED); |
| andresag | 14:1c15d473b42f | 74 | |
| andresag | 14:1c15d473b42f | 75 | /* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */ |
| mbedAustin | 16:1e6fdee20db9 | 76 | //ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); |
| andresag | 14:1c15d473b42f | 77 | |
| andresag | 14:1c15d473b42f | 78 | /* Optional: Add name to device */ |
| mbedAustin | 16:1e6fdee20db9 | 79 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
| mbedAustin | 16:1e6fdee20db9 | 80 | ble.gap().accumulateScanResponse(GapAdvertisingData::COMPLETE_LOCAL_NAME, reinterpret_cast<const uint8_t *>(&DEVICE_NAME), sizeof(DEVICE_NAME)); |
| andresag | 14:1c15d473b42f | 81 | /* Set advertising interval. Longer interval == longer battery life */ |
| andresag | 14:1c15d473b42f | 82 | ble.gap().setAdvertisingInterval(100); /* 100ms */ |
| andresag | 14:1c15d473b42f | 83 | |
| andresag | 14:1c15d473b42f | 84 | /* Start advertising */ |
| mbedAustin | 16:1e6fdee20db9 | 85 | ble.gap().setTxPower(0); |
| andresag | 14:1c15d473b42f | 86 | ble.gap().startAdvertising(); |
| mbedAustin | 8:5442739198ec | 87 | } |
| mbedAustin | 8:5442739198ec | 88 | |
| mbedAustin | 1:0692bee84264 | 89 | int main(void) |
| mbedAustin | 0:5375be4301ed | 90 | { |
| andresag | 14:1c15d473b42f | 91 | BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE); |
| andresag | 14:1c15d473b42f | 92 | |
| andresag | 14:1c15d473b42f | 93 | /* Initialize BLE baselayer, always do this first! */ |
| andresag | 14:1c15d473b42f | 94 | ble.init(bleInitComplete); |
| mbedAustin | 1:0692bee84264 | 95 | |
| andresag | 14:1c15d473b42f | 96 | /* Infinite loop waiting for BLE events */ |
| andresag | 14:1c15d473b42f | 97 | while (true) { |
| andresag | 14:1c15d473b42f | 98 | /* Save power while waiting for callback events */ |
| mbedAustin | 16:1e6fdee20db9 | 99 | //ble.waitForEvent(); |
| mbedAustin | 1:0692bee84264 | 100 | } |
| mbedAustin | 1:0692bee84264 | 101 | } |
