BLE Observer example for nucleo platform

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Committer:
erikamorv
Date:
Thu Jan 26 12:49:50 2017 +0000
Revision:
8:0052fb9fe16c
Parent:
7:743f48384bdf
2017.01.26

Who changed what in which revision?

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