Sending a string over bluetooth

How do I send a string using Bluetooth? I used ble101char.setValue(0x02) to send 1 value and used the same function multiple times with different values to send a string but it is too slow.

I looked at BLECharacteristics.cpp and found
BLECharacteristic::setValue(const unsigned char value, uint16_t length).

When I tried it an error occurred.
bleTest:37: error: no matching function for call to ‘BLECharCharacteristic::setValue(char [4], int)’
ble101char.setValue(dataStream,4);

The rest of my code is below. I want to know how to send a string using Bluetooth

#include <CurieBle.h>

BLEPeripheral ble101;
BLEService ble101Service("0BD51666-E7CB-469B-8E4D-2742F1BA77DD");
BLECharCharacteristic ble101char ("E7ADD780-B042-4876-AAE1-112855353CC1", BLEWrite | BLEIndicate);
char dataStream[] = {0x02,0x43,0x21,0x90};

void setup() {
  Serial.begin(9600);  
  ble101.setLocalName("ble101");
  ble101.setDeviceName("ble101");
  ble101.setAdvertisedServiceUuid(ble101Service.uuid());
  ble101.addAttribute(ble101Service);
  ble101.addAttribute(ble101char);
  ble101char.setValue(0);
  ble101.begin();
}

void loop() {  
  BLECentral central = ble101.central();

  if (central)
  {
    Serial.print("Connected to central: ");    
    Serial.println(central.address());

    while (central.connected()) 
    {      
     ble101char.setValue(dataStream,4);      
    }

    Serial.print(F("Disconnected from central: "));
    Serial.println(central.address());    
  }
}

I just figured this out last night. Use the base class, BLECharacteristic. You can provide an initial string value via the constructor (although oddly, only takes a char *, not a char * and length), and then use .setValue(char *, int len) to provide a string afterwards. Note that because this is binary data, you need to know the length of the string rather than expecting it to be zero-terminated.

Hello Guys, Do you know how to receive and convert a float from bluetooth? I'm trying on C# and can't make it work.

Hi dekduino, does that mean you have managed to send a string over bluetooth? If possible, could you post the code you used for the arduino and whatever program you use for the receiving bluetooth side?

zwyber: Hi dekduino, does that mean you have managed to send a string over bluetooth? If possible, could you post the code you used for the arduino and whatever program you use for the receiving bluetooth side?

I used this on Arduino side:

Code: [Select]

char buff[10]; String gyroString = ""; dtostrf(yaw, 4, 2, buff); //4 is mininum width, 2 is precision gyroString += buff; gyroString += ", "; dtostrf(roll, 4, 2, buff); gyroString += buff; gyroString += ", "; dtostrf(pitch, 4, 2, buff); gyroString += buff;

char b[20] ; gyroString.toCharArray(b, 20); yawCharacteristic.setValue((unsigned char*)b,20);

and then in c# you just parsed it like this on the notification event:

string gyro = System.Text.Encoding.UTF8.GetString(data);

Hi matiasjaure,
Did you ever manage to build a C# client to connect via BLE and receive data? would you have any code snippet in c#? It’s been really hard to find a good example!

Thanks!

amarie: Hi matiasjaure, Did you ever manage to build a C# client to connect via BLE and receive data? would you have any code snippet in c#? It's been really hard to find a good example!

Thanks!

Hi Amarie, did you develop a C# app to connect via BLE and receive data? I am trying to do that now.

Thank you, Tim