Kris Scholte Lubberink
/
SSS_Ble
Ble for smart sOlutions
Diff: source/BleDeviceCentral.h
- Revision:
- 10:d845189d146e
- Parent:
- 9:92d861703f96
- Child:
- 11:d6ed1437c2ee
--- a/source/BleDeviceCentral.h Tue Jun 11 11:16:55 2019 +0200 +++ b/source/BleDeviceCentral.h Fri Jun 14 10:24:57 2019 +0200 @@ -10,6 +10,8 @@ #include "list" +#include "MyStripSingleton.h" + class BleDeviceCentral : public BleDevice { public: BleDeviceCentral(BLE &ble, events::EventQueue &event_queue) @@ -21,6 +23,7 @@ virtual void start() { printf("[CENTRAL]\t Scan started\r\n"); + MyStripSingleton::getInstance()->resetCrawl(); ble::ScanParameters params; ble_error_t error = _ble.gap().setScanParameters(params); @@ -40,7 +43,7 @@ } virtual void stop(){ printf("[CENTRAL]\t Stopping \r\n"); - + MyStripSingleton::getInstance()->resetColor(); addresses.clear(); _ble.gap().stopScan(); @@ -50,96 +53,8 @@ } public: - - - /** Look at scan payload to find a peer device and connect to it */ - virtual void onAdvertisingReport(const ble::AdvertisingReportEvent &event) { - /* don't bother with analysing scan result if we're already connecting */ - if (_is_connecting) { - return; - } - bool found = false; - ble::address_t::byte_array_t actualdata = event.getPeerAddress().data(); - - //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(); - - /* connect to a discoverable device */ - //Deze if-statement kijkt naar de naam en MEMCMP vergelijkt de advertising-naam met de target-naam (sssble) - //Als het SSS BLE is, moet ik dus de services gaan ontdekken en de interest-service uitlezen. Dit is altijd een array van 5 bytes, allemaal - /*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 && - field.value.size() == strlen("SSS BLE") && - (memcmp(field.value.data(), "SSS BLE", field.value.size()) == 0)) { - printf("[CENTRAL]\t Found another wearable, let's try connecting to him.\r\n"); - - - ble_error_t error = _ble.gap().stopScan(); - - if (error) { - print_error(error, "Error caused by Gap::stopScan"); - return; - } - printf("[CENTRAL]\t Stopped the scan.\r\n"); - - const ble::ConnectionParameters connection_params; - - printf("[CENTRAL]\t Trying to connect.\r\n"); - - error = _ble.gap().connect( - event.getPeerAddressType(), - event.getPeerAddress(), - connection_params - ); - printf("[CENTRAL]\t Trying to connect..\r\n"); - - if (error) { - _ble.gap().startScan(); - return; - } - printf("[CENTRAL]\t Looks like I'm connected...\r\n"); - //TODO: Break the event queue, but keep doing what im doing - _event_queue.dispatch_forever(); - } - - char test[field.value.size()]; - memcpy(test, field.value.data(), field.value.size()); - - printf("\n\t\t Adv from: "); - printf("%s", test); - printf(" rssi: %d, scan response: %u, connectable: %u\r\n", - event.getRssi(), event.getType().scan_response(), event.getType().connectable()); - - - /* we may have already scan events waiting - * to be processed so we need to remember - * that we are already connecting and ignore them */ -// _is_connecting = true; - - return; - } - } - - } + virtual void onAdvertisingReport(const ble::AdvertisingReportEvent &event); };