BLE Observer example for nucleo platform

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Committer:
erikamorv
Date:
Tue Nov 15 14:19:18 2016 +0000
Revision:
7:743f48384bdf
Parent:
6:1c20e1891e4b
Child:
8:0052fb9fe16c
2016.11.15 - ok for demo

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