Tasmota/lib/libesp32_div/NimBLE-Arduino/docs/Improvements_and_updates.md

149 lines
6.2 KiB
Markdown
Raw Normal View History

2020-09-15 09:45:24 +01:00
# Improvements and updates
2022-08-17 11:20:12 +01:00
Many improvements have been made to this library vs the original, this is a brief overview of the most significant changes. Refer to the [class documentation](https://h2zero.github.io/esp-nimble-cpp/annotated.html) for further information on class specifics.
2020-09-15 09:45:24 +01:00
* [Server](#server)
* [Advertising](#advertising)
* [Client](#client)
* [General](#general)
<br/>
<a name="server"></a>
2022-08-17 11:20:12 +01:00
# Server
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
`NimBLEService::NimBLEService::createCharacteristic` takes a 3rd parameter to specify the maximum data size that can be stored by the characteristic. This allows for limiting the RAM use of the characteristic in cases where small amounts of data are expected.
<br/>
`NimBLECharacteristic::setValue(const T &s)`
`NimBLEDescriptor::setValue(const T &s)`
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
Now use the `NimbleAttValue` class and templates to accommodate standard and custom types/values.
2020-09-15 09:45:24 +01:00
**Example**
```
2022-08-17 11:20:12 +01:00
struct my_struct {
2020-09-15 09:45:24 +01:00
uint8_t one;
uint16_t two;
uint32_t four;
uint64_t eight;
float flt;
2022-08-17 11:20:12 +01:00
} myStruct;
2020-09-15 09:45:24 +01:00
myStruct.one = 1;
myStruct.two = 2;
myStruct.four = 4;
myStruct.eight = 8;
myStruct.flt = 1234.56;
pCharacteristic->setValue(myStruct);
2022-08-17 11:20:12 +01:00
// Arduino String support
String myString = "Hello";
pCharacteristic->setValue(myString);
2020-09-15 09:45:24 +01:00
```
2022-08-17 11:20:12 +01:00
This will send the struct to the receiving client when read or a notification sent.
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
`NimBLECharacteristic::getValue` now takes an optional timestamp parameter which will update it's value with the time the last value was received. In addition an overloaded template has been added to retrieve the value as a type specified by the user.
2020-09-15 09:45:24 +01:00
**Example**
```
time_t timestamp;
myStruct = pCharacteristic->getValue<myStruct>(&timestamp); // timestamp optional
```
<br/>
2022-08-17 11:20:12 +01:00
**Advertising will automatically start when a client disconnects.**
2020-09-15 09:45:24 +01:00
A new method `NimBLEServer::advertiseOnDisconnect(bool)` has been implemented to control this, true(default) = enabled.
<br/>
2022-08-17 11:20:12 +01:00
`NimBLEServer::removeService` takes an additional parameter `bool deleteSvc` that if true will delete the service and all characteristics / descriptors belonging to it and invalidating any pointers to them.
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
If false the service is only removed from visibility by clients. The pointers to the service and it's characteristics / descriptors will remain valid and the service can be re-added in the future using `NimBLEServer::addService`.
2020-09-15 09:45:24 +01:00
<br/>
<a name="advertising"></a>
# Advertising
`NimBLEAdvertising::start`
2022-08-17 11:20:12 +01:00
Now takes 2 optional parameters, the first is the duration to advertise for (in seconds), the second is a callback that is invoked when advertising ends and takes a pointer to a `NimBLEAdvertising` object (similar to the `NimBLEScan::start` API).
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
This provides an opportunity to update the advertisement data if desired.
2022-02-12 19:01:12 +00:00
2022-08-17 11:20:12 +01:00
Also now returns a bool value to indicate if advertising successfully started or not.
<br/>
2020-09-15 09:45:24 +01:00
<a name="client"></a>
2022-08-17 11:20:12 +01:00
# Client
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
`NimBLERemoteCharacteristic::readValue(time_t\*, bool)`
`NimBLERemoteDescriptor::readValue(bool)`
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
Have been added as templates to allow reading the values as any specified type.
2020-09-15 09:45:24 +01:00
**Example**
```
struct my_struct{
uint8_t one;
uint16_t two;
uint32_t four;
uint64_t eight;
float flt;
}myStruct;
time_t timestamp;
myStruct = pRemoteCharacteristic->readValue<myStruct>(&timestamp); // timestamp optional
2022-08-17 11:20:12 +01:00
```
2020-09-15 09:45:24 +01:00
<br/>
2022-08-17 11:20:12 +01:00
`NimBLERemoteCharacteristic::registerForNotify`
Has been **deprecated** as now the internally stored characteristic value is updated when notification/indication is received.
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
`NimBLERemoteCharacteristic::subscribe` and `NimBLERemoteCharacteristic::unsubscribe` have been implemented to replace it.
A callback is no longer required to get the most recent value unless timing is important. Instead, the application can call `NimBLERemoteCharacteristic::getValue` to get the last updated value any time.
<br/>
2022-02-12 19:01:12 +00:00
2022-08-17 11:20:12 +01:00
The `notify_callback` function is now defined as a `std::function` to take advantage of using `std::bind` to specify a class member function for the callback.
2022-02-12 19:01:12 +00:00
2022-08-17 11:20:12 +01:00
Example:
2022-02-12 19:01:12 +00:00
```
using namespace std::placeholders;
notify_callback callback = std::bind(&<ClassName>::<memberFunctionCallbackName>, this, _1, _2, _3, _4);
2022-08-17 11:20:12 +01:00
2022-02-12 19:01:12 +00:00
<remoteCharacteristicInstance>->subscribe(true, callback);
```
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
`NimBLERemoteCharacteristic::readValue` and `NimBLERemoteCharacteristic::getValue` take an optional timestamp parameter which will update it's value with
the time the last value was received.
2020-09-15 09:45:24 +01:00
> NimBLEClient::getService
> NimBLERemoteService::getCharacteristic
2022-08-17 11:20:12 +01:00
> NimBLERemoteCharacteristic::getDescriptor
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
These methods will now check the respective vectors for the attribute object and, if not found, will retrieve (only)
the specified attribute from the peripheral.
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
These changes allow more control for the user to manage the resources used for the attributes.
2020-09-15 09:45:24 +01:00
<br/>
2022-08-17 11:20:12 +01:00
`NimBLEClient::connect()` can now be called without an address or advertised device parameter. This will connect to the device with the address previously set when last connected or set with `NimBLEDevice::setPeerAddress()`.
2020-09-15 09:45:24 +01:00
<a name="general"></a>
2022-08-17 11:20:12 +01:00
# General
To reduce resource use all instances of `std::map` have been replaced with `std::vector`.
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
Use of `FreeRTOS::Semaphore` has been removed as it was consuming too much ram, the related files have been left in place to accomodate application use.
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
Operators `==`, `!=` and `std::string` have been added to `NimBLEAddress` and `NimBLEUUID` for easier comparison and logging.
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
New constructor for `NimBLEUUID(uint32_t, uint16_t, uint16_t, uint64_t)` added to lower memory use vs string construction. See: [#21](https://github.com/h2zero/NimBLE-Arduino/pull/21).
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
Security/pairing operations are now handled in the respective `NimBLEClientCallbacks` and `NimBLEServerCallbacks` classes, `NimBLESecurity`(deprecated) remains for backward compatibility.
2020-09-15 09:45:24 +01:00
2022-08-17 11:20:12 +01:00
Configuration options have been added to add or remove debugging information, when disabled (default) significantly reduces binary size.
2020-09-15 09:45:24 +01:00
In ESP-IDF the options are in menuconfig: `Main menu -> ESP-NimBLE-cpp configuration`.
2022-08-17 11:20:12 +01:00
For Arduino the options must be commented / uncommented in nimconfig.h.
Characteristics and descriptors now use the `NimBLEAttValue` class to store their data. This is a polymorphic container class capable of converting to/from many different types efficiently. See: [#286](https://github.com/h2zero/NimBLE-Arduino/pull/286)
2020-09-15 09:45:24 +01:00