Go Down

Topic: Write/Read a simple string to/from a BLE Characteristic? (Read 8963 times) previous topic - next topic

bleckett

Can someone help please?

I'd like to send a Manufacturer Name based on the Bluetooth Sig Specification


But don't know how to place a string of less than 20 (ASCII) characters into the Characteristic. If only it was as easy as using setLocalName :(
 
So I'm looking for that 'magical encoding function' or steps (i.e., sprintf, strncpy, or loops) that can be added to this type of code...


Code: [Select]

BLEPeripheral blePeripheral;
BLECharCharacteristic manufacturerNameChar ("2A29", BLERead);

//...

void setup() {

    blePeripheral.setLocalName ("BLEDevice");

    blePeripheral.setAdvertisedServiceUuid (myService.uuid());

    blePeripheral.addAttribute (myService);
    blePeripheral.addAttribute (manufacturerNameChar);


    String manufacturerName = "ABC Corp";

    const unsigned char manufacturerNameArray[] = magical_encoding_function(manufacturerName);

    manufacturerNameChar.setValue(mainButtonValue);

    blePeripheral.begin();

    // ....



I'd also like to read the data, too. In Swift it's easy, but is there an easy way to do this in a sketch?

Code: [Select]

// Swift 3 code...
let manufacturerName = NSString(data: characteristic.value!, encoding: String.Encoding.utf8.rawValue)

// Arduino Sketch...

if (myStringChar.written() {

   myString = another_magical_encoding_function(myStringChar.value())




Appreciate any help.

PS: I saw a similar post here, but I don't have an android device, so I can't use the App and see what this would look like.
Can someone post the steps please?

govkn

bleckett,

Have you seen this post, it may be of help:

https://forum.arduino.cc/index.php?topic=432210.0

This is a bit of tricky topic, at least it was for me when I first started exploring BLE last year. All BLE data is sent as bytes. So you will have to translate the bytes you send at both ends of the communication.

One technique I learned how to use on the arduino sketch end is the union which allows you to share a memory location between two different data types. You can read about how this helped me handle a stream of floats here:

https://www.hackster.io/gov/imu-to-you-ae53e1?ref=channel&ref_id=12470_trending___&offset=5

You should also consider getting the book

Make: Bluetooth by Allan, Coleman and Mistry great projects, tips and advice for working with BLE on Arduino. It is the best book on BLE for the maker crowd.

Greg









bleckett

Hi Greg,

Sorry for the late reply. I'm still new to using the forms and didn't how to get to my messages.
I saw the topic you're referring to. Unfortunately, I don't have an Android device to load and run the app with.

And yes, I saw your imu-to-you. That's what got me started on all this :)
I'm definitely going to use your approach. Plus I'm want to send quaternion float data (x, y, z, m) and will try to do a union with that (only I don't know how read it back in Swift/iOS - but I'll work on it).

I picked up the Bluetooth book per your recommendation, too.
Been going through it, but all the 'functions' seem to be high level for js, android, and other languages.

String stringValue = "Hello";
const unsigned char dataValue[] = stringToBytes(stringValue); 
myCharacteristic.setValue(dataValue, sizeof(dataValue));

I really thought there would be a 'stringToBytes' type of function in c (for .ino sketches) to turn a string into a bytes or visa-versa that can be sent over BLE communications.

Anyway, the search continues.
Once I find a working solution, I'll post it for all to see.

Cheers...   Blaine

ccomparey

Blaine have you had any luck sending orientation as a string over BLE? I am trying to do the same with no luck

Go Up