Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 11 months ago.
Why ble.gattServer().read() is not working with X-NUCLEO-IDB04A1?
Hi, I was trying to read a ReadWrite chacteristic value from the gattServer defining the function " readFirstValue()" and using it, but I read random values.
I looked inside the BLE_API wrapper library for STMicroelectronics' BlueNRG Bluetooth Low Energy expansion board shield written by ST and it seems that this function "read" for the gattServer is not implemented. How can I read a characteristic value using the X-NUCLEO-IDB04A1?
Thanks
buttonService with ReadWrite characteristic
#ifndef __BLE_BUTTON_SERVICE_H__ #define __BLE_BUTTON_SERVICE_H__ Serial pc(USBTX, USBRX); class ButtonService { public: const static uint16_t BUTTON_SERVICE_UUID = 0xA000; /* define uuid for the service*/ const static uint16_t BUTTON_STATE_CHARACTERISTIC_UUID = 0xA001; /* define uuid for the charcateristic "state" of the service */ const static uint16_t BUTTON_FIRST_VALUE_CHAR_UUID = 0xA002; ButtonService(BLE &_ble, bool buttonPressedInitial, uint8_t initialFirstValue) : ble(_ble), buttonState(BUTTON_STATE_CHARACTERISTIC_UUID, &buttonPressedInitial, GattCharacteristic::BLE_GATT_CHAR_PROPERTIES_NOTIFY),firstValue(BUTTON_FIRST_VALUE_CHAR_UUID, &initialFirstValue) /* constructor of the class buttonService which inherits ble (which is an objet of class BLE), buttonState ( which is an object of the class ReadOnlyGattCharacteristic). When it inherits it also initializes the class with the proper parametes */ { GattCharacteristic *charTable[] = {&buttonState, &firstValue}; /* prepare the characteristic table for the service that we are going to initialize */ GattService buttonService(ButtonService::BUTTON_SERVICE_UUID, charTable, sizeof(charTable) / sizeof(GattCharacteristic *)); /* Create the gatt service called buttonServices with uuid as previously defined, characteristics table just defined and the number of elements of the char table */ ble.gattServer().addService(buttonService); /* Add to the ble gattServer the service just defined */ } void updateButtonState(bool newState) { ble.gattServer().write(buttonState.getValueHandle(), (uint8_t *)&newState, sizeof(bool)); /* The attribute handle is a unique 16 bit identifier for each attribute on a particular gatt server. It the part of each attribute which makes it addressable and it is guaranteed not to change */ /*new state is a bool so sizeof(bool)*/ } int readFirstValue() { uint8_t buffer[10]; int i; uint16_t lenght=sizeof(uint8_t); pc.printf("\nla lenght prima e %d\n ", (int)lenght); ble.gattServer().read(firstValue.getValueHandle(), buffer, &lenght); for(i = 0; i< 10; i++) { pc.printf("%d ", (int)buffer[i]); } pc.printf("\nla lenght e %d\n ",(int) lenght); pc.printf("\r\n"); return ((int) buffer[0]); } private: BLE &ble; ReadOnlyGattCharacteristic<bool> buttonState; ReadWriteGattCharacteristic<uint8_t> firstValue; }; #endif /* #ifndef __BLE_BUTTON_SERVICE_H__ */
Question relating to:
1 Answer
8 years, 11 months ago.
Dear Gabriele,
thanks for your feedback! We just updated the X-NUCLEO-IDB0XA1 with the API allowing to read the Char value from the local Gatt Server.
Please, check out the latest version of the library.
Kind regards