Metamorphoses with string and char* variables from BLEScan data

My aim and task is creation BLE device list, show it at the TFT, choose one to connect by MAC-address.

Main part preparation is:

#define SCAN_TIME 10  // seconds
std::stringstream ss;

  BLEScan* pBLEScan = BLEDevice::getScan();  //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true);  //active scan uses more power, but get results faster
  pBLEScan->setInterval(0x50);
  pBLEScan->setWindow(0x30);
  BLEScanResults foundDevices = pBLEScan->start(SCAN_TIME);
  int count = foundDevices.getCount();

  for (int i = 0; i < count; i++) {
    BLEAdvertisedDevice d = foundDevices.getDevice(i);
    BLEAdvertisedDevice d = foundDevices.getDevice(i);
    ss << d.getAddress().toString() << ";" << d.getRSSI() << "\n";
  }

 const char* out = ss.str().c_str();

Than I do this:

Serial.printf("out = %s\n\n",out);
  if ( out[0] == 'a' ) {
    Serial.printf("c=%c\n",out[0]);
    Serial.printf("c=%c\n",out[1]);
    Serial.printf("c=%c\n",out[2]);
  }

And it shows correctly:
out = a4:c1:38:4f:cb:0c;-47
c=a
c=4
c=:

But if I do this:

  out2=String(out);
  Serial.printf("out2 = %s\n",out2);

I give strange unicode symbols
out2 = ?

The same result if I change to:

  Serial.printf("out2 = %c\n",out2);

The question is how can I get for example array of mac-addresses with normally String variables without strange behaviour. I already three day try many methods with no success.

String is a different type o variable for handling ASCII string data than C style arrays of chars, c-string. so out2 is not the address of an array of chars.

have you tried Serial.println (out2);? Serial.println() will handle a String variable type

but since it seems you want to use printf() why do you want to convert your c-string, out to a String?

First of all, I'm not a developer. It's just a hobby :slight_smile:

Ghhhm, println works well, but and the printf too. But my main aim is to gen normal variable thich I can print to TFT Display which apply only normal String as I know.

Excactly I try to convert it for diagnostic reason and I understand that it's not the same with show in display.

Thanks.

i'd be very surprised that some display device doesn't support c-string. the String variable type used by the Arduino IDE is more like the Java version than the C++ version.

i suggest using sprintf () to format a c-string which can be used to print using printf, Serial, or to the display device.

1 Like

Cool, tft.println works well too :slight_smile:
But now I should choose right address and send to function to connect. I could try it. Thank you, doctor.

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