BLE Observer example for nucleo platform
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
main.cpp@5:346bcd62f251, 2016-11-14 (annotated)
- Committer:
- erikamorv
- Date:
- Mon Nov 14 15:29:43 2016 +0000
- Revision:
- 5:346bcd62f251
- Parent:
- 4:2aefa62bdce5
- Child:
- 6:1c20e1891e4b
2016.11.14
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 | */ |
erikamorv | 1:6ec60260b682 | 16 | |
vcoubard | 0:39b311448c9e | 17 | #include "mbed.h" |
vcoubard | 0:39b311448c9e | 18 | #include "BLE.h" |
erikamorv | 1:6ec60260b682 | 19 | |
vcoubard | 0:39b311448c9e | 20 | BLE ble; |
erikamorv | 2:b34944e028f9 | 21 | BLE ble_Scan; |
vcoubard | 0:39b311448c9e | 22 | DigitalOut led1(LED1); |
erikamorv | 2:b34944e028f9 | 23 | |
erikamorv | 2:b34944e028f9 | 24 | // Change your device name below |
erikamorv | 2:b34944e028f9 | 25 | const char DEVICE_NAME[] = "Nucleo_test"; |
erikamorv | 5:346bcd62f251 | 26 | |
erikamorv | 5:346bcd62f251 | 27 | #define UUID 0x00FF //Beacon UUID |
erikamorv | 5:346bcd62f251 | 28 | const uint16_t uuid16_list[] = {UUID}; |
erikamorv | 1:6ec60260b682 | 29 | |
erikamorv | 4:2aefa62bdce5 | 30 | // Control Beacon read |
erikamorv | 4:2aefa62bdce5 | 31 | static int beacon = 0; |
erikamorv | 5:346bcd62f251 | 32 | |
vcoubard | 0:39b311448c9e | 33 | void periodicCallback(void) |
vcoubard | 0:39b311448c9e | 34 | { |
vcoubard | 0:39b311448c9e | 35 | led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ |
vcoubard | 0:39b311448c9e | 36 | } |
erikamorv | 1:6ec60260b682 | 37 | |
erikamorv | 2:b34944e028f9 | 38 | void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { |
erikamorv | 2:b34944e028f9 | 39 | |
erikamorv | 2:b34944e028f9 | 40 | if (params->peerAddr[0] == 0xB2) { |
erikamorv | 2:b34944e028f9 | 41 | |
erikamorv | 2:b34944e028f9 | 42 | if (abs(params->rssi) <= 60) { |
erikamorv | 2:b34944e028f9 | 43 | |
erikamorv | 2:b34944e028f9 | 44 | printf("Beacon_1: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", |
vcoubard | 0:39b311448c9e | 45 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
erikamorv | 4:2aefa62bdce5 | 46 | params->rssi); |
erikamorv | 4:2aefa62bdce5 | 47 | |
erikamorv | 4:2aefa62bdce5 | 48 | beacon = 1; |
erikamorv | 2:b34944e028f9 | 49 | } |
erikamorv | 2:b34944e028f9 | 50 | } |
erikamorv | 2:b34944e028f9 | 51 | |
erikamorv | 2:b34944e028f9 | 52 | if (params->peerAddr[0] == 0xA8) { |
erikamorv | 2:b34944e028f9 | 53 | |
erikamorv | 5:346bcd62f251 | 54 | if (abs(params->rssi) <= 60) { |
erikamorv | 2:b34944e028f9 | 55 | printf("Beacon_2: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", |
erikamorv | 2:b34944e028f9 | 56 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
erikamorv | 4:2aefa62bdce5 | 57 | params->rssi); |
erikamorv | 4:2aefa62bdce5 | 58 | |
erikamorv | 4:2aefa62bdce5 | 59 | beacon = 2; |
erikamorv | 2:b34944e028f9 | 60 | } |
erikamorv | 2:b34944e028f9 | 61 | } |
erikamorv | 1:6ec60260b682 | 62 | |
erikamorv | 2:b34944e028f9 | 63 | if (params->peerAddr[0] == 0x46) { |
erikamorv | 2:b34944e028f9 | 64 | |
erikamorv | 2:b34944e028f9 | 65 | if (abs(params->rssi) <= 60) { |
erikamorv | 2:b34944e028f9 | 66 | printf("Beacon_3: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", |
erikamorv | 2:b34944e028f9 | 67 | params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], |
erikamorv | 2:b34944e028f9 | 68 | params->rssi); |
erikamorv | 4:2aefa62bdce5 | 69 | |
erikamorv | 4:2aefa62bdce5 | 70 | beacon = 3; |
vcoubard | 0:39b311448c9e | 71 | } |
erikamorv | 2:b34944e028f9 | 72 | } |
erikamorv | 1:6ec60260b682 | 73 | |
erikamorv | 2:b34944e028f9 | 74 | } |
erikamorv | 2:b34944e028f9 | 75 | |
erikamorv | 2:b34944e028f9 | 76 | /* Optional: Restart advertising when peer disconnects */ |
erikamorv | 2:b34944e028f9 | 77 | void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) |
erikamorv | 2:b34944e028f9 | 78 | { |
erikamorv | 2:b34944e028f9 | 79 | BLE::Instance().gap().startAdvertising(); |
erikamorv | 2:b34944e028f9 | 80 | } |
erikamorv | 1:6ec60260b682 | 81 | |
erikamorv | 2:b34944e028f9 | 82 | void bleInitComplete(BLE::InitializationCompleteCallbackContext *context) |
erikamorv | 2:b34944e028f9 | 83 | { |
erikamorv | 2:b34944e028f9 | 84 | // Set up the advertising flags. Note: not all combination of flags are valid |
erikamorv | 2:b34944e028f9 | 85 | // BREDR_NOT_SUPPORTED: Device does not support Basic Rate or Enchanced Data Rate, It is Low Energy only. |
erikamorv | 2:b34944e028f9 | 86 | // LE_GENERAL_DISCOVERABLE: Peripheral device is discoverable at any moment |
erikamorv | 2:b34944e028f9 | 87 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); |
erikamorv | 2:b34944e028f9 | 88 | |
erikamorv | 2:b34944e028f9 | 89 | // Put the device name in the advertising payload |
erikamorv | 2:b34944e028f9 | 90 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); |
erikamorv | 5:346bcd62f251 | 91 | |
erikamorv | 5:346bcd62f251 | 92 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list)); |
erikamorv | 5:346bcd62f251 | 93 | |
erikamorv | 2:b34944e028f9 | 94 | /* Optional: add callback for disconnection */ |
erikamorv | 2:b34944e028f9 | 95 | ble.gap().onDisconnection(disconnectionCallback); |
erikamorv | 1:6ec60260b682 | 96 | |
erikamorv | 2:b34944e028f9 | 97 | // The Service Data data type consists of a service UUID with the data associated with that service. |
erikamorv | 4:2aefa62bdce5 | 98 | // We will encode the number of beacon received |
erikamorv | 5:346bcd62f251 | 99 | uint8_t service_data[2]; |
erikamorv | 5:346bcd62f251 | 100 | service_data[0] = UUID & 0xff; |
erikamorv | 5:346bcd62f251 | 101 | service_data[1] = beacon & 0xff; |
erikamorv | 5:346bcd62f251 | 102 | ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)service_data, sizeof(service_data)); |
erikamorv | 2:b34944e028f9 | 103 | |
erikamorv | 2:b34944e028f9 | 104 | // It is not connectable as we are just boardcasting |
erikamorv | 2:b34944e028f9 | 105 | ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); |
erikamorv | 2:b34944e028f9 | 106 | |
erikamorv | 2:b34944e028f9 | 107 | //Send out the advertising payload every 1000ms |
erikamorv | 2:b34944e028f9 | 108 | ble.gap().setAdvertisingInterval(1000); |
vcoubard | 0:39b311448c9e | 109 | } |
erikamorv | 3:37a2f13b746d | 110 | |
erikamorv | 3:37a2f13b746d | 111 | void bleScanInitComplete(BLE::InitializationCompleteCallbackContext *context) { |
erikamorv | 3:37a2f13b746d | 112 | |
erikamorv | 5:346bcd62f251 | 113 | ble_Scan.gap().setScanParams(100 /* scan interval */, 50 /* scan window */, 1); |
erikamorv | 3:37a2f13b746d | 114 | |
erikamorv | 3:37a2f13b746d | 115 | } |
erikamorv | 3:37a2f13b746d | 116 | |
erikamorv | 1:6ec60260b682 | 117 | |
vcoubard | 0:39b311448c9e | 118 | int main(void) |
vcoubard | 0:39b311448c9e | 119 | { |
vcoubard | 0:39b311448c9e | 120 | led1 = 1; |
vcoubard | 0:39b311448c9e | 121 | Ticker ticker; |
vcoubard | 0:39b311448c9e | 122 | ticker.attach(periodicCallback, 1); |
erikamorv | 2:b34944e028f9 | 123 | |
erikamorv | 2:b34944e028f9 | 124 | BLE &ble = BLE::Instance(); |
erikamorv | 2:b34944e028f9 | 125 | BLE &ble_Scan = BLE::Instance(); |
erikamorv | 2:b34944e028f9 | 126 | |
erikamorv | 5:346bcd62f251 | 127 | int status = 1; |
erikamorv | 2:b34944e028f9 | 128 | |
erikamorv | 2:b34944e028f9 | 129 | /* Infinite loop waiting for BLE events */ |
erikamorv | 2:b34944e028f9 | 130 | while (true) { |
erikamorv | 5:346bcd62f251 | 131 | switch (status) { |
erikamorv | 3:37a2f13b746d | 132 | case(0): |
erikamorv | 3:37a2f13b746d | 133 | printf("Advertising stato 0\r\n"); |
erikamorv | 3:37a2f13b746d | 134 | ble.init(bleInitComplete); |
erikamorv | 3:37a2f13b746d | 135 | ble.gap().startAdvertising(); |
erikamorv | 5:346bcd62f251 | 136 | wait(1); |
erikamorv | 3:37a2f13b746d | 137 | ble.gap().stopAdvertising(); |
erikamorv | 5:346bcd62f251 | 138 | status = 1; |
erikamorv | 4:2aefa62bdce5 | 139 | NVIC_SystemReset(); |
erikamorv | 3:37a2f13b746d | 140 | break; |
erikamorv | 3:37a2f13b746d | 141 | case(1): |
erikamorv | 5:346bcd62f251 | 142 | printf("Scan stato 1\r\n"); |
erikamorv | 3:37a2f13b746d | 143 | ble_Scan.init(bleScanInitComplete); |
erikamorv | 3:37a2f13b746d | 144 | ble_Scan.gap().startScan(advertisementCallback); |
erikamorv | 5:346bcd62f251 | 145 | wait(1); |
erikamorv | 5:346bcd62f251 | 146 | status = 0; |
erikamorv | 5:346bcd62f251 | 147 | break; |
erikamorv | 3:37a2f13b746d | 148 | } |
erikamorv | 2:b34944e028f9 | 149 | |
erikamorv | 5:346bcd62f251 | 150 | /* Save power while waiting for callback events */ |
erikamorv | 5:346bcd62f251 | 151 | ble.waitForEvent(); |
erikamorv | 5:346bcd62f251 | 152 | ble_Scan.waitForEvent(); |
erikamorv | 5:346bcd62f251 | 153 | } |
erikamorv | 2:b34944e028f9 | 154 | } |