B4R Question How to use pointer to an array into String [c_str()]

AndroidMadhu

Active Member
Licensed User
Hello,
I want to convert the c_str() -- which is a builtin function in C++ which returns a pointer to an array into string at B4R.

In Inline C below is the line I want to convert into string

B4X:
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

int scanTime = 10; //In seconds
BLEScan* pBLEScan;

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());  ====>> This Line I am getting error

How do I change the value to String format. I am not able to convert the value to string.

The below is the Logs I am getting so far...
B4X:
Advertised Device: Name: , Address: 25:91:c8:a0:b8:2f, manufacturer data: 060001092002d4c7b0ef9a45a26e18156c112acd93549b955b573ea574



Please advice on this please
 
Last edited:

AndroidMadhu

Active Member
Licensed User
Hello,
I want to convert the c_str() -- which is a builtin function in C++ which returns a pointer to an array into string at B4R.

In Inline C below is the line I want to convert into string

B4X:
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

int scanTime = 10; //In seconds
BLEScan* pBLEScan;

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
      Serial.printf("Advertised Device: %s \n", advertisedDevice.toString().c_str());  ====>> This Line I am getting error

How do I change the value to String format. I am not able to convert the value to string.

The below is the Logs I am getting so far...
B4X:
Advertised Device: Name: , Address: 25:91:c8:a0:b8:2f, manufacturer data: 060001092002d4c7b0ef9a45a26e18156c112acd93549b955b573ea574



Please advice on this please
I am able to convert the pointer to string as below :
B4X:
String S1= advertisedDevice.toString().c_str();
Serial.println(S1);

But when I am trying to push the S1 value to B4R string I am getting error as below :

B4X:
Sub Process_Globals
Public Serial1 As Serial
    Public GetRSSI As Int
    Public GetAdd As String
    Public mymqtt As MqttClient
End Sub

inline C
------------------
b4r_main::_getadd=S1;


Error from Logs:
================
B4X:
b4r_main.cpp:35:22: error: cannot convert 'String' to 'B4R::B4RString*' in assignment
b4r_main::_getadd=S1;

Please advice

Thanks
 
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
Here is a solution with Globalstore. A B4RSting differs from a String.
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
Here is a solution with Globalstore. A B4RSting differs from a String.
Thanks @thetahsk ..... But when I am trying to convert the address of the device to B4R::string I am getting error and B4R hangs... No compilation happening...

Below is my fully working sketch running at Arduino IDE...
B4X:
#include <BLEDevice.h>
#include <BLEUtils.h>
#include <BLEScan.h>
#include <BLEAdvertisedDevice.h>

int scanTime = 10; //In seconds
BLEScan* pBLEScan;

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
    }
};


void setup() {
  Serial.begin(115200);
  Serial.println("Scanning...");
  BLEDevice::init("");
  pBLEScan = BLEDevice::getScan(); //create new scan
  pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
  pBLEScan->setActiveScan(true); //active scan uses more power, but get results faster
  pBLEScan->setInterval(100);
  pBLEScan->setWindow(99);  // less or equal setInterval value
 
}

void loop() {
  // put your main code here, to run repeatedly:
  BLEScanResults foundDevices = pBLEScan->start(scanTime, true);
  Serial.print("Devices found: ");
  Serial.println(foundDevices.getCount());
  int deviceCount= foundDevices.getCount();
  for (uint32_t i = 0; i < deviceCount; i++)
  {
    BLEAdvertisedDevice device = foundDevices.getDevice(i);
          Serial.print(device.getAddress().toString().c_str()); ----->>> Getting Error This Line. Not able to Convert to B4R String
          int rssi = device.getRSSI();
          Serial.print(". RSSI: ");
          Serial.println(rssi); 
  pBLEScan->clearResults();   // delete results fromBLEScan buffer to release memory
  delay(2000);
}
}
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
I am getting the below error while compiling...

B4X:
D:\Arduino_Test\BLE_RSSI\Objects\src\b4r_main.cpp: In function 'void loop(B4R::Object*)':
b4r_main.cpp:52:49: error: no matching function for call to 'B4R::B4RString::wrap(String&)'
     b4r_main::_globalstore->_put(0,s.wrap(devmac)->GetBytes());
                                                 ^
In file included from D:\Arduino_Test\BLE_RSSI\Objects\src\B4RDefines.h:24,
                 from D:\Arduino_Test\BLE_RSSI\Objects\src\b4r_main.cpp:1:
D:\Arduino_Test\BLE_RSSI\Objects\src\rCore.h:601:14: note: candidate: 'B4R::B4RString* B4R::B4RString::wrap(const char*)'
   B4RString* wrap(const char* c);
              ^~~~
D:\Arduino_Test\BLE_RSSI\Objects\src\rCore.h:601:14: note:   no known conversion for argument 1 from 'String' to 'const char*'


Below is the code snippet..
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'Public variables can be accessed from all modules.
    Public Serial1 As Serial
    Public GetRSSI As Int
    Public GetAdd As String  --->>>  I want to store the MAC at this variable
End Sub

Inline C:

B4X:
void loop(B4R::Object* msg) {
 
  BLEScanResults foundDevices = pBLEScan->start(scanTime, true);
  Serial.print("Devices found: ");
  Serial.println(foundDevices.getCount());
  int deviceCount= foundDevices.getCount();
  for (uint32_t i = 0; i < deviceCount; i++)
  {
    BLEAdvertisedDevice device = foundDevices.getDevice(i);
          //Serial.print(device.getAddress().toString().c_str());
          const char* str = (const char*)msg->data.PointerField;
          String devmac = device.getAddress().toString().c_str();
          devmac+=str;
          B4R::B4RString s;
          b4r_main::_globalstore->_put(0,s.wrap(devmac)->GetBytes());
          //Extracting RSSI Value.
          int rssi = device.getRSSI();
          b4r_main::_getrssi=rssi;
  pBLEScan->clearResults();
  delay(2000);
}
}

B4R code:
B4X:
Private Sub AppStart
    Serial1.Initialize(115200)
    Log("AppStart")
    GlobalStore.Put(0,"This is Global Store")
    RunNative("setup", Null)
    AddLooper("Looper1")
End Sub

Private Sub Looper1
    RunNative("loop", Null)
    Log("B4R RSSI Value is :" ,GetRSSI)
    Log(GlobalStore.Slot0)
End Sub

Please advice to fix the issue...
 
Upvote 0

thetahsk

Active Member
Licensed User
Longtime User
Read carefully the error messages.
Untestet, but should work.
B4X:
b4r_main::_globalstore->_put(0,s.wrap(device.getAddress().toString().c_str())->GetBytes());
 
Upvote 0

AndroidMadhu

Active Member
Licensed User
Read carefully the error messages.
Untestet, but should work.
B4X:
b4r_main::_globalstore->_put(0,s.wrap(device.getAddress().toString().c_str())->GetBytes());
Excellent.. Its working now.....
One more confusion. If there are multiple device and I want to capture multiple DeviceID for each device [Which I am able to capture at Inline C] and want to send at B4R[GlobalSore], Do I need to declare array of Slots at GlobalStore?
 
Upvote 0
Top