BLE Observer example for nucleo platform
Dependencies: BLE_API X_NUCLEO_IDB0XA1 mbed
Diff: main.cpp
- Revision:
- 2:b34944e028f9
- Parent:
- 1:6ec60260b682
- Child:
- 3:37a2f13b746d
diff -r 6ec60260b682 -r b34944e028f9 main.cpp --- a/main.cpp Wed Nov 09 12:49:37 2016 +0000 +++ b/main.cpp Fri Nov 11 13:31:05 2016 +0000 @@ -18,42 +18,89 @@ #include "BLE.h" BLE ble; +BLE ble_Scan; DigitalOut led1(LED1); + +// Change your device name below +const char DEVICE_NAME[] = "Nucleo_test"; void periodicCallback(void) { led1 = !led1; /* Do blinky on LED1 while we're waiting for BLE events */ } -void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { - - printf("Adv peerAddr: [%02x %02x %02x %02x %02x %02x] rssi %d, ScanResp: %u, AdvType: %u\r\n", +void advertisementCallback(const Gap::AdvertisementCallbackParams_t *params) { + +if (params->peerAddr[0] == 0xB2) { + + 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, params->isScanResponse, params->type); + params->rssi); + } +} + +if (params->peerAddr[0] == 0xA8) { + + if (abs(params->rssi) <= 60) { // Beacon 2 necessita di una soglia di vicinanza più alta + printf("Beacon_2: [%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); + } +} - unsigned index; - -#if DUMP_ADV_DATA - - for (index = 0; index < params->advertisingDataLen; index++) { - printf("%02x ", params->advertisingData[index]); +if (params->peerAddr[0] == 0x46) { + + if (abs(params->rssi) <= 60) { + printf("Beacon_3: [%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); } - printf("\r\n"); +} -#endif /* DUMP_ADV_DATA */ +} + +/* Optional: Restart advertising when peer disconnects */ +void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) +{ + BLE::Instance().gap().startAdvertising(); +} - // RSSI info - - printf("\nRSSI = %d", params -> rssi); - + void bleInitComplete(BLE::InitializationCompleteCallbackContext *context) +{ + // Set up the advertising flags. Note: not all combination of flags are valid + // BREDR_NOT_SUPPORTED: Device does not support Basic Rate or Enchanced Data Rate, It is Low Energy only. + // LE_GENERAL_DISCOVERABLE: Peripheral device is discoverable at any moment + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE); + + // Put the device name in the advertising payload + ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME)); + + /* Optional: add callback for disconnection */ + ble.gap().onDisconnection(disconnectionCallback); - // TxPower info - uint16_t txpower; - txpower = params -> advertisingData[index-1]; - printf(" TxPower = %u \n\r", txpower); - - // Elaborate distance - + // The Service Data data type consists of a service UUID with the data associated with that service. + // We will encode the number of button clicks in the Service Data field + // First two bytes of SERVICE_DATA field should contain the UUID of the service + //uint8_t service_data[3]; + //service_data[0] = MajorID & 0xff; + //service_data[1] = MinorID & 0xff; + //service_data[2] = TXPOWER & 0xff; + //err = ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, (uint8_t *)service_data, sizeof(service_data)); + //if (err != BLE_ERROR_NONE) { + // print_error(err, "Setting service data failed"); + // return; + //} + + // It is not connectable as we are just boardcasting + ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_NON_CONNECTABLE_UNDIRECTED); + + //Send out the advertising payload every 1000ms + ble.gap().setAdvertisingInterval(1000); + + //ble.gap().startAdvertising(); + } int main(void) @@ -61,13 +108,48 @@ led1 = 1; Ticker ticker; ticker.attach(periodicCallback, 1); - - ble.init(); + + BLE &ble = BLE::Instance(); + BLE &ble_Scan = BLE::Instance(); - ble.gap().setScanParams(500 /* scan interval */, 200 /* scan window */); - ble.gap().startScan(advertisementCallback); - - while (true) { - ble.waitForEvent(); - } -} \ No newline at end of file + ble.init(bleInitComplete); + //ble_Scan.init(); + + //ble.gap().setScanParams(100 /* scan interval */, 50 /* scan window */, 1); + //ble.gap().startScan(advertisementCallback); + +int stato = 0; + + /* Infinite loop waiting for BLE events */ + while (true) { + switch (stato) { + case(0): + //BLE &ble = BLE::Instance(); + printf("Advertising stato 0\r\n"); + ble.gap().stopScan(); + //ble.init(bleInitComplete); + ble.gap().startAdvertising(); + wait(10); + //ble.gap().stopAdvertising(); + //wait(1); + stato = 1; + break; + case(1): + //BLE &ble_Scan = BLE::Instance(); + //ble_Scan.init(); + printf("Scan stato 1\r\n"); + ble.gap().stopAdvertising(); + ble_Scan.gap().setScanParams(100 /* scan interval */, 50 /* scan window */, 1); + ble_Scan.gap().startScan(advertisementCallback); + wait(1); + ble_Scan.gap().stopScan(); + //wait(1); + stato = 0; + break; + } + + /* Save power while waiting for callback events */ + ble.waitForEvent(); + + } +}