BLE Observer example for nucleo platform

Dependencies:   BLE_API X_NUCLEO_IDB0XA1 mbed

Committer:
erikamorv
Date:
Mon Nov 14 10:20:45 2016 +0000
Revision:
4:2aefa62bdce5
Parent:
3:37a2f13b746d
Child:
5:346bcd62f251
2016.11.14 - Scan and Advertising OK

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