Kris Scholte Lubberink
/
SSS_Ble
Ble for smart sOlutions
Diff: source/BleDeviceCentral.h
- Revision:
- 8:369b80cef5ae
- Parent:
- 7:9cda1b0f25ae
- Child:
- 9:92d861703f96
--- a/source/BleDeviceCentral.h Mon May 20 13:20:35 2019 +0200 +++ b/source/BleDeviceCentral.h Tue May 21 13:57:27 2019 +0200 @@ -7,8 +7,9 @@ #include <AdvertisingDataParser.h> #include "BleDevice.h" + + #include "list" - class BleDeviceCentral : public BleDevice { public: BleDeviceCentral(BLE &ble, events::EventQueue &event_queue) @@ -17,6 +18,7 @@ virtual void start() { + printf("[CENTRAL]\t Scan started\r\n"); ble::ScanParameters params; ble_error_t error = _ble.gap().setScanParameters(params); @@ -28,16 +30,16 @@ // /* start scanning, results will be handled by onAdvertisingReport */ error = _ble.gap().startScan(); - if (error) { print_error(error, "Error in Gap::startScan %d\r\n"); return; } + } virtual void stop(){ printf("[CENTRAL]\t Stopping \r\n"); - //TODO: Destroy the list with addresses - //FIXME: Out-of-memory error + + addresses.clear(); _ble.gap().stopScan(); if (_ble.hasInitialized()) { @@ -45,12 +47,11 @@ } } -private: - /* Gap::EventHandler */ +public: + /** Look at scan payload to find a peer device and connect to it */ - virtual void onAdvertisingReport(const ble::AdvertisingReportEvent &event) - { + virtual void onAdvertisingReport(const ble::AdvertisingReportEvent &event) { /* don't bother with analysing scan result if we're already connecting */ if (_is_connecting) { return; @@ -64,6 +65,18 @@ //TODO: Start service discovery. ble::AdvertisingDataParser adv_data(event.getPayload()); while (adv_data.hasNext()) { + if (addresses.size() > 0) { + found = (std::find(addresses.begin(), addresses.end(), actualdata) != addresses.end()); + } + + if (!found) { + //TODO: Clear the list to prevent memory overflow + addresses.push_back(actualdata); + printf("\r\n Amount devices: %d \r\n", addresses.size()); + return; + } else { +// TODO: Increment internal counter + } // printf("\n\t\t Has next"); ble::AdvertisingDataParser::element_t field = adv_data.next(); @@ -73,11 +86,11 @@ /*if (field.type == ble::adv_data_type_t::COMPLETE_LOCAL_NAME && field.value.size() == strlen("SSS BLE") && (memcmp(field.value.data(), "SSS BLE", field.value.size()) == 0)) {*/ - if (field.type == ble::adv_data_type_t::COMPLETE_LOCAL_NAME){ + if (field.type == ble::adv_data_type_t::COMPLETE_LOCAL_NAME) { if (field.type == ble::adv_data_type_t::COMPLETE_LOCAL_NAME && field.value.size() == strlen("SSS BLE") && (memcmp(field.value.data(), "SSS BLE", field.value.size()) == 0)) { - printf("Found another wearable, let's try connecting to him."); + printf("[PERIPHERAL]\t Found another wearable, let's try connecting to him.\r\n"); ble_error_t error = _ble.gap().stopScan(); @@ -86,20 +99,24 @@ print_error(error, "Error caused by Gap::stopScan"); return; } + printf("[PERIPHERAL]\t Stopped the scan.\r\n"); const ble::ConnectionParameters connection_params; + printf("[PERIPHERAL]\t Trying to connect.\r\n"); error = _ble.gap().connect( event.getPeerAddressType(), event.getPeerAddress(), connection_params ); + printf("[PERIPHERAL]\t Trying to connect..\r\n"); if (error) { _ble.gap().startScan(); return; } + printf("[PERIPHERAL]\t Trying to connect...\r\n"); } @@ -121,23 +138,7 @@ } } - if(addresses.size() > 0){ - found = (std::find(addresses.begin(), addresses.end(),actualdata ) != addresses.end()); - } - - if (!found) { - //TODO: Clear the list to prevent memory overflow - addresses.push_back(actualdata); - printf("\r\n Amount devices: %d \r\n", addresses.size()); - -// printf("%02x:%02x:%02x:%02x:%02x:%02x\r\n", actualdata[5], actualdata[4], actualdata[3], actualdata[2], actualdata[1], actualdata[0]); - return; - } else { -// TODO: Increment internal counter - } } - - };