0

I've hacked apart the ESP32 BLE arduino sketches to do what I want. The server side is easy. Please see code below:

 if (con == 0){
      digitalWrite(LED, LOW);
    }
    
    if (con == 1){
      digitalWrite(LED, HIGH);
      delay(1000);
      digitalWrite(LED, LOW);
      delay(1000);
    }
    if (deviceConnected) {
        pCharacteristic->setValue((uint8_t*)&value, 4);
        pCharacteristic->notify();
        value++;
        delay(3); // bluetooth stack will go into congestion, if too many packets are sent, in 6 hours test i was able to go as low as 3ms
        con = 1;
    }
    // disconnecting
    if (!deviceConnected && oldDeviceConnected) {
        delay(500); // give the bluetooth stack the chance to get things ready
        pServer->startAdvertising(); // restart advertising
        Serial.println("start advertising");
        oldDeviceConnected = deviceConnected;
        con = 0;
    }

This works exactly how I want. It simply sits idle doing nothing, when a device connects to the BLE server then it will flash an LED.

No problems there, even though I suspect my code isn't 'that pretty.

What i'm having trouble doing however is creating an ESP32 client to connect to the BLE device.

The client has the name set as

BLEDevice::init("BOX_A1");

The example code seems to want UID for both the service and characteristic. Is there any way to just connect to the short advertised name? No data is being shared, it's just simply acting as a beacon to identify a box when connected to.

Thanks

Andrew

2 Answers 2

0

You can't connect to a device using the advertised name, not directly at least.

If you want to use the advertised name you have to scan for all BLE devices around you and select the one matching your name. The BLE scan example shows you how this is done. The code scans for a scanTime of 5 seconds, waits 2 seconds and starts scanning again:

void loop() {
  // put your main code here, to run repeatedly:
  BLEScanResults foundDevices = pBLEScan->start(scanTime, false);
  Serial.print("Devices found: ");
  Serial.println(foundDevices.getCount());
  Serial.println("Scan done!");
  pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
  delay(2000);
}

The example just prints the amount of found devices, you want to search through them and look for the correct name. The BLEScan returns an object of type BLEScanResults. You can access the found devices using getDevice with an index. Something like this might work to print the names of all found devices:

BLEAdvertisedDevice device;
for (int i = 0; i < foundDevices.getCount(); ++i) {
  device = foundDevices.getDevice(i);
  Serial.println(device.getName().c_str());
}

Now you can compare the names and work with the correct device.

Sign up to request clarification or add additional context in comments.

4 Comments

device.getName() always seems to be an empty string. Do you see otherwise?
Do you also see no name when using a generic BLE exploration tool such as nRF Connect? Not every device has its name set, so maybe it is correct that it is empty. The best way to filter for your device is the service UUID
I see a name with nRF Connect but not using the BLE scan example you linked. eg Advertised Device: Name: , Address: 40:22:d8:22:b2:82, serviceUUID: 91bad492-b950-4226-aa2b-4ede9fa42f59, rssi: -40 Advertised Device: Name: , Address: 73:97:a5:26:c0:f8, serviceUUID: 0000fe9f-0000-1000-8000-00805f9b34fb, rssi: -81, serviceData:
@juliusspencer maybe you might want to ask a new question to get some more insight to the problem you experience :)
0

To my understanding,

  1. You want the client to connect to the server with given advertised name.
  2. After connection is success, server turns on led.

You do have notification service running on server, but your client isn't interested. Below is the client code which only connects to server with name "BOX_A1".

  1. First Set our callback function that checks if device name matches when scanner discovers the devices:
class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks 
{
  void onResult(BLEAdvertisedDevice advertisedDevice) 
  {
    if (advertisedDevice.getName() == "BOX_A1") 
    { 
      advertisedDevice.getScan()->stop(); //Scan can be stopped, we found what we are looking for
      foundDevice = new BLEAdvertisedDevice(advertisedDevice);
      deviceFound = true;
      
      Serial.print("Device found:  ");
      Serial.println(advertisedDevice.toString().c_str());
    }
  }
};
  1. Use the BLEAdvertisedDevice object i.e. foundDevice object to connect to this server.
BLEClient* connectToServer(BLEAdvertisedDevice* device) {
  BLEClient* pClient = BLEDevice::createClient();

  if (pClient->connect(device)){ // Connect to the remote BLE Server.
      Serial.println(" - Connected to server");
      return pClient;
  }
  else{
    Serial.println("Failed to Connect to device");
    return NULL;
  }
}
  1. Use following line to call this connect function, it return the client object which can be used to disconnect from server.
if(deviceFound==true){
    BLEClient* myDevice = connectToServer(device);
    if(myDevice!=NULL){
      Serial.print("Connected to the BLE Server: ");
      Serial.println(device->getName().c_str());//print name of server
      
      //DO SOME STUFF
      
      //disconnect the device
      myDevice->disconnect();
      Serial.println("Device disconnected.");
    }
  }

This was the client side.

At server side to set the connection status flag use the following:

//Setup callbacks onConnect and onDisconnect
class MyServerCallbacks: public BLEServerCallbacks {
  void onConnect(BLEServer* pServer) {
    Serial.println("Client Connected");
    deviceConnected = true;
  };
  void onDisconnect(BLEServer* pServer) {
    Serial.println("Client Disconnected");
    deviceConnected = false;
  }
};

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.