Esp32 not able to connect to android phone for data transfer

#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEServer.h>
#include <BLE2902.h>

// See the following for generating UUIDs:
// https://www.uuidgenerator.net/

#define SERVICE_UUID           "e03c2e56-a951-45b8-b02a-2a662930d3d3"
#define CHARACTERISTIC_UUID_TX "a9d75a3b-8668-42fc-a23b-3a86bbf45d55"

BLECharacteristic *pCharacteristic;
bool deviceConnected = false;
int txValue = 0;

class MyServerCallbacks : public BLEServerCallbacks
{
void onConnect(BLEServer *pServer)
{
    deviceConnected = true;
}

void onDisconnect(BLEServer *pServer)
{
    deviceConnected = false;
}



};









void setup() {
  Serial.begin(115200);
  Serial.println("Starting BLE work!");

  //Create the ble device
  BLEDevice::init("ESP32_BLE");

  //Create the ble server
  BLEServer *pServer = BLEDevice::createServer();
  pServer->setCallbacks(new MyServerCallbacks());

  //Create the ble service
  BLEService *pService = pServer->createService(SERVICE_UUID);

  //create the ble characteristic
  BLECharacteristic *pCharacteristic = pService->createCharacteristic(CHARACTERISTIC_UUID_TX, BLECharacteristic::PROPERTY_NOTIFY);

  //BLE2902 is needed to notify
  pCharacteristic->addDescriptor(new BLE2902());

  //start the service
  pService->start();

  //start advertising
  pServer->getAdvertising()->start();

  Serial.println("Waiting for a client connection to notify.....");
}

void loop() 
 {
   if(deviceConnected)
   {
     txValue = random(-10,20);
     // conversion of txValue
     char txString[8];
    dtostrf(txValue,1,2,txString);
    //setting the value to the characteristic
    pCharacteristic->setValue(txString);
    //Notifying the connected client
    pCharacteristic->notify();

    Serial.println("Sent value: " + String(txString));
    delay(500);
   }

 
 }

The above code  is written for sending data from ble device to android phone.
But the problem here is that,in nrf connect app i am not able to connect .

Kindly follow this code & inform me where i am doing mistake.


Thanks & Regards
Satyabrata Senapati  

suggest you initially try some of the ESP32 BLE examples
e.g. set Tools>Board to "ESP32 dev module" then open File>Examples>BLE>UART

compile/link/load the program into the ESP32
on the Android run the serial_bluetooth_terminal app
the ESP32 and Android can transfer text

1 Like

ok .let me check all these things.

I followed the notify example & it worked.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.