bluetooth lowenergy - BLE: Add characteristic to a service (C++) -
i newbie in ble coding , have been struggling simple do.
i trying receive characteristic service specific uuid. experimenting ble_gap_example mbed.org.
by making slight modifications, able see device , uuid of service have assigned, unable add characteristic service.
what want achieve merely broadcast 2 values, using 2 services, each each own uuid , value. there no connection between devices.
the code simple , there no major differences sample:
#include "mbed.h" #include "ble/ble.h" /* optional: device name, add human read-ability */ const static char device_name[] = "g4"; static const uint16_t uuid16_list[] = {0x2334}; /* have 26 bytes of advertising data use. */ const static uint8_t advdata[] = {0x01,0x02,0x03,0x04,0x05}; /* example of hex data */ float measurement = 10;// want broadcast. /* optional: restart advertising when peer disconnects */ void disconnectioncallback(const gap::disconnectioncallbackparams_t *params) { ble::instance().gap().startadvertising(); } /** * function called when 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 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 error handling onbleiniterror */ onbleiniterror(ble, error); return; } /* ensure 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 disconnection */ ble.gap().ondisconnection(disconnectioncallback); /* sacrifice 3b of 31b advertising flags */ ble.gap().accumulateadvertisingpayload(gapadvertisingdata::bredr_not_supported | gapadvertisingdata::le_general_discoverable ); ble.gap().setadvertisingtype(gapadvertisingparams::adv_connectable_undirected); /* sacrifice 2b of 31b advtype overhead, rest goes advdata array define */ ble.gap().accumulateadvertisingpayload(gapadvertisingdata::manufacturer_specific_data, advdata, sizeof(advdata)); ble.gap().accumulateadvertisingpayload(gapadvertisingdata::complete_list_16bit_service_ids, (uint8_t *)uuid16_list, sizeof(uuid16_list)); //ble.gap().accumulateadvertisingpayload(gapadvertisingdata::service_data, measurement, sizeof(measurement));//when uncomment line cannot see device anymore phone /* optional: add name 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); /* initialize ble baselayer, first! */ ble.init(bleinitcomplete); /* infinite loop waiting ble events */ while (true) { /* save power while waiting callback events */ ble.waitforevent(); } }
i thoughtful on how change value of measurement dynamically in code.
thank in advance help.
Comments
Post a Comment