WriteValue Method

Write the value of a characteristic or descriptor.

Syntax

ANSI (Cross Platform)
int WriteValue(const char* lpszServiceId, const char* lpszCharacteristicId, const char* lpszDescriptorId, const char* lpValue, int lenValue);

Unicode (Windows)
INT WriteValue(LPCWSTR lpszServiceId, LPCWSTR lpszCharacteristicId, LPCWSTR lpszDescriptorId, LPCSTR lpValue, INT lenValue);
#define MID_BLECLIENT_WRITEVALUE 17

IPWORKSBLE_EXTERNAL int IPWORKSBLE_CALL IPWorksBLE_BLEClient_Do(void *lpObj, int methid, int cparam, void *param[], int cbparam[], int64 *lpllVal);

Remarks

This method is used to write the value of a characteristic or descriptor. The method will return and the WriteResponse event will fire if the write is a success. If the write fails, the Error event will fire and the class fails with an error.

To write the value of a characteristic, pass the Ids of it and its owning service for the CharacteristicId and ServiceId parameters, and pass empty string for the DescriptorId parameter. To write the value of a descriptor, do the same thing, but pass the descriptor's Id for the DescriptorId parameter too.

If your use-case doesn't require confirmation of characteristic value writes, prefer the PostValue method in order to reduce power consumption.

Writing Values

// WriteResponse event handler.
bleclient1.OnWriteResponse += (s, e) => {
  if (string.IsNullOrEmpty(e.DescriptorId)) {
    Console.WriteLine("Successfully wrote to characteristic with UUID " + e.Uuid);
  } else {
    Console.WriteLine("Successfully wrote to descriptor with UUID " + e.Uuid);
  }
};

// Write to the Luxometer Config characteristic. These Ids are correct
// for our CC2650STK TI SensorTag, but yours might differ.
bleclient1.WriteValue("004200000000", "004200460000", "", new byte[] { 0x1 });

// Write to the Client Characteristic Configuration descriptor on the
// Luxometer Data characteristic. Again, your Ids might differ.
bleclient1.WriteValue("004200000000", "004200430000", "004200430045", new byte[] { 0x1 });

Error Handling (C++)

This method returns a result code; 0 indicates success, while a non-zero error code indicates that this method encountered an error during its execution. If an error occurs, the GetLastError() method can be called to retrieve the associated error message. (Note: This method's result code can also be obtained by calling the GetLastErrorCode() method after it returns.)

Copyright (c) 2022 /n software inc. - All rights reserved.
IPWorks BLE 2020 C++ Edition - Version 20.0 [Build 8158]