7 years, 3 months ago.

How to update the characteristic of a GAP servise

Dear all,

I am fairly new to the BLE world and mbed.

I am trying to build a sensor that will just advertise a service or two and their characteristic.

The way I understand it the most suitable example for me is the BLE_GAP_EXAMPLE.

Playing a bit with it and tweaking the given code a bit I am able to read the advertisement and the characteristic that I am sending.

In the next step I want to be able to manipulate the advertisement package: meaning to be able to change the UUID services and the values of the characteristic.

In my understanding those things are done in the <<code title=include the mbed library with this snippet>> bleInitComplete <</code>> But I do not understand how it is called and how it is used in the code.

<<code title=include the mbed library with this snippet>>

  1. include "mbed.h"
  2. include "ble/BLE.h"

/* Optional: Device Name, add for human read-ability */ const static char DEVICE_NAME[] = "G4";

static const uint16_t uuid16_list[] = {0x2334, 0x2335};

/* You have up to 26 bytes of advertising data to use. */ const static uint8_t AdvData[] = {0x01,0x02,0x03,0x04,0x05}; /* Example of hex data */ const static uint8_t AdvData[] = {"ChangeThisData"}; /* Example of character data */

int meas = 0;

uint8_t measurement[] = {0x34,0x23, meas};

/* Optional: Restart advertising when peer disconnects */ void disconnectionCallback(const Gap::DisconnectionCallbackParams_t *params) { BLE::Instance().gap().startAdvertising(); } /**

  • This function is called when the ble initialization process has failed
  • / void onBleInitError(BLE &ble, ble_error_t error) { /* Avoid compiler warnings */ (void) ble; (void) error;

/* Initialization error handling should go here */ }

/**

  • Callback triggered when the ble initialization process has finished
  • / void bleInitComplete(BLE::InitializationCompleteCallbackContext *params) { BLE& ble = params->ble; ble_error_t error = params->error;

if (error != BLE_ERROR_NONE) { /* In case of error, forward the error handling to onBleInitError */ onBleInitError(ble, error); return; }

/* Ensure that it is the default instance of BLE */ if(ble.getInstanceID() != BLE::DEFAULT_INSTANCE) { return; }

/* Set device name characteristic data */ ble.gap().setDeviceName((const uint8_t *) DEVICE_NAME);

/* Optional: add callback for disconnection */ ble.gap().onDisconnection(disconnectionCallback);

/* Sacrifice 3B of 31B to Advertising Flags */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::BREDR_NOT_SUPPORTED | GapAdvertisingData::LE_GENERAL_DISCOVERABLE ); ble.gap().setAdvertisingType(GapAdvertisingParams::ADV_CONNECTABLE_UNDIRECTED);

/* Sacrifice 2B of 31B to AdvType overhead, rest goes to AdvData array you define */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::MANUFACTURER_SPECIFIC_DATA, AdvData, sizeof(AdvData)); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::SERVICE_DATA, measurement, sizeof(measurement)); ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LIST_16BIT_SERVICE_IDS, (uint8_t *)uuid16_list, sizeof(uuid16_list));

/* Optional: Add name to device */ ble.gap().accumulateAdvertisingPayload(GapAdvertisingData::COMPLETE_LOCAL_NAME, (uint8_t *)DEVICE_NAME, sizeof(DEVICE_NAME));

/* Set advertising interval. Longer interval == longer battery life */ ble.gap().setAdvertisingInterval(100); /* 100ms */

/* Start advertising */ ble.gap().startAdvertising(); }

int main(void) { BLE& ble = BLE::Instance(BLE::DEFAULT_INSTANCE);

for(int i = 0; i < 10; i++){ ble.gap().stopAdvertising(); for(int k = 0; k < 1000; k++); measurement[2]++; ble.init(bleInitComplete); } } <</code>>

The code that I have at the moment is the one above and what I noticed is that once the code gets into the for loop, it does not get out of it in order to run for 10 times.

Thank you for your help in advance.

Be the first to answer this question.