Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
source/BleDeviceCentral.h
- Committer:
- kris@kris-X682X
- Date:
- 2019-05-21
- Revision:
- 8:369b80cef5ae
- Parent:
- 7:9cda1b0f25ae
- Child:
- 9:92d861703f96
File content as of revision 8:369b80cef5ae:
//
// Created by kris on 20-4-19.
//
#ifndef SSS_BLE_BLEDEVICECENTRAL_H
#define SSS_BLE_BLEDEVICECENTRAL_H
#include <AdvertisingDataParser.h>
#include "BleDevice.h"
#include "list"
class BleDeviceCentral : public BleDevice {
public:
BleDeviceCentral(BLE &ble, events::EventQueue &event_queue)
: BleDevice(ble, event_queue) {}
std::list<ble::address_t::byte_array_t> addresses ;
virtual void start()
{
printf("[CENTRAL]\t Scan started\r\n");
ble::ScanParameters params;
ble_error_t error = _ble.gap().setScanParameters(params);
if (error) {
print_error(error, "Error in Gap::startScan %d\r\n");
return;
}
//
// /* 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");
addresses.clear();
_ble.gap().stopScan();
if (_ble.hasInitialized()) {
_ble.shutdown();
}
}
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;
}
// printf("found someone ");
/* parse the advertising payload, looking for a discoverable device */
//TODO: Hier checken ofhet een andere SSS wearable is. Zo niet, gewoon tellen.
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("[PERIPHERAL]\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("[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");
}
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;
}
}
}
};
#endif //SSS_BLE_BLEDEVICECENTRAL_H