embeddedbluetooth-lowenergymbed

How to read/write data via BLE in Mbed OS 6?


I have an Android smartphone and a Discovery L475VG IoT development board. I am trying send/receive data (both directions) via BLE. I have consulted a good bit of documentation (although much of it is out of date for Mbed OS 6) and I have been trying to analyze what's going on in the GattServer characteristicUpdate and characteristicWrite example codes. See here

The two example codes work for what they're supposed to demonstrate, but they seem to go about implementing reads and writes differently, or maybe I am just not following what's going on?

If it isn't obvious, I am not super familiar with the BLE stack as a whole, and particularly not the mbed implementation of it. I previously made an Arduino-based system that accomplished the same goal I'm trying to here, but I am having trouble figuring out more complicated, lower-level library that mbed provides.

The read() and write() functions, which seem like the obvious things I would need, appear to be private functions, so I assume there is more to it than that and, naturally, mbed Studio doesn't just let me call them from main().

Can someone point me in the right direction of where to start?


Solution

  • Ok, I have figured it out, thanks to Michael Kotzjan for the help:

        virtual void onDataWritten(const GattWriteCallbackParams &params)
        {
            if ((params.handle == _writable_characteristic->getValueHandle()) && (params.len == 1)) {
                printf("New characteristic value written: %x\r\n", *(params.data));
                // Set value of characteristic to value * 2
                uint8_t temp = *params.data * 2;
                _server->write(_writable_characteristic->getValueHandle(),&temp, sizeof(temp),false);
            }
        }
    

    When the phone sends a byte to the mbed board, the mbed board sets the write characteristic to double the value that was received. The pointer stuff was a bit confusing, but this works now.