# Writting string in à BLE caractéristiques using CurieBLE lib

**URL:** <https://forum.arduino.cc/t/writting-string-in-a-ble-caracteristiques-using-curieble-lib/417096>\
**Category:** Programming\
**Created:** [November 6, 2016, 8:24am UTC](https://forum.arduino.cc/t/writting-string-in-a-ble-caracteristiques-using-curieble-lib/417096 "2016-11-06T08:24:49Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![pat-ase30](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pat-ase30/32/243389_2.png) [@pat-ase30](https://forum.arduino.cc/u/pat-ase30)\
**Post date:** [November 6, 2016, 8:24am UTC](https://forum.arduino.cc/t/writting-string-in-a-ble-caracteristiques-using-curieble-lib/417096/1 "2016-11-06T08:24:49Z")

</div>

Hello everyone,

I encounter a problem on an Arduino 101 communicating with a smartphone application, over BLE with CurieBLE library. I'm using several Services and typed Characteristics sucessfully, but for one of them, I need to exchange a data string (array of char) in both direction.

I could find how to write (from Arduino side) using the "BLECharacteristic()" method without type declaration, and adding a lenght parameter. Then I write using "setValue()" specifiyng the lenght. That is Ok.

But I tried several similar methods to perform the opposite operation : Reading a string from the Characteristic.... I couldn't find any solution to be able to compile without any error. I tried severai type declarations, pointer, ... nothing is right !

My problem is on Arduino side, not with smartphone application.

Do someone already experiment this and have the solution ??

Some code extracts :  
unsigned char MyString[16] ;  
BLECharacteristic MyChar("xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx", BLERead | BLEWrite,16);  
MyChar.setValue(MyString,16) ;

Thank you in advance for your help.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [November 6, 2016, 11:05am UTC](https://forum.arduino.cc/t/writting-string-in-a-ble-caracteristiques-using-curieble-lib/417096/2 "2016-11-06T11:05:22Z")

</div>

> My problem is on Arduino side, not with smartphone application.

Your problem is that you posted no code and no links to any libraries.

---

<div class="post-metadata">

**Author:** ![pat-ase30](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/pat-ase30/32/243389_2.png) [@pat-ase30](https://forum.arduino.cc/u/pat-ase30)\
**Post date:** [November 18, 2016, 2:12pm UTC](https://forum.arduino.cc/t/writting-string-in-a-ble-caracteristiques-using-curieble-lib/417096/3 "2016-11-18T14:12:08Z")

</div>

Hi,

Finally, my problem is solved !  
I was extracting main lines codes from my application to post here, and had some ideas....  
In fact, since Characterostics are only "unsigned char \*" type when using for a string, it's obliged to cast variables used to write from or to.

Here is my simplified example.

//-------------------------------------------- Includes for BLE  
#include \<BLEAttribute.h\>  
#include \<BLECentral.h\>  
#include \<BLECharacteristic.h\>  
#include \<BLECommon.h\>  
//#include \<BLEDescriptor.h\>  
#include \<BLEPeripheral.h\>  
#include \<BLEService.h\>  
#include \<BLETypedCharacteristic.h\>  
#include \<BLETypedCharacteristics.h\>  
#include \<BLEUuid.h\>  
#include \<CurieBLE.h\>

//----------------------------------------------------------- BLE declaring  
BLEPeripheral blePeripheral;  
BLEService InitAdmin("41300000-EB90-09D7-0001-413031360000");  
BLECharacteristic Ssid("41300001-EB90-09D7-0001-413031360000", BLERead | BLEWrite,16);

//---------------------------------------------------------------------- Global Variables  
int i;  
char SsidLocal[32] ;

//----------------------------------------------------------------- Initialisation

void setup()  
{  
Serial.begin(9600);

// Set advertised local name and service UUID:  
blePeripheral.setLocalName("ASE\_A016");  
blePeripheral.setDeviceName("ASE\_A016");  
blePeripheral.setAppearance(true);

// No visible service ?  
[//blePeripheral.setAdvertisedServiceUuid](https://blePeripheral.setAdvertisedServiceUuid)(InitAdmin.uuid());

// Add Service & Caracteristic  
blePeripheral.addAttribute(InitAdmin);  
blePeripheral.addAttribute(Ssid);

// Write into the SSID characteristic  
sprintf(SsidLocal,"AZERTYUIOPQSDFGH");  
Ssid.setValue((unsigned char \*)SsidLocal,16) ; // Needs to cast because setValue accepts only unsigned char \*

// begin advertising BLE service:  
blePeripheral.begin();

Serial.println("A016 peripheral running...");  
Serial.println("");

}

//----------------------------------------------------------------------- Main loop  
void loop()  
{  
// listen for BLE peripherals to connect:  
BLECentral central = blePeripheral.central();

// if a central is connected to peripheral:  
if (central)  
{  
// print the central's MAC address:  
Serial.print("Connected to central: ");  
Serial.println(central.address());

// while the central is still connected to peripheral:  
while (central.connected())  
{  
if (Ssid.written()) // le SSID  
{  
Serial.print("Admin : SSID recu ");  
sprintf(SsidLocal,(char \*)Ssid.value()); // Needs to cast because Value is unsigned char \*  
Serial.println(SsidLocal);  
}  
delay(250);  
}

// when the central disconnects, print it out:  
Serial.print("Disconnected from central: ");  
Serial.println(central.address());  
Serial.println();

}  
}

If this may help someone !

Regards.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 5, 2021, 10:09pm UTC](https://forum.arduino.cc/t/writting-string-in-a-ble-caracteristiques-using-curieble-lib/417096/4 "2021-05-05T22:09:44Z")

</div>

Closed on May 5, 2021, 10:09pm UTC


