Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

BLE: Scanned/Connected/Disconnected/Data Received callback in C++ style #2224

Merged
merged 11 commits into from
Nov 20, 2020

Conversation

XuGuohui
Copy link
Member

@XuGuohui XuGuohui commented Oct 19, 2020

This PR is targeting to the feature/ble_scan_filter/ch37224 branch.

Problem

The existing wiring BLE APIs only support c style callback.

Solution

Using function wrapper to support c++ style callback.

Example App

  1. Central side:
#include "Particle.h"

SYSTEM_MODE(MANUAL);

SerialLogHandler log(LOG_LEVEL_INFO);

BleCharacteristic peerTxChar;

void onBleScanCallbackWrapper(const BleScanResult& result) {
    Log.info("I'm a normal function.");
}

void onBleScanCallbackWithName(const char* name, const BleScanResult& result, void* context) {
    Log.info("I'm a normal function with context: %s", name);
}

class Test {
public:
    void scanHandler(const BleScanResult& result) {
        Log.info("Test::scanHandler()");
    }

    void dataHandler(const uint8_t* buf, size_t len) {
        Log.info("Test::dataHandler(), Data received:");
        for (uint8_t i = 0; i < len; i++) {
            Serial.printf("0x%02X ", buf[i]);
        }
        Serial.println("");
    }

    void disconnectHandler(const BlePeerDevice& peer) {
        Log.info("Test::disconnectHandler(), Disconnected by peer device.");
    }
};

Test test;

void setup() {
    while (!Serial.isConnected());
    delay(1s);

    Log.info("Start scanning...");
    BLE.scan(onBleScanCallbackWrapper);
    Log.info("Scan stopped\r\n");

    delay(3s);

    Log.info("Start scanning...");
    BLE.scan([](const BleScanResult& result) {
        Log.info("I'm a lambda.");
    });
    Log.info("Scan stopped\r\n");

    delay(3s);

    Log.info("Start scanning...");
    BLE.scan(std::bind(onBleScanCallbackWithName, "BindCallback", std::placeholders::_1, nullptr));
    Log.info("Scan stopped\r\n");

    Log.info("Start scanning...");
    BLE.scan(&Test::scanHandler, &test);
    Log.info("Scan stopped\r\n");

    delay(3s);

    // auto dataCb = [](const uint8_t* buf, size_t len, BleCharacteristic* characteristic) {
    //     Log.info("Data received from %s: ", characteristic->UUID().toString().c_str());
    //     for (uint8_t i = 0; i < len; i++) {
    //         Serial.printf("0x%02X ", buf[i]);
    //     }
    //     Serial.println("");
    // };
    // peerTxChar.onDataReceived(std::bind(dataCb, std::placeholders::_1, std::placeholders::_2, &peerTxChar));
    peerTxChar.onDataReceived(&Test::dataHandler, &test);

    // BLE.onDisconnected([](const BlePeerDevice& peer) {
    //     Log.info("Disconnected by peer device.");
    // });
    BLE.onDisconnected(&Test::disconnectHandler, &test);
}

void loop() {
    if (!BLE.connected()) {
        Log.info("Start scanning...");
        auto results = BLE.scan();
        for (const auto& result : results) {
            BleUuid foundServiceUUID;
            size_t svcCount = result.advertisingData.serviceUUID(&foundServiceUUID, 1);
            if (svcCount > 0 && foundServiceUUID == "6E400001-B5A3-F393-E0A9-E50E24DCCA9E") {
                Log.info("Connecting...");
                BlePeerDevice peer = BLE.connect(result.address);
                if (peer.connected()) {
                    Log.info("Connected.");
                    peer.getCharacteristicByUUID(peerTxChar, "6E400003-B5A3-F393-E0A9-E50E24DCCA9E");
                    if (!peerTxChar.valid()) {
                        Log.error("TX characteristic not found.");
                    }
                }
                break;
            }
        }
    }
}
  1. Peripheral side:
  • user/tests/app/ble/uart_peripheral

References

N/A


Completeness

  • User is totes amazing for contributing!
  • Contributor has signed CLA (Info here)
  • Problem and Solution clearly stated
  • Run unit/integration/application tests on device
  • Added documentation
  • Added to CHANGELOG.md after merging (add links to docs and issues)

@XuGuohui XuGuohui added needs review feature ble Bluetooth Low Energy labels Oct 19, 2020
@XuGuohui XuGuohui added this to the 3.0.0 milestone Oct 19, 2020
@XuGuohui XuGuohui force-pushed the feature/ble_cpp_callback/ch65803 branch from ce6c6bf to d567d5b Compare October 26, 2020 13:13
@XuGuohui XuGuohui changed the base branch from develop to feature/ble_scan_filter/ch37224 October 26, 2020 13:15
Copy link
Member

@avtolstoy avtolstoy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@XuGuohui OK, but if possible let's refactor and just use std::function everywhere.

wiring/inc/spark_wiring_ble.h Show resolved Hide resolved
wiring/src/spark_wiring_ble.cpp Outdated Show resolved Hide resolved
@XuGuohui XuGuohui force-pushed the feature/ble_scan_filter/ch37224 branch from 7c6a032 to a759caf Compare November 20, 2020 06:47
Base automatically changed from feature/ble_scan_filter/ch37224 to develop November 20, 2020 11:42
@XuGuohui XuGuohui force-pushed the feature/ble_cpp_callback/ch65803 branch from d567d5b to e3967bb Compare November 20, 2020 13:28
disconnectedCb_(nullptr),
connectedContext_(nullptr),
disconnectedContext_(nullptr) {
: wiringConnectedCb_(nullptr),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: probably fine without wiring prefix :)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed.

@XuGuohui XuGuohui merged commit abbdd44 into develop Nov 20, 2020
@XuGuohui XuGuohui deleted the feature/ble_cpp_callback/ch65803 branch November 20, 2020 14:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
ble Bluetooth Low Energy feature needs review
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants