WriteCharacteristic using C# API with CY5677

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
lock attach
Attachments are accessible only for community members.
MuNa_4652896
Level 4
Level 4
First like received First like given

I am using the C# Application provided here at this thread

Re: CySmart API C# example and a small application for PSoC 6. My focus is on two different characteristics:

LEDPWM (Readable / Notifiable)  and EDLED (Readable / Writable)

LEDPWM is used to store a new PWM value every time SW2 is pressed. If notifications are enabled, the new value is pushed to the GATT Client.

EDLED is used to enable or disable the SW2 Interrupt. (0 = disabled, 1 = enabled)

Everything works correctly using CySmart App on Android.

Using the previously mentioned C# App provided, I was able to modify it to work with LEDPWM Characteristic as well as its CCCD, but for the EDLED, when I send a value, the application freezes till a BLE_ERROR_GATT_OPERATION_TIMEOUT is issued.

From the BLE_custom_config.c

static const cy_stc_ble_customs_t cy_ble_customs[0x01u] = {

    /* LEDServ service */

    {

        0x0010u, /* Handle of the LEDServ service */

        {

            /* LEDPWM characteristic */

            {

                0x0012u, /* Handle of the LEDPWM characteristic */

              

                /* Array of Descriptors handles */

                {

                    0x0013u, /* Handle of the ledCCCD descriptor */

                },

            },

            /* EDLED characteristic */

            {

                0x0015u, /* Handle of the EDLED characteristic */            

            },

        },

    },

};

From the C# App

Characteristic Declarations:

const ushort pwm_CharHandle = 0x0012; //1 byte

const ushort pwm_CCCDHandle = 0x0013; //2 byte

const ushort interrupt_CharHandle = 0x0015; //1 byte

Write Button

  

private void Write_Button_Click(object sender, EventArgs e)

        {

            AutoResetEvent sync = new AutoResetEvent(false);

            CyApiErr err = CyApiErr.OK;

            // Setup the descriptor write handler

            GATTClientEventCallback.CharacteristicWriteHandler = (CyConnectResult, status) =>

            {

                if (status != CyStatus.BLE_STATUS_OK)

                    err = new CyApiErr("Failed to Write: Reason: " + status.ToString());

                sync.Set();

            };

          

                // Initiate write descriptor request to the CCCD

            err = GattClient.WriteCharacteristic(new CyGattWriteInfo(interrupt_CharHandle, BitConverter.GetBytes(int.Parse(WriteTextBox.Text, System.Globalization.NumberStyles.HexNumber))));

           

                if (err.IsOK)

                {

                    sync.WaitOne();

                   

                }

                else

                {

                    MessageBox.Show ("Write Failed: Reason :"+err.ToString());

                }

        }

Event Listener

public override void OnCharacteristicWrite(CyGattWriteResult result, CyStatus status)

        {

            if (CharacteristicWriteHandler != null)

                CharacteristicWriteHandler(result, status);

        }

Thank you

0 Likes
1 Solution
DheerajK_81
Moderator
Moderator
Moderator
First comment on KBA First comment on blog 5 questions asked

Hello,

In your PSoC Creator project, the characteristic EDLED is of type uint8 and hence requires the attribute length of the data to be 1. In this line of code in your C# application, you are using int data type which is 4 bytes and will have attribute length of 4.

err = GattClient.WriteCharacteristic(new CyGattWriteInfo(interrupt_CharHandle, BitConverter.GetBytes(int.Parse(WriteTextBox.Text, System.Globalization.NumberStyles.HexNumber))));

If you send this data, you will get CY_BLE_GATT_ERR_INVALID_ATTRIBUTE_LEN error code and this particular if condition is going to fail and the updateIntStat function never gets called.

invalidBle.png

You can use either of these two methods to solve this problem:

  • Modify the EDLED characteristic to be of type 32bit in PSoC Creator project

     ble2.png

  • Modify the C# application to make use of byte data type

               byte writtenData = byte.Parse(WriteTextBox.Text, System.Globalization.NumberStyles.HexNumber);

               err = GattClient.WriteCharacteristic(new CyGattWriteInfo(interrupt_CharHandle, writtenData));

It should now work as expected!

Regards,

Dheeraj

View solution in original post

2 Replies
DheerajK_81
Moderator
Moderator
Moderator
First comment on KBA First comment on blog 5 questions asked

Hello,

In your PSoC Creator project, the characteristic EDLED is of type uint8 and hence requires the attribute length of the data to be 1. In this line of code in your C# application, you are using int data type which is 4 bytes and will have attribute length of 4.

err = GattClient.WriteCharacteristic(new CyGattWriteInfo(interrupt_CharHandle, BitConverter.GetBytes(int.Parse(WriteTextBox.Text, System.Globalization.NumberStyles.HexNumber))));

If you send this data, you will get CY_BLE_GATT_ERR_INVALID_ATTRIBUTE_LEN error code and this particular if condition is going to fail and the updateIntStat function never gets called.

invalidBle.png

You can use either of these two methods to solve this problem:

  • Modify the EDLED characteristic to be of type 32bit in PSoC Creator project

     ble2.png

  • Modify the C# application to make use of byte data type

               byte writtenData = byte.Parse(WriteTextBox.Text, System.Globalization.NumberStyles.HexNumber);

               err = GattClient.WriteCharacteristic(new CyGattWriteInfo(interrupt_CharHandle, writtenData));

It should now work as expected!

Regards,

Dheeraj

Thank you very much. Perfect Answer

0 Likes