esphome icon indicating copy to clipboard operation
esphome copied to clipboard

esp32_ble_tracker continuous and one shot scanning modes

Open jonofmac opened this issue 2 years ago • 0 comments

What does this implement/fix?

The current esp32_ble_tracker component only supports continuous scanning. These changes give the user additional features and flexibility without breaking existing automations. New features

  1. Added new scan_parameter called continuous, default true. When set to true, bluetooth scans in a loop forever as it does today. When set to false, it will only scan when instructed via a start_scan() action
  2. Added start_scan() action. Will start a single bluetooth scan. If there is an ongoing scan, then it is ignored (and a warning posted to log)
  3. Added an "on_ble_scan_end" trigger. This works for continuous or one-shot BLE scanning modes. It is called AFTER BLE clean up occurs, so that any actions in this automation can re-start a scan.

Types of changes

  • [ ] Bugfix (non-breaking change which fixes an issue)
  • [x] New feature (non-breaking change which adds functionality)
  • [ ] Breaking change (fix or feature that would cause existing functionality to not work as expected)
  • [ ] Other

Related issue or feature (if applicable): fixes <Not Applicable>

Pull request in esphome-docs with documentation (if applicable): esphome/esphome-docs#<esphome-docs PR number goes here> - Will update with this PR once I create one.

Test Environment

  • [x] ESP32
  • [ ] ESP32 IDF
  • [ ] ESP8266

Example entry for config.yaml:

# Example config.yaml
time:
  - platform: sntp
    id: sntp_time
    on_time_sync:
      then:
      - logger.log: "Synchronized system clock"
    on_time: 
      - seconds: 1
        minutes: /1
        then:
          - esp32_ble_tracker.start_scan:
              continuous: true

esp32_ble_tracker:
  id: blu_trax
  scan_parameters:
    interval: 100ms
    window: 60ms
    duration: 10s
    active: false
    continuous: false
    
  on_ble_scan_end:
    - then:
        - lambda: |-
             ESP_LOGD("BLE_SCAN_END_AUTOMATION", "The scan has ended!");
             id(blu_trax).start_scan();

Checklist:

  • [x] The code change is tested and works locally.
  • [ ] Tests have been added to verify that the new code works (under tests/ folder).

If user exposed functionality or configuration variables are added/changed:

jonofmac avatar Jul 22 '22 23:07 jonofmac