This is C++ newbie question. I have an Arduino sketch with following code to transmit bluetooth low energy UART notification.
When command
is 5 characters I get the 5 characters at the bluetooth receiver side. However, when I follow a 5 character command
with a single character command, what is received is the single character followed by last 3 characters from the previous command
.
Example,
command
is -1,0t
. What I receive is -1,0t
. But next command
is just r
. What I receive is r,0t
.
What's happening here? How do I get just "r"?
int BLEsend(String command){
byte length = sizeof(command);
unsigned char* notification = (unsigned char*) command.c_str(); // cast from string to unsigned char*
Serial.print(length);
BLE.sendData(UART_SEND, notification, length);
Serial.print("Notification Sent: "); Serial.println(command);
delay(100);
return 1;
}
sizeof( ) doesn't give you the length of the string.
You can use int length = command.length( );
instead.
Also command.size( );
should work as well.
you need also to copy the string command.c_str()
to notification
:
notification = new char[length + 1];
strcpy(notification , command.c_str());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.