mirror of
https://github.com/h2zero/esp-nimble-cpp.git
synced 2024-11-28 08:00:55 +01:00
4980e6a10a
* General code cleanup. * `NimBLEAdvertisementData` moved to it's own .h and .cpp files. * Added new method, `NimBLEAdvertising::setPreferredParams` that takes the min and max preferred connection parameters as an alternative for `setMinPreferred` and `setMaxPreferred`. * Added new method, `NimBLEAdvertising::setAdvertisingInterval` Sets the advertisement interval for min and max to the same value instead of calling `setMinInterval` and `setMaxInterval` separately if there is not value difference. * `NimBLEAdvertisementData` payload is now stored in `std::vector<uint8_t>` instead of `std::string`. * `NimBLEAdvertisementData::getPayload` now returns `std::vector<uint8_t>` instead of `std::string`. * `NimBLEAdvertisementData::addData` now takes either a `std::vector<uint8_t>` or `uint8_t* + length` instead of `std::string` or `char + length`. * `NimBLEAdvertisementData::setName` now takes an optional `bool` parameter to indicate if the name is complete or incomplete, default = complete. * `NimBLEAdvertising::start` No longer takes a callback pointer parameter, instead the new method `NimBLEAdvertising::setAdvertisingCompleteCallback` should be used. * `NimBLEAdvertising::setAdvertisementType` has been renamed to `NimBLEAdvertising::setConnectableMode` to better reflect it's function. * `NimBLEAdvertising::setScanResponse` has been renamed to `NimBLEAdvertising::enableScanResponse` to better reflect it's function. * Scan response is no longer enabled by default. * Added new method, `NimBLEAdvertising::setDiscoverableMode` to allow applications to control the discoverability of the advertiser. * Advertising the name and TX power of the device will no longer happen by default and should be set manually by the application. * Added overload for `NimBLEAdvertising::setManufacturerData` that takes a `const uint8_t*` and , size_t` paramter. * Added overload for `NimBLEAdvertising::setServiceData` that takes `const NimBLEUUID& uuid`, ` const uint8_t* data`, ` size_t length` as parameters. * Added overload for `NimBLEAdvertising::setServiceData` that takes `const NimBLEUUID& uuid`, `const std::vector<uint8_t>&` as parameters. * All `NimBLEAdvertisementData` functions that change data values now return `bool`, true = success. * All `NimBLEAdvertising` functions that change data values now return `bool`, true = success. * `NimBLEAdvertising::setMinPreferred` and `NimBLEAdvertising::setMaxPreferred` have been removed, use `NimBLEAdvertising::setPreferredParams` instead. * All advertising data is now stored in instances of `NimBLEAdvertisingData` and vectors removed from `NimBLEAdvertising`. * `NimBLEAdvertising::setAdvertisementData` and `NimBLEAdvertising::setScanResponseData` now return `bool`, true = success. * Added new method, `NimBLEAdvertisementData::removeData`, which takes a parameter `uint8_t type`, the data type to remove. * Added new method, `NimBLEAdvertisementData::toString`, which will print the data in hex. * Added new method, `NimBLEAdvertising::getAdvertisementData`, which returns a reference to the currently set advertisement data. * Added new method, `NimBLEAdvertising::getScanData`, which returns a reference to the currently set scan response data. * Added overloads for `NimBLEAdvertising::removeServiceUUID` and `NimBLEAdvertisementData::removeServiceUUID` to accept a `const char*` * Added new method, `NimBLEAdvertising::clearData`, which will clear the advertisement and scan response data.
83 lines
3.2 KiB
C++
83 lines
3.2 KiB
C++
/** NimBLE_server_get_client_name
|
|
*
|
|
* Demonstrates 2 ways for the server to read the device name from the connected client.
|
|
*
|
|
* Created: on June 24 2024
|
|
* Author: H2zero
|
|
*
|
|
*/
|
|
|
|
#include <NimBLEDevice.h>
|
|
|
|
// See the following for generating UUIDs:
|
|
// https://www.uuidgenerator.net/
|
|
|
|
#define SERVICE_UUID "4fafc201-1fb5-459e-8fcc-c5c9c331914b"
|
|
#define CHARACTERISTIC_UUID "beb5483e-36e1-4688-b7f5-ea07361b26a8"
|
|
#define ENC_CHARACTERISTIC_UUID "9551f35b-8d91-42e4-8f7e-1358dfe272dc"
|
|
|
|
NimBLEServer* pServer;
|
|
|
|
class ServerCallbacks : public NimBLEServerCallbacks {
|
|
// Same as before but now includes the name parameter
|
|
void onConnect(NimBLEServer* pServer, NimBLEConnInfo& connInfo, std::string& name) override {
|
|
printf("Client address: %s Name: %s\n", connInfo.getAddress().toString().c_str(), name.c_str());
|
|
}
|
|
|
|
// Same as before but now includes the name parameter
|
|
void onAuthenticationComplete(NimBLEConnInfo& connInfo, const std::string& name) override {
|
|
if (!connInfo.isEncrypted()) {
|
|
NimBLEDevice::getServer()->disconnect(connInfo.getConnHandle());
|
|
printf("Encrypt connection failed - disconnecting client\n");
|
|
return;
|
|
}
|
|
|
|
printf("Encrypted Client address: %s Name: %s\n", connInfo.getAddress().toString().c_str(), name.c_str());
|
|
}
|
|
};
|
|
|
|
extern "C" void app_main(void) {
|
|
printf("Starting BLE Server!\n");
|
|
|
|
NimBLEDevice::init("Connect to me!");
|
|
NimBLEDevice::setSecurityAuth(true, false, true); // Enable bonding to see full name on phones.
|
|
|
|
pServer = NimBLEDevice::createServer();
|
|
NimBLEService* pService = pServer->createService(SERVICE_UUID);
|
|
NimBLECharacteristic* pCharacteristic =
|
|
pService->createCharacteristic(CHARACTERISTIC_UUID, NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE);
|
|
pCharacteristic->setValue("Hello World says NimBLE!");
|
|
|
|
NimBLECharacteristic* pEncCharacteristic = pService->createCharacteristic(
|
|
ENC_CHARACTERISTIC_UUID,
|
|
(NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE | NIMBLE_PROPERTY::READ_ENC | NIMBLE_PROPERTY::WRITE_ENC));
|
|
pEncCharacteristic->setValue("Hello World says NimBLE Encrypted");
|
|
|
|
pService->start();
|
|
|
|
pServer->setCallbacks(new ServerCallbacks());
|
|
pServer->getPeerNameOnConnect(true); // Setting this will enable the onConnect callback that provides the name.
|
|
|
|
BLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
|
|
pAdvertising->addServiceUUID(SERVICE_UUID);
|
|
pAdvertising->enableScanResponse(true);
|
|
|
|
pAdvertising->start();
|
|
printf("Advertising started, connect with your phone.\n");
|
|
|
|
while (true) {
|
|
auto clientCount = pServer->getConnectedCount();
|
|
if (clientCount) {
|
|
printf("Connected clients:\n");
|
|
for (auto i = 0; i < clientCount; ++i) {
|
|
NimBLEConnInfo peerInfo = pServer->getPeerInfo(i);
|
|
printf("Client address: %s Name: %s\n", peerInfo.getAddress().toString().c_str(),
|
|
// This function blocks until the name is retrieved, so cannot be used in callback functions.
|
|
pServer->getPeerName(peerInfo).c_str());
|
|
|
|
}
|
|
}
|
|
|
|
vTaskDelay(pdMS_TO_TICKS(10000));
|
|
}
|
|
}
|