Converting String to const char*

I'm trying to send a string from a Blend micro to an iOS app. I've been trying to modify the chat example but keep running into the error:

SimpleChat2.ino: In function 'void transmit(String)':
SimpleChat2:128: error: invalid cast from type 'String' to type 'unsigned char*'
SimpleChat2:128: error: cannot convert 'String' to 'const char*' for argument '1' to 'size_t strlen(const char*)'

I'm trying to replace the line from the example:

  • ble_write( Serial.read() );*

with:

String testString = "test content";

ble_write( testString );

How would I go about converting my string into something that will work?

#include <SPI.h>
#include <boards.h>
#include <RBL_nRF8001.h>

void setup()
{  
  // Default pins set to 9 and 8 for REQN and RDYN
  // Set your REQN and RDYN here before ble_begin() if you need
  //ble_set_pins(3, 2);
  
  // Set your BLE Shield name here, max. length 10
  //ble_set_name("My Name");
  
  // Init. and start BLE library.
  ble_begin();
  
  // Enable serial debug
  Serial.begin(57600);
}

unsigned char buf[16] = {0};
unsigned char len = 0;

void loop()
{
  if ( ble_available() )
  {
    while ( ble_available() )
      Serial.write(ble_read());
      
    Serial.println();
  }
  
  if ( Serial.available() )
  {
    delay(5);
    
    while ( Serial.available() ){
    
      //////This line from the example works
         ble_write( Serial.read() );

    //////These two lines of mine don't
        String testString = "test content";
        
        ble_write( testString );
    //////////////
  }
  
  }
  
  ble_do_events();
}

Why convert?
Why not simply use a string?

Serial.read() returns 1 byte...your testString is 13 bytes

I tried doing this:

transmit("testing1234");

transmit("testingABCD");

transmit("testing5678");

transmit("testingEFGH");



void transmit(String testString){uint8_t sendbuffer[20];
          testString.getBytes(sendbuffer, 20);
           char sendbuffersize = min( 20, testString.length());
      //I also tried:      uint8_t sendbuffersize = min( 20,testString.length());

          Serial.print(F("\n* Sending -> \"")); Serial.print((char *)sendbuffer); Serial.print("        ");Serial.print(sendbuffersize);Serial.println("");

          // write the data
    ble_write_bytes(sendbuffer, sendbuffersize);}

Which sends the whole string, but links consecutive sends together into 20 character strings. For example, the above sends the following:

testing1234testingAB
CDtesting5678testing
EFGH

rather than:

testing1234
testingABCD
testing5678
testingEFGH

I tried doing this:

Code: [Select]

transmit("testing1234";

What's that?

Sorry, I was typing in an example of what I was trying to do and forgot my closing parentheses.

I edited my previous post to fix the parentheses. But my problem is still the same. I need to know how to convert a string so that I can send it via Bluetooth.

What parameter type does the ble_write()
function require? Is it char *?

If so, why are you using C++ String instead of C string? Try:

const char[] = “test content”;

It still gives me the same error.

 //////These two lines of mine don't
        const char testString = "test content";
        
        ble_write( testString );
    //////////////

nonlinearmind:
It still gives me the same error.

So, I ask again: what parameter type does the ble_write() function require? I don't have that libray.

It isn't a String, so it doesn't give the same error message.

Post your code, post your error message.

I did this morning. Up top.

The code "up top" has a String.

It has been suggested (at least twice) that you don't use a String, but a string.

How's that going?

I don't know the parameter type. These are answers I'm trying to find out by posting here. I've been searching forums and stack exchange and this problem for this board seems to come up a bunch, but there are either no answers or the ones I tried haven't worked. There is little documentation and this is all very frustrating.

here is the library itself:

Have another look at reply #7

It's not going well. I want to be able to send "strings" of letters and numbers. You've told me not to use strings and I've been asking from the beginning, how do I convert "strings" of characters into something that I can send. I've asked that more than once.

So take a look at the file you just specified:

You'll see this:

void ble_write(unsigned char data)
{
    if(tx_buffer_len == MAX_TX_BUFF)
    {
            return;
    }
    tx_buff[tx_buffer_len] = data;
    tx_buffer_len++;
}

void ble_write_bytes(unsigned char *data, uint8_t len)
{
    for (int i = 0; i < len; i++)
        ble_write(data[i]);
}

Now you know. You can supply either a 'char' or a 'char *' and a 'uint8_t' string length.

nonlinearmind:
It's not going well. I want to be able to send "strings" of letters and numbers. You've told me not to use strings and I've been asking from the beginning, how do I convert "strings" of characters into something that I can send. I've asked that more than once.

No, we've told you not to use Strings.

When you say "look at reply #7, it isn't helpful, without elaboration, when in reply #8, I show where I tried it and I still get a conversion error. Though it is :

SimpleChat:45: error: invalid conversion from 'const char*' to 'char'

You didn't try what I suggested. You forgot the '[]'. Read it again.

But, it doesn't matter. You can see from the code snippet I just posted what the parameters need to be.