Step 2: Give the phone something to read
Step 2: Give the phone something to read
Step 2: Give the phone something to read
Advertising made the device discoverable; now inspect Figure 1 to decide how the phone will find and interpret the actual sensor value. The hierarchy matters because a characteristic only has application meaning inside an agreed service, UUID, encoding, properties, and access policy.
Read Figure 1 from the GATT server into its services and then down to characteristics. For this build, the phone discovers Environmental Sensing service 0x181A, then Temperature characteristic 0x2A6E, whose properties and encoding define what the client can read or subscribe to. That ordered discovery connects the scanner exercise to the code below: the ESP32 becomes ESP32-TempSensor, but interoperability comes from the standard service contract rather than the device name.
#include <BLEDevice.h>
#include <BLEServer.h>
#include <BLE2902.h>
#define SERVICE_UUID "181A" // Environmental Sensing (standard)
#define TEMP_CHAR_UUID "2A6E" // Temperature (standard)
BLECharacteristic* pTempChar = NULL;
bool deviceConnected = false;
float temperature = 22.5;
class ServerCB : public BLEServerCallbacks {
void onConnect(BLEServer* s) { deviceConnected = true; }
void onDisconnect(BLEServer* s) {
deviceConnected = false;
s->getAdvertising()->start(); // Resume advertising
}
};
void setup() {
Serial.begin(115200);
BLEDevice::init("ESP32-TempSensor");
BLEServer* pServer = BLEDevice::createServer();
pServer->setCallbacks(new ServerCB());
BLEService* pSvc = pServer->createService(SERVICE_UUID);
pTempChar = pSvc->createCharacteristic(TEMP_CHAR_UUID,
BLECharacteristic::PROPERTY_READ |
BLECharacteristic::PROPERTY_NOTIFY);
pTempChar->addDescriptor(new BLE2902()); // CCCD for notifications
pSvc->start();
BLEDevice::getAdvertising()->addServiceUUID(SERVICE_UUID);
BLEDevice::getAdvertising()->start();
}
void loop() {
if (deviceConnected) {
temperature += random(-10, 11) / 100.0;
int16_t val = (int16_t)(temperature * 100); // 0.01 C resolution
pTempChar->setValue((uint8_t*)&val, 2);
pTempChar->notify();
}
delay(1000);
}The service UUID 181A tells generic BLE tools that this is Environmental Sensing data. The characteristic UUID 2A6E says the value is temperature. Standard services improve interoperability with generic tools and gateways, while vendor-specific UUIDs still work for private services when you document them clearly; the Bluetooth SIG database matters for shared meaning, not for whether packets can move. PROPERTY_READ supports polling, while PROPERTY_NOTIFY supports real-time updates after the client enables notifications. The BLE2902 descriptor is the Client Characteristic Configuration Descriptor, or CCCD, that gives the client a place to write that opt-in. The temperature is packed as an int16_t in 0.01 C units, so 22.50 C travels as 2250.
Checkpoint: you should now be able to discover ESP32-TempSensor, find service 0x181A, read characteristic 0x2A6E, and receive changing notifications once the client writes the CCCD.