1
0
mirror of https://github.com/esphome/esphome.git synced 2025-10-29 22:24:26 +00:00

Initial bluetooth_proxy support (#3736)

This commit is contained in:
Jesse Hills
2022-08-25 07:13:44 +12:00
committed by GitHub
parent 5dec9d88f6
commit b854e17995
16 changed files with 394 additions and 1 deletions

View File

@@ -0,0 +1,27 @@
from esphome.components import esp32_ble_tracker
import esphome.config_validation as cv
import esphome.codegen as cg
from esphome.const import CONF_ID
DEPENDENCIES = ["esp32", "esp32_ble_tracker"]
CODEOWNERS = ["@jesserockz"]
bluetooth_proxy_ns = cg.esphome_ns.namespace("bluetooth_proxy")
BluetoothProxy = bluetooth_proxy_ns.class_("BluetoothProxy", cg.Component)
CONFIG_SCHEMA = cv.Schema(
{
cv.GenerateID(): cv.declare_id(BluetoothProxy),
}
).extend(esp32_ble_tracker.ESP_BLE_DEVICE_SCHEMA)
async def to_code(config):
var = cg.new_Pvariable(config[CONF_ID])
await cg.register_component(var, config)
await esp32_ble_tracker.register_ble_device(var, config)
cg.add_define("USE_BLUETOOTH_PROXY")

View File

@@ -0,0 +1,58 @@
#include "bluetooth_proxy.h"
#ifdef USE_API
#include "esphome/components/api/api_pb2.h"
#include "esphome/components/api/api_server.h"
#endif // USE_API
#include "esphome/core/log.h"
#ifdef USE_ESP32
namespace esphome {
namespace bluetooth_proxy {
static const char *const TAG = "bluetooth_proxy";
bool BluetoothProxy::parse_device(const esp32_ble_tracker::ESPBTDevice &device) {
ESP_LOGV(TAG, "Proxying packet from %s - %s. RSSI: %d dB", device.get_name().c_str(), device.address_str().c_str(),
device.get_rssi());
this->send_api_packet_(device);
return true;
}
void BluetoothProxy::send_api_packet_(const esp32_ble_tracker::ESPBTDevice &device) {
#ifndef USE_API
return;
#else
api::BluetoothLEAdvertisementResponse resp;
resp.address = device.address_uint64();
if (!device.get_name().empty())
resp.name = device.get_name();
resp.rssi = device.get_rssi();
for (auto uuid : device.get_service_uuids()) {
resp.service_uuids.push_back(uuid.to_string());
}
for (auto &data : device.get_service_datas()) {
api::BluetoothServiceData service_data;
service_data.uuid = data.uuid.to_string();
for (auto d : data.data)
service_data.data.push_back(d);
resp.service_data.push_back(service_data);
}
for (auto &data : device.get_manufacturer_datas()) {
api::BluetoothServiceData manufacturer_data;
manufacturer_data.uuid = data.uuid.to_string();
for (auto d : data.data)
manufacturer_data.data.push_back(d);
resp.manufacturer_data.push_back(manufacturer_data);
}
api::global_api_server->send_bluetooth_le_advertisement(resp);
#endif
}
void BluetoothProxy::dump_config() { ESP_LOGCONFIG(TAG, "Bluetooth Proxy:"); }
} // namespace bluetooth_proxy
} // namespace esphome
#endif // USE_ESP32

View File

@@ -0,0 +1,26 @@
#pragma once
#ifdef USE_ESP32
#include <map>
#include "esphome/components/esp32_ble_tracker/esp32_ble_tracker.h"
#include "esphome/core/automation.h"
#include "esphome/core/component.h"
namespace esphome {
namespace bluetooth_proxy {
class BluetoothProxy : public Component, public esp32_ble_tracker::ESPBTDeviceListener {
public:
bool parse_device(const esp32_ble_tracker::ESPBTDevice &device) override;
void dump_config() override;
protected:
void send_api_packet_(const esp32_ble_tracker::ESPBTDevice &device);
};
} // namespace bluetooth_proxy
} // namespace esphome
#endif // USE_ESP32