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 NimBLESecurityclass has been removed it's functionality has been merged into theNimBLEServerandNimBLEClientclasses.- All connection oriented callbacks now receive a reference to
NimBLEConnInfoand theble_gap_conn_descparameter has been replaced withNimBLEConnInfoin 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::startSecuritynow returns abool, true on success, instead of an int to be consistent with the rest of the library.NimBLEDevice::getInitializedrenamed toNimBLEDevice::isInitialized.NimBLEDevice::setPowerno longer takes theesp_power_level_tandesp_ble_power_type_t, instead only an integer value in dbm units is accepted, so instead ofESP_PWR_LVL_P9anint8_tvalue of9would be used for the same result.NimBLEDevice::setOwnAddrTypeno longer takes abool nrpaparameter, 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::setOwnAddrfirst before callingNimBLEDevice::setOwnAddrType.NimBLEDevice::getClientListSizereplaced withNimBLEDevice::getCreatedClientCount.NimBLEDevice::getClientListwas removed andNimBLEDevice::getConnectedClientscan be used instead which returns astd::vectorof pointers to the connected client instances. This was done because internally the clients are managed in astd::arraywhich replaced the 'std::list`.NimBLEDevice::getClientByID(uint16_t conn_id);has been changed toNimBLEDevice::getClientByHandle(uint16_t connHandle)
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.val and type is in ble_addr_t.type.
BLE UUID's
NimBLEUUID::getNativemethod replaced withNimBLEUUID::getBasewhich returns a read-only pointer to the underlyingble_uuid_tstruct.NimBLEUUID;msbFirstparameter has been removed from constructor, caller should reverse the data first or call the newNimBLEUUID::reverseByteOrdermethod after.
Server
NimBLEServer::disconnectnow returnsbool, true = success, instead ofintto be consistent with the rest of the library.NimBLEServerCallbacks::onMTUChangedrenamed toNimBLEServerCallbacks::onMTUChangeto be consistent with the client callback.NimBLEServer::getPeerIDInforenamed toNimBLEServer::getPeerInfoByHandleto better describe it's use.- Advertising is no longer automatically restarted when a peer disconnects, to re-enable this feature either call
NimBLEServer::advertiseOnDisconnect(true);after creating the server or manually restart advertising in theonDisconnectcallback.
Services
NimBLEService::getCharacteristicsnow returns aconst std::vector<NimBLECharacteristic*>&instead of a copy of the vector.
Characteristics
NimBLECharacteristic::notifyno longer takes abool is_notificationparameter, insteadNimBLECharacteristic::indicateshould be called to send indications.
Characteristic callbacks
NimBLECharacteristicCallbacks::onNotifyremoved as unnecessary, the library does not call notify without app input.NimBLECharacteristicCallbacks::onStatusNo longer takes astatusparameter, refer to the return code parameter for success/failure.
Server Security
NimBLEServerCallbacks::onPassKeyRequesthas been renamed toNimBLEServerCallbacks::onPassKeyDisplayas it is intended that the device should display the passkey for the client to enter.NimBLEServerCallbacks::onAuthenticationCompletenow takes aNimBLEConnInfo¶meter.
Client
NimBLEClient::getServicesnow returns a const reference to std::vector<NimBLERemoteService*> instead of a pointer to the internal vector.NimBLEClient::getConnIdhas been renamed togetConnHandleto be consistent with bluetooth terminology.NimBLEClient::disconnectnow returns abool, true on success, instead of anintto be consistent with the rest of the library.
Client callbacks
NimBLEClientCallbacks::onConfirmPINrenamed toNimBLEClientCallbacks::onConfirmPasskey, takes aNimBLEConnInfo¶meter 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::onPassKeyRequesthas been changed toNimBLEClientCallbacks::onPassKeyEntrywhich takes aNimBLEConnInfo¶meter 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::getCharacteristicsnow returns aconst std::vector<NimBLERemoteCharacteristic*>&instead of non-conststd::vector<NimBLERemoteCharacteristic*>*.
Remote Characteristics
-
NimBLERemoteCharacteristic::getRemoteServicenow returns aconst NimBLERemoteService*instead of non-const. -
NimBLERemoteCharacteristic::registerForNotify, has been removed, the application should useNimBLERemoteCharacteristic::subscribeandNimBLERemoteCharacteristic::unSubscribe.`NimBLERemoteCharacteristic::readUInt32` `NimBLERemoteCharacteristic::readUInt16` `NimBLERemoteCharacteristic::readUInt8` `NimBLERemoteCharacteristic::readFloat`
Have been removed, instead the application should use NimBLERemoteCharacteristic::readValue<type\>.
Scan
-
NimBLEScan::stopwill no longer call theonScanEndcallback as the caller should know it has been stopped either by initiating a connection or calling this function itself. -
NimBLEScan::clearDuplicateCachehas 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::startwith the newbool restartparameter set to true. -
NimBLEScanResults::getDevicemethods now returnconst NimBLEAdvertisedDevice*instead of a non-const pointer. -
NimBLEScanResultsiterators are nowconst_iterator. -
The callback parameter for
NimBLEScan::starthas been removed and the blocking overload ofNimBLEScan::starthas been replaced by an overload ofNimBLEScan::getResultswith 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 -
NimBLEAdvertisedDeviceCallbacksHas been replaced byNimBLEScanCallbackswhich contains the following methods: -
NimBLEScanCallbacks::onResult, functions the same as the oldNimBLEAdvertisedDeviceCallbacks::onResultbut now takes aaconst NimBLEAdvertisedDevice*instead of non-const.
-
NimBLEScanCallbacks::onScanEnd, replaces the scanEnded callback passed toNimBLEScan::startand now takes aconst NimBLEScanResults&andint reasonparameter.
-
NimBLEScanCallbacks::onDiscovered, This is called immediately when a device is first scanned, before any scan response data is available and takes aconst NimBLEAdvertisedDevice*parameter.
-
NimBLEScan::setAdvertisedDeviceCallbacks(NimBLEAdvertisedDeviceCallbacks* callbacks, bool wantDuplicates)has been changed toNimBLEScan::setScanCallbacks(NimBLEScanCallbacks* callbacks, bool wantDuplicates);
Advertised Device
NimBLEAdvertisedDevice::hasRSSIremoved as redundant, RSSI is always available.NimBLEAdvertisedDevice::getPayloadnow returnsconst std::vector<uint8_t>&instead of a pointer to internal memory.NimBLEAdvertisedDeviceTimestamp removed, if needed then the app should track the time from the callback.
Advertising
NimBLEAdvertising::setMinPreferredandNimBLEAdvertising::setMaxPreferredhave been removed and replaced byNimBLEAdvertising::setPreferredParamswhich 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::setNameandNimBLEAdvertising::addTxPower. NimBLEAdvertising::setAdvertisementTypehas been renamed toNimBLEAdvertising::setConnectableModeto better reflect it's function.NimBLEAdvertising::setScanResponsehas been renamed toNimBLEAdvertising::enableScanResponseto better reflect it's function.NimBLEAdvertising; Scan response is no longer enabled by default.NimBLEAdvertising::startNo longer takes a callback pointer parameter, instead the new methodNimBLEAdvertising::setAdvertisingCompleteCallbackshould be used to set the callback function.
Beacons
- Removed Eddystone URL as it has been shutdown by google since 2021.
NimBLEEddystoneTLM::setTempnow takes anint16_tparameter instead of float to be friendly to devices without floating point support.NimBLEEddystoneTLM::getTempnow returnsint16_tto work with devices that don't have floating point support.NimBLEEddystoneTLM::setDatanow takes a reference to *NimBLEEddystoneTLM::BeaconDatainstead ofstd::string.NimBLEEddystoneTLM::getDatanow returns a reference to *NimBLEEddystoneTLM::BeaconDatainstead ofstd::string.NimBLEBeacon::setDatanow takesconst NimBLEBeacon::BeaconData&instead ofstd::string.NimBLEBeacon::getDatanow returnsconst NimBLEBeacon::BeaconData&instead ofstd::string.
Utilities
NimBLEUtils::dumpGapEventfunction removed.NimBLEUtils::buildHexDatareplaced withNimBLEUtils::dataToHexString, which returns astd::stringcontaining the hex string.