7 years, 7 months ago.

How can I add more arguments in and existing BLE program?

I am using a BLE program(cycle speed and cadence). It only sends 3 params right now and of its params ahs to be the function of timer.read(). If I assign it something else it doesnt show on my corresponding app. Please tell me how can I add more arguments of float type and show on my android. here is the link of program I am using, https://developer.mbed.org/users/tenfoot/code/BLE_CycleSpeedCadence/ , and also my modified program is as following.

modified cycle program

#include "mbed.h"
#include "ble/BLE.h"
#include "CyclingSpeedAndCadenceService.h"
#include "ble/services/BatteryService.h"
#include "ble/services/DeviceInformationService.h"

BLE  ble;
Timer timer;
Timer t;
const static char     DEVICE_NAME[]        = "Flowmeter";
static const uint16_t uuid16_list[]        = {GattService::UUID_CYCLING_SPEED_AND_CADENCE,
                                              GattService::UUID_DEVICE_INFORMATION_SERVICE};

uint32_t c;
uint32_t r;
uint32_t irritime = 100;
float nextWheel = 0;
uint16_t runtime;
const static int radius = 9;
uint32_t nextCrank = 0;
static volatile bool  triggerWheel = false;
static volatile bool  triggerCrank = false;
int tim;
void disconnectionCallback(Gap::Handle_t handle, Gap::DisconnectionReason_t reason)
{
    ble.gap().startAdvertising(); // restart advertising
}


void onTick(void)
{
    c++;
    r++;
    if (c >= nextWheel)
    {
        triggerWheel = true;
      //  nextWheel += 7 + (rand() % 10);
    }
    
    if (r >= nextCrank)
    {
        triggerCrank = true;    
       // nextCrank += 8 + (rand() % 20);
    }
}

int main(void)
{
    Ticker ticker;
    ticker.attach(onTick, 0.5);
   

    ble.init();
    ble.gap().onDisconnection(disconnectionCallback);

    /* Setup primary service. */
    float rpm = r; // init Wheel to 100revs
     float total; // init crank to 10revs
   
     // init crank to 10revs
    CyclingSpeedAndCadenceService cscService(ble,
        CyclingSpeedAndCadenceService::MODE_SPEED_CADENCE,
        CyclingSpeedAndCadenceService::LOCATION_CHAINSTAY);

    /* Setup auxiliary service. */
    DeviceInformationService deviceInfo(ble, "ROB", "Model1", "SN1", "hw-rev1", "fw-rev1", "soft-rev1");

    /* Setup advertising. */
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE);
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::CYCLING_SPEED_AND_CADENCE_SENSOR);
    ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));
    ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);
    ble.gap().setAdvertisingInterval(10000); /* 1000ms */
    ble.gap().startAdvertising();
    
    //nextWheel = 7 + (rand() % 10);
    nextCrank = 8 + (rand() % 20);

    // infinite loop
    while (1) {
         if(timer.read()==0){
         timer.start();
         }
        // check for trigger from periodicCallback()
        if (ble.getGapState().connected)
        {
            if (triggerCrank && triggerWheel)
            {
                //wait(20);
                 t.reset();
                t.start();
                r=0;
                while(t.read_ms() < 1000) {
                    ;
                }
                t.stop();
              // long int rpm = r;
                for(int i =0; i<=5; i++) {
                    rpm = r+rpm;
                }
            
               runtime =  irritime + timer.read();               
                //timer.reset();}
                cscService.updateCounters(((rpm/6)*60)* radius,total,runtime);
                triggerWheel = false;
                triggerCrank = false;
                rpm = 0;
                if(timer.read()==59){
                    irritime = irritime + 100;
                timer.reset();
                }
                
            }
           /* else if (triggerWheel)
            {
                uint16_t when = (timer.read() * 1024);
                cscService.updateWheelCounter(++wheelCounter, when);
                triggerWheel = false;
            }
            else if (triggerCrank)
            {
                uint16_t when = (timer.read() * 1024);
                cscService.updateCrankCounter(++crankCounter, when);
                triggerCrank = false;
            }*/
            else
            {
                ble.waitForEvent(); // low power wait for event
            }
        } else {
            ble.waitForEvent(); // low power wait for event
        }
    }
}

Question relating to:

1 Answer

7 years, 7 months ago.

The actual logic is here: https://developer.mbed.org/users/tenfoot/code/BLE_CycleSpeedCadence/file/7e334e81da21/CyclingSpeedAndCadenceService.h. Look at the definitions of GattCharacteristic, these are the actual values. By copying the lines where the characteristics are created and where they are added to the characteristic table, you should be able to figure out how to add a new variable that is broadcasted over BLE.

But - and don't take me wrong - if you do not have experience building BLE devices, I'd strongly suggest first reading up on BLE & mbed, before trying to change someone else's program. Here's some docs: https://docs.mbed.com/docs/ble-intros/en/v1.0/.

Accepted Answer