12 KiB
Migrating from 1.x to 2.x
Nearly all of the codebase has been updated and changed under the surface, which has greatly reduced the resource use of the library and improved it's performance. To be able to support these changes it required various API changes and additions.
This guide will help you migrate your application code to use the new API.
The changes listed here are only the required changes that must be made, and a short overview of options for migrating existing applications.
- General changes
- BLE Device
- BLE Addresses
- BLE UUID's
- Server
- Client
- Scanning
- Advertising
- Beacons
- Utilities
General changes
- All functions that take a time parameter now require the time in milliseconds instead of seconds, i.e.
NimBLEScan::start(10000); // 10 seconds
NimBLESecurity
class has been removed it's functionality has been merged into theNimBLEServer
andNimBLEClient
classes.- All connection oriented callbacks now receive a reference to
NimBLEConnInfo
and theble_gap_conn_desc
parameter has been replaced withNimBLEConnInfo
in the functions that received it.
For instanceonAuthenticationComplete(ble_gap_conn_desc* desc)
signature is nowonAuthenticationComplete(NimBLEConnInfo& connInfo)
and
NimBLEServerCallbacks::onConnect(NimBLEServer* pServer)
signature is nowNimBLEServerCallbacks::onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo)
.
BLE Device
- Ignore list functions and vector have been removed, the application should implement this if desired. It was no longer used by the library.
NimBLEDevice::startSecurity
now returns abool
, true on success, instead of an int to be consistent with the rest of the library.NimBLEDevice::getInitialized
renamed toNimBLEDevice::isInitialized
.NimBLEDevice::setPower
no longer takes theesp_power_level_t
andesp_ble_power_type_t
, instead only an integer value in dbm units is accepted, so instead ofESP_PWR_LVL_P9
anint8_t
value of9
would be used for the same result.NimBLEDevice::setOwnAddrType
no longer takes abool nrpa
parameter, the random address type will be determined by the bits the in the address instead.
Note: If setting a custom address, it should be set withNimBLEDevice::setOwnAddr
first before callingNimBLEDevice::setOwnAddrType
.NimBLEDevice::getClientListSize
replaced withNimBLEDevice::getCreatedClientCount
.NimBLEDevice::getClientList
was removed andNimBLEDevice::getConnectedClients
can be used instead which returns astd::vector
of pointers to the connected client instances. This was done because internally the clients are managed in astd::array
which replaced the 'std::list`.
BLE Addresses
NimBLEAddress comparisons have changed to now consider the address type. If 2 address values are the same but the type is different then they are no longer considered equal. This is a correction to the 1.x version which did not consider the type, whereas the BLE specification states:
Whenever two device addresses are compared, the comparison shall include the device address type (i.e. if the two addresses have different types, they are different even if the two 48-bit addresses are the same).
This means that if in your application you create a NimBLEAddress instance and are comparing a scan result or some other address created by the library and you did not define the address type then the comparison may fail.
The default address type is public 0
, whereas many devices use a random 1
address type.
If you experience this issue please create your address instances with the address type specified, i.e. NimBLEAddress address("11:22:33:44:55:66", TYPE_HERE)
NimBLEAddress::getNative
has been removed and replaced with NimBLEAddress::getBase
.
This returns a pointer to const ble_addr_t
instead of a pointer to the address value that getNative
did. The value can be accessed through this struct via ble_addr_t.value
and type is in ble_addr_t.type
.
BLE UUID's
NimBLEUUID::getNative
method replaced withNimBLEUUID::getBase
which returns a read-only pointer to the underlyingble_uuid_t
struct.NimBLEUUID
;msbFirst
parameter has been removed from constructor, caller should reverse the data first or call the newNimBLEUUID::reverseByteOrder
method after.
Server
NimBLEServer::disconnect
now returnsbool
, true = success, instead ofint
to be consistent with the rest of the library.NimBLEServerCallbacks::onMTUChanged
renamed toNimBLEServerCallbacks::onMTUChange
to be consistent with the client callback.NimBLEServer::getPeerIDInfo
renamed toNimBLEServer::getPeerInfoByHandle
to better describe it's use.
Services
NimBLEService::getCharacteristics
now returns aconst std::vector<NimBLECharacteristic*>&
instead of a copy of the vector.
Characteristics
NimBLECharacteristic::notify
no longer takes abool is_notification
parameter, insteadNimBLECharacteristic::indicate
should be called to send indications.
Characteristic callbacks
NimBLECharacteristicCallbacks::onNotify
removed as unnecessary, the library does not call notify without app input.NimBLECharacteristicCallbacks::onStatus
No longer takes astatus
parameter, refer to the return code parameter for success/failure.
Server Security
NimBLEServerCallbacks::onPassKeyRequest
has been renamed toNimBLEServerCallbacks::onPassKeyDisplay
as it is intended that the device should display the passkey for the client to enter.NimBLEServerCallbacks::onAuthenticationComplete
now takes aNimBLEConnInfo&
parameter.
Client
NimBLEClient::getServices
now returns a const reference to std::vector<NimBLERemoteService*> instead of a pointer to the internal vector.NimBLEClient::getConnId
has been renamed togetConnHandle
to be consistent with bluetooth terminology.NimBLEClient::disconnect
now returns abool
, true on success, instead of anint
to be consistent with the rest of the library.
Client callbacks
NimBLEClientCallbacks::onConfirmPIN
renamed toNimBLEClientCallbacks::onConfirmPasskey
, takes aNimBLEConnInfo&
parameter and no longer returns a value. This should be used to prompt a user to confirm the pin on the display (YES/NO) after which the response should be sent withNimBLEDevice::injectConfirmPasskey
.NimBLEClientCallbacks::onPassKeyRequest
has been changed toNimBLEClientCallbacks::onPassKeyEntry
which takes aNimBLEConnInfo&
parameter and no longer returns a value. Instead of returning a value this callback should prompt a user to enter a passkey number which is sent later viaNimBLEDevice::injectPassKey
.
Remote Services
NimBLERemoteService::getCharacteristics
now returns aconst std::vector<NimBLERemoteCharacteristic*>&
instead of non-conststd::vector<NimBLERemoteCharacteristic*>*
.
Remote Characteristics
-
NimBLERemoteCharacteristic::getRemoteService
now returns aconst NimBLERemoteService*
instead of non-const. -
NimBLERemoteCharacteristic::registerForNotify
, has been removed, the application should useNimBLERemoteCharacteristic::subscribe
andNimBLERemoteCharacteristic::unSubscribe
.`NimBLERemoteCharacteristic::readUInt32` `NimBLERemoteCharacteristic::readUInt16` `NimBLERemoteCharacteristic::readUInt8` `NimBLERemoteCharacteristic::readFloat`
Have been removed, instead the application should use NimBLERemoteCharacteristic::readValue<type\>
.
Scan
-
NimBLEScan::stop
will no longer call theonScanEnd
callback as the caller should know it has been stopped either by initiating a connection or calling this function itself. -
NimBLEScan::clearDuplicateCache
has been removed as it was problematic and only for the original esp32. The application should stop and start the scanner for the same effect or callNimBLEScan::start
with the newbool restart
parameter set to true. -
NimBLEScanResults::getDevice
methods now returnconst NimBLEAdvertisedDevice*
instead of a non-const pointer. -
NimBLEScanResults
iterators are nowconst_iterator
. -
The callback parameter for
NimBLEScan::start
has been removed and the blocking overload ofNimBLEScan::start
has been replaced by an overload ofNimBLEScan::getResults
with the same parameters.So if your code prior was this:
NimBLEScanResults results = pScan->start(10, false);
It should now be:
NimBLEScanResults results = pScan->getResults(10000, false); // note the time is now in milliseconds
-
NimBLEAdvertisedDeviceCallbacks
Has been replaced byNimBLEScanCallbacks
which contains the following methods: -
NimBLEScanCallbacks::onResult
, functions the same as the oldNimBLEAdvertisedDeviceCallbacks::onResult
but now takes aaconst NimBLEAdvertisedDevice*
instead of non-const.
-
NimBLEScanCallbacks::onScanEnd
, replaces the scanEnded callback passed toNimBLEScan::start
and now takes aconst NimBLEScanResults&
andint reason
parameter.
-
NimBLEScanCallbacks::onDiscovered
, This is called immediately when a device is first scanned, before any scan response data is available and takes aconst NimBLEAdvertisedDevice*
parameter.
Advertised Device
NimBLEAdvertisedDevice::hasRSSI
removed as redundant, RSSI is always available.NimBLEAdvertisedDevice::getPayload
now returnsconst std::vector<uint8_t>&
instead of a pointer to internal memory.NimBLEAdvertisedDevice
Timestamp removed, if needed then the app should track the time from the callback.
Advertising
NimBLEAdvertising::setMinPreferred
andNimBLEAdvertising::setMaxPreferred
have been removed and replaced byNimBLEAdvertising::setPreferredParams
which takes both the min and max parameters.- Advertising the name and TX power of the device will no longer happen by default and should be set manually by the application using
NimBLEAdvertising::setName
andNimBLEAdvertising::addTxPower
. NimBLEAdvertising::setAdvertisementType
has been renamed toNimBLEAdvertising::setConnectableMode
to better reflect it's function.NimBLEAdvertising::setScanResponse
has been renamed toNimBLEAdvertising::enableScanResponse
to better reflect it's function.NimBLEAdvertising
; Scan response is no longer enabled by default.NimBLEAdvertising::start
No longer takes a callback pointer parameter, instead the new methodNimBLEAdvertising::setAdvertisingCompleteCallback
should be used to set the callback function.
Beacons
- Removed Eddystone URL as it has been shutdown by google since 2021.
NimBLEEddystoneTLM::setTemp
now takes anint16_t
parameter instead of float to be friendly to devices without floating point support.NimBLEEddystoneTLM::getTemp
now returnsint16_t
to work with devices that don't have floating point support.NimBLEEddystoneTLM::setData
now takes a reference to *NimBLEEddystoneTLM::BeaconData
instead ofstd::string
.NimBLEEddystoneTLM::getData
now returns a reference to *NimBLEEddystoneTLM::BeaconData
instead ofstd::string
.NimBLEBeacon::setData
now takesconst NimBLEBeacon::BeaconData&
instead ofstd::string
.NimBLEBeacon::getData
now returnsconst NimBLEBeacon::BeaconData&
instead ofstd::string
.
Utilities
NimBLEUtils::dumpGapEvent
function removed.NimBLEUtils::buildHexData
replaced withNimBLEUtils::dataToHexString
, which returns astd::string
containing the hex string.