Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getName returns empty string #559

Closed
rodneygmorgan opened this issue Jun 28, 2023 · 3 comments
Closed

getName returns empty string #559

rodneygmorgan opened this issue Jun 28, 2023 · 3 comments

Comments

@rodneygmorgan
Copy link

What am I missing or doing wrong?

--- More details at https://bit.ly/pio-monitor-filters
--- Quit: Ctrl+C | Menu: Ctrl+T | Help: Ctrl+T followed by Ctrl+H
Found 8 devices
Connected to
Length of getName() : 0
Found our UUID : 4fafc201-1fb5-459e-8fcc-c5c9c331914b

BLE server code=========
#include <Arduino.h>
#include "NimBLEDevice.h"

NimBLEServer *pServer;

// void setup()
void setup(void)
{
Serial.begin(115200);
Serial.println("Starting NimBLE Server");

NimBLEDevice::init("Ranger Beacon");

// Create Server
pServer = NimBLEDevice::createServer();

// Create Service
NimBLEService* pService = pServer->createService("4fafc201-1fb5-459e-8fcc-c5c9c331914b");

// Create Characteristic
NimBLECharacteristic* pCharacteristic = pService->createCharacteristic("beb5483e-36e1-4688-b7f5-ea07361b26a8");

NimBLEDescriptor* pDscr = pCharacteristic->createDescriptor("Ranger",NIMBLE_PROPERTY::READ | NIMBLE_PROPERTY::WRITE,20);
pDscr->setValue("at Barn");

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

// Set Characteristic value
pCharacteristic->setValue("Barn");

NimBLEAdvertising* pAdvertising = NimBLEDevice::getAdvertising();
pAdvertising->addServiceUUID(pService->getUUID()); 

// Start advertising
pAdvertising->setScanResponse(true);
pAdvertising->start(); 

Serial.println("Advertising Started");

}

void loop()
{
delay(2000);
Serial.print(".");
}

BLE Client code=================
#include "NimBLEDevice.h"
#include <Arduino.h>

void setup() {
char foo[48];
Serial.begin(115200);
Serial.println("Starting NimBLE Client");

NimBLEDevice::init("");

NimBLEScan *pScan = NimBLEDevice::getScan();
NimBLEScanResults results = pScan->start(10);

NimBLEUUID serviceUuid("4fafc201-1fb5-459e-8fcc-c5c9c331914b");

Serial.printf("Found %i devices\n",results.getCount());
for(int i = 0; i < results.getCount(); i++) {
NimBLEAdvertisedDevice device = results.getDevice(i);

if (device.isAdvertisingService(serviceUuid)) {
  Serial.printf("Connected to %s\n",device.getName().c_str());
  Serial.printf("Length of getName() : %i\n",strlen(device.getName().c_str()));
  Serial.printf("Found our UUID : %s\n",device.getServiceUUID().toString().c_str());
  NimBLEClient *pClient = NimBLEDevice::createClient();
      
  if (pClient->connect(&device)) {
    NimBLERemoteService *pService = pClient->getService(serviceUuid);
            
    if (pService != nullptr) {
      NimBLERemoteCharacteristic *pCharacteristic = pService->getCharacteristic("beb5483e-36e1-4688-b7f5-ea07361b26a8");
                
      if (pCharacteristic != nullptr) {
        std::string value = pCharacteristic->readValue();
        //Serial.printf("Characteristic value %s\n",value);
        // print or do whatever you need with the value
      }
    }
  } else {
  // failed to connect
  }
        
  NimBLEDevice::deleteClient(pClient);
}

}
}

void loop() {
// put your main code here, to run repeatedly:
}

@h2zero
Copy link
Owner

h2zero commented Jun 29, 2023

The device may not be advertising it's name so there is no data.

@rodneygmorgan
Copy link
Author

rodneygmorgan commented Jun 29, 2023 via email

@h2zero
Copy link
Owner

h2zero commented Jun 29, 2023

Yes you need to set the name in the advertisement, the device name in init only sets the name value for the characteristic.

@h2zero h2zero closed this as completed Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants