BLE Observer example for nucleo platform
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Diff: main.cpp
- Revision:
- 8:0052fb9fe16c
- Parent:
- 7:743f48384bdf
diff -r 743f48384bdf -r 0052fb9fe16c main.cpp --- a/main.cpp Tue Nov 15 14:19:18 2016 +0000 +++ b/main.cpp Thu Jan 26 12:49:50 2017 +0000 @@ -17,13 +17,14 @@ #include "mbed.h" #include "BLE.h" -BLE ble; -BLE ble_Scan; -DigitalOut led1(LED1); +BLE ble; // Bluetooth Low Energy device: Advertising mode +BLE ble_Scan; // Bluetooth Low Energy device: Scan mode +DigitalOut led1(LED1); // Led on Nucleo Board // Change your device name below const char DEVICE_NAME[] = "Nucleo_test"; +#define T 50 // Threshold for Beacon RSSI #define UUID 0x00FF //Beacon UUID const uint16_t uuid16_list[] = {UUID}; @@ -36,35 +37,23 @@ } void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { - -if (params->peerAddr[0] == 0x8C) { - if (abs(params->rssi) <= 60) { - - printf("Beacon_1: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", - params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], - params->rssi); +if (params->peerAddr[0] == 0xB2) { // Control last bytes of Beacon Address -> Address Beacon 1 is 00 07 80 15 6E B2 - beacon = 1; - } -} - -if (params->peerAddr[0] == 0xB2) { + if (abs(params->rssi) <= T) { - if (abs(params->rssi) <= 60) { - - printf("Beacon_1: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", + printf("Beacon_1: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", // Display Beacon Address and relative RSSI params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], - params->rssi); + params->rssi); beacon = 1; } } -if (params->peerAddr[0] == 0xA8) { +if (params->peerAddr[0] == 0xA8) { // Control last bytes of Beacon Address -> Address Beacon 2 is EE 45 56 1C D0 A8 - if (abs(params->rssi) <= 60) { - printf("Beacon_2: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", + if (abs(params->rssi) <= T) { + printf("Beacon_2: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", // Display Beacon Address and relative RSSI params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], params->rssi); @@ -72,10 +61,10 @@ } } -if (params->peerAddr[0] == 0x46) { +if (params->peerAddr[0] == 0x46) { // Control last bytes of Beacon Address -> Address Beacon 3 is 00 07 80 15 79 46 - if (abs(params->rssi) <= 60) { - printf("Beacon_3: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", + if (abs(params->rssi) <= T) { + printf("Beacon_3: [%02x %02x %02x %02x %02x %02x] rssi %d\r\n", // Display Beacon Address and relative RSSI params->peerAddr[5], params->peerAddr[4], params->peerAddr[3], params->peerAddr[2], params->peerAddr[1], params->peerAddr[0], params->rssi); @@ -131,7 +120,7 @@ { led1 = 1; Ticker ticker; - ticker.attach(periodicCallback, 1); + ticker.attach(periodicCallback, 1); // Debug Periodic Callback BLE &ble = BLE::Instance(); BLE &ble_Scan = BLE::Instance(); @@ -142,16 +131,22 @@ while (true) { switch (status) { case(0): - printf("Advertising stato 0\r\n"); + + // Advertising mode + + printf("Advertising mode 0\r\n"); ble.init(bleInitComplete); ble.gap().startAdvertising(); wait(1); ble.gap().stopAdvertising(); status = 1; - NVIC_SystemReset(); + NVIC_SystemReset(); break; case(1): - printf("Scan stato 1\r\n"); + + // Scan mode + + printf("Scan mode 1\r\n"); ble_Scan.init(bleScanInitComplete); ble_Scan.gap().startScan(advertisementCallback); wait(1);