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: introduces BLE.begin() and BLE.end() wiring APIs. #1890

Merged
merged 2 commits into from Aug 26, 2019

Conversation

@XuGuohui
Copy link
Contributor

commented Aug 22, 2019

IMPORTANT: this PR targets #1882 branch

Problem

In Device OS 1.3.1-rc.1 and below, to terminate all the BLE activities initiated by user application, we can call BLE.off() to just shut off the BLE radio. While this API will also prevent the mobile setup from going further when device is in the Listening mode, since BLE will stop advertising and Peripheral connection is dropped if connected when that API is called.

Solution

By introducing the BLE.end() API, it makes sure that only the BLE activities initiated by user application is terminated, leaving the mobile setup works as expected.

Examples App

  1. BLE scanner
#include "Particle.h"

SYSTEM_MODE(MANUAL);

Serial1LogHandler log(115200, LOG_LEVEL_ALL);

void bleOnScanResultCallback(const BleScanResult* result, void* context) {
    LOG(TRACE, "---------- END ----------");
    BLE.end()
}

void setup() {
}

void loop() {
    BLE.scan(bleOnScanResultCallback, nullptr);
    delay(3000);
}
  1. BLE Peripheral
#include "Particle.h"

SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);

Serial1LogHandler log(115200, LOG_LEVEL_ALL);

const char* serviceUuid = "6E400001-B5A3-F393-E0A9-E50E24DCCA9E";

void setup() {
    BleAdvertisingData data;
    data.appendServiceUUID(serviceUuid);
    BLE.advertise(&data);
}

void loop() {
    delay(20000);
    LOG(TRACE, "---------- END ----------");
    BLE.end()
}
  1. BLE Central
#include "Particle.h"

SYSTEM_MODE(MANUAL);
SYSTEM_THREAD(ENABLED);

Serial1LogHandler log(115200, LOG_LEVEL_ALL);

#define SCAN_RESULT_COUNT       20

BleScanResult results[SCAN_RESULT_COUNT];

BlePeerDevice peer;

void setup() {
}

void loop() {
    if (peer.connected()) {
        delay(10000);
        LOG(TRACE, "---------- END ----------");
        BLE.end()
    }
    else {
        delay(3000);
        size_t count = BLE.scan(results, SCAN_RESULT_COUNT);
        if (count > 0) {
            for (uint8_t i = 0; i < count; i++) {
                BleUuid foundServiceUUID;
                size_t svcCount = results[i].advertisingData.serviceUUID(&foundServiceUUID, 1);
                if (svcCount > 0 && foundServiceUUID == "6E400001-B5A3-F393-E0A9-E50E24DCCA9E") {
                    peer = BLE.connect(results[i].address);
                    break;
                }
            }
        }
    }
}

Steps to Test

  1. Build and flash the 1st application, see if BLE stops scanning on device scanned.
  2. Build and flash the 2nd application:
    • See if BLE stops advertising after 20 seconds.
    • On device started, make it enter the Listening mode, see if BLE is still advertising after 20 seconds.
    • On device started, make it enter the Listening mode and connect to the device using a mobile App, see if BLE connection is retained after 20 seconds.
  3. Build and flash the 3rd application (need another device that is running the 1st application but without the BLE.end() is called), see if BLE connection is dropped after device being connected for 10 seconds.

References

Known issue: #1847

}

int BleLocalDevice::on() const {
WiringBleLock lk;

This comment has been minimized.

Copy link
@avtolstoy

avtolstoy Aug 23, 2019

Member

hal_ble_stack_init()?

This comment has been minimized.

Copy link
@avtolstoy

avtolstoy Aug 23, 2019

Member

Shouldn't we also call it when entering listening mode?

@avtolstoy avtolstoy modified the milestones: 1.4.0, 1.4.0-rc.1 Aug 23, 2019

@XuGuohui XuGuohui force-pushed the refactor/ble_listening_mode branch from 2dabf41 to a55192a Aug 23, 2019

@XuGuohui XuGuohui force-pushed the feature/ble_end_api branch from 4043316 to ca6b602 Aug 23, 2019

@XuGuohui XuGuohui requested a review from avtolstoy Aug 23, 2019

@XuGuohui XuGuohui merged commit 8873abf into refactor/ble_listening_mode Aug 26, 2019

2 of 3 checks passed

Codacy/PR Quality Review Codacy was unable to analyse your pull request.
Details
continuous-integration/travis-ci/pr The Travis CI build passed
Details
continuous-integration/travis-ci/push The Travis CI build passed
Details

@XuGuohui XuGuohui deleted the feature/ble_end_api branch Aug 26, 2019

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
2 participants
You can’t perform that action at this time.