The secret to String characteristics is that your strings need to use a fixed length. Here is a short example. You can write the characteristic with an app. I used BLE Scanner on iPhone.
/*
This example creates a BLE peripheral with a service that contains String
characteristics that can be read and written.
The length of the String characteristic needs to be fixed.
The circuit:
- Arduino Nano 33 BLE Sense board.
You can use a generic BLE central app, like LightBlue (iOS and Android) or
nRF Connect (Android), to interact with the services and characteristics
created in this sketch.
This example code is in the public domain.
*/
#include <ArduinoBLE.h>
//----------------------------------------------------------------------------------------------------------------------
// BLE UUIDs
//----------------------------------------------------------------------------------------------------------------------
#define BLE_UUID_TEST_SERVICE "9A48ECBA-2E92-082F-C079-9E75AAE428B1"
#define BLE_UUID_FILE_NAME "2D2F88C4-F244-5A80-21F1-EE0224E80658"
//----------------------------------------------------------------------------------------------------------------------
// BLE
//----------------------------------------------------------------------------------------------------------------------
BLEService testService( BLE_UUID_TEST_SERVICE );
BLEStringCharacteristic fileNameCharacteristic( BLE_UUID_FILE_NAME, BLERead | BLEWrite, 20 );
String fileName = "";
void setup()
{
Serial.begin( 9600 );
while ( !Serial );
BLE.begin();
// set advertised local name and service UUID:
BLE.setDeviceName( "Arduino Nano 33 BLE" );
BLE.setLocalName( "Arduino Nano 33 BLE" );
BLE.setAdvertisedService( testService );
// BLE add characteristics
testService.addCharacteristic( fileNameCharacteristic );
// add service
BLE.addService( testService );
// set the initial value for the characeristic:
fileNameCharacteristic.writeValue( fileName );
// start advertising
BLE.advertise();
}
void loop()
{
// listen for BLE peripherals to connect:
BLEDevice central = BLE.central();
if ( central )
{
Serial.print( "Connected to central: " );
Serial.println( central.address() );
while ( central.connected() )
{
if ( fileNameCharacteristic.written() )
{
fileName = fileNameCharacteristic.value();
Serial.print( "New file name: " );
Serial.println( fileName );
}
}
Serial.print( F( "Disconnected from central: " ) );
Serial.println( central.address() );
}
}