Skip to content

Commit c324c45

Browse files
authored
feat(ble): Update BleGattServer and esp-nimble-cpp submodule (#366)
* feat(ble): Update `BleGattServer` and `esp-nimble-cpp` submodule * Add some new methods to `BleGattServer` * Remove logging from many methods which was unnecessary, and mark those methods `const` * Update `hid_service` example to print Manufacturer name, model number, and PnpId for remote device * Update `hid_service` example to print connected info more clearly and have cleaner code * Update `hid_service` example to wait until after auth to read values from the remote * Update `DeviceInfoService` to have static methods for parsing received PnpId * Update `DeviceInfoService` and `BatteryService` so UUIDs are public * Update `esp-nimble-cpp` submodule to latest, brining in improvements as well as new feature to register store callback for managing bonding info when it fills up. Allows you to read more information from the remotely connected device, if it is supported. Allows you to better control which bonds are deleted / how bonds are managed when the bond storage space fills up * Build and run `hid_service/exmaple` on QtPy ESP32s3 and test with iPhone and Android * Build and run `ble_gatt_server/example` on QtPy ESP32s3 and test with Android. * fix sa
1 parent 7c3ee7d commit c324c45

File tree

7 files changed

+294
-121
lines changed

7 files changed

+294
-121
lines changed

components/ble_gatt_server/example/main/ble_gatt_server_example.cpp

+33-11
Original file line numberDiff line numberDiff line change
@@ -130,20 +130,40 @@ extern "C" void app_main(void) {
130130
bool was_connected = false;
131131
bool can_exit = false;
132132
int num_seconds_to_wait = 30;
133+
auto loop_start = std::chrono::steady_clock::now();
133134
while (true) {
134135
auto start = std::chrono::steady_clock::now();
135136

136137
// if we are now connected, but were not, then get the services
137138
if (ble_gatt_server.is_connected() && !was_connected) {
139+
auto connected_device_infos = ble_gatt_server.get_connected_device_infos();
140+
// check to make sure the first connection has bonded at least.
141+
//
142+
// NOTE: if we are not bonded, then the name that will be read out will be
143+
// generic, such as "iPhone". If we are bonded, then the name will be the
144+
// actual device name, such as "iPhone 14 Plus William".
145+
const auto &first_device = connected_device_infos.front();
146+
if (first_device.isBonded()) {
147+
// if it was, mark connected and print all the device infos
148+
was_connected = true;
149+
can_exit = true;
150+
logger.info("Connected devices: {}", connected_device_infos.size());
151+
for (const auto &info : connected_device_infos) {
152+
auto rssi = ble_gatt_server.get_connected_device_rssi(info);
153+
auto name = ble_gatt_server.get_connected_device_name(info);
154+
auto mfg_name = ble_gatt_server.get_connected_device_manufacturer_name(info);
155+
auto model_number = ble_gatt_server.get_connected_device_model_number(info);
156+
auto pnp_id = ble_gatt_server.get_connected_device_pnp_id(info);
157+
logger.info(" RSSI: {}", rssi);
158+
logger.info(" Name: {}", name);
159+
// NOTE: these are optionals, so they may not be set
160+
logger.info(" Mfg: {}", mfg_name);
161+
logger.info(" Model: {}", model_number);
162+
logger.info(" PnP: {}", pnp_id);
163+
}
164+
}
138165
was_connected = true;
139166
can_exit = true;
140-
auto connected_device_infos = ble_gatt_server.get_connected_device_infos();
141-
logger.info("Connected devices: {}", connected_device_infos.size());
142-
std::vector<std::string> connected_device_names;
143-
std::transform(connected_device_infos.begin(), connected_device_infos.end(),
144-
std::back_inserter(connected_device_names),
145-
[&](auto &info) { return ble_gatt_server.get_connected_device_name(info); });
146-
logger.info(" Names: {}", connected_device_names);
147167
} else if (!ble_gatt_server.is_connected()) {
148168
was_connected = false;
149169
if (can_exit) {
@@ -155,13 +175,15 @@ extern "C" void app_main(void) {
155175
if (!ble_gatt_server.is_connected()) {
156176
logger.move_up();
157177
logger.clear_line();
158-
logger.info("Waiting for connection... {}s", --num_seconds_to_wait);
159-
if (num_seconds_to_wait == 0) {
178+
auto now = std::chrono::steady_clock::now();
179+
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - loop_start);
180+
logger.info("Waiting for connection... {}s", num_seconds_to_wait - elapsed.count());
181+
if (elapsed.count() > num_seconds_to_wait) {
160182
logger.info("No connection, exiting");
161183
break;
162184
}
163185
// sleep
164-
std::this_thread::sleep_until(start + 1s);
186+
std::this_thread::sleep_until(start + 100ms);
165187
continue;
166188
}
167189

@@ -170,7 +192,7 @@ extern "C" void app_main(void) {
170192
battery_level = (battery_level % 100) + 1;
171193

172194
// sleep
173-
std::this_thread::sleep_until(start + 1s);
195+
std::this_thread::sleep_until(start + 100ms);
174196
}
175197

176198
// we are done, so stop the server and deinit the BLE stack. NOTE: this will

components/ble_gatt_server/include/battery_service.hpp

+5-5
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,11 @@ namespace espp {
2626
/// device.
2727
class BatteryService : public BaseComponent {
2828
public:
29+
static constexpr uint8_t BATTERY_LEVEL_MAX = 100; ///< Maximum battery level
30+
static constexpr uint16_t BATTERY_LEVEL_UNIT = 0x27AD; ///< Unit is percentage
31+
static constexpr uint16_t BATTERY_SERVICE_UUID = 0x180F; ///< Battery Service UUID
32+
static constexpr uint16_t BATTERY_LEVEL_CHAR_UUID = 0x2A19; ///< Battery Level Characteristic UUID
33+
2934
/// Constructor
3035
/// \param log_level The log level for the component
3136
explicit BatteryService(espp::Logger::Verbosity log_level = espp::Logger::Verbosity::WARN)
@@ -103,11 +108,6 @@ class BatteryService : public BaseComponent {
103108
}
104109

105110
protected:
106-
static constexpr uint8_t BATTERY_LEVEL_MAX = 100; ///< Maximum battery level
107-
static constexpr uint16_t BATTERY_LEVEL_UNIT = 0x27AD; ///< Unit is percentage
108-
static constexpr uint16_t BATTERY_SERVICE_UUID = 0x180F; ///< Battery Service UUID
109-
static constexpr uint16_t BATTERY_LEVEL_CHAR_UUID = 0x2A19; ///< Battery Level Characteristic UUID
110-
111111
void make_service(NimBLEServer *server) {
112112
service_ = server->createService(NimBLEUUID(BATTERY_SERVICE_UUID));
113113
if (!service_) {

0 commit comments

Comments
 (0)