A little help with this code, if you'd be so kind?

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.
DeCo_1926091
Level 4
Level 4
First like received

If anyone has a few minutes to look over the attached code and see what I may be doing wrong it would be a great help to me, and, perhaps, keep me from going entirely insane.

There is also a file attached that describes the exact problems I'm seeing.

Thanks for your time and assistance.

Dennis

0 Likes
1 Solution

Hello,

BLE component will be in a single state at a time and CyBle_GetState() API will return the current state. Please see below explanation for CyBle_GetState() from BLE component datasheet.

The component is in the state CYBLE_STATE_INITIALIZING after CyBle_Start() function is called and until

CYBLE_EVT_STACK_ON event is not received. After successful initialization the state is changed to

CYBLE_STATE_DISCONNECTED. For GAP Peripheral role if CyBle_GappStartAdvertisement() is called and

CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP event received the state is changed to the

CYBLE_STATE_ADVERTISING. For GAP Central role if CyBle_GapcStartScan() API function is called and

CYBLE_EVT_GAPC_SCAN_START_STOP event received the state is changed to the

CYBLE_STATE_SCANNING. When CyBle_GapcConnectDevice() is called the state is changed to

CYBLE_STATE_CONNECTING. After successfully connection indicated by

CYBLE_EVT_GAP_DEVICE_CONNECTED or CYBLE_EVT_GAP_ENHANCE_CONN_COMPLETE event the

state is changed to CYBLE_STATE_CONNECTED. If CyBle_GapDisconnect() API function is called and

EVT_GAP_DEVICE_DISCONNECTED event received the state is changed to the

CYBLE_STATE_DISCONNECTED. If CyBle_Stop() is called state of component is changed to the

CYBLE_STATE_STOPPED.

You have to implement below sequence of operations before calling CyBle_GapRemoveBondedDevice() to make sure that the device is not in connected state and not in advertising state.

  1. If BLE state is CYBLE_STATE_CONNECTED, call CyBle_GapDisconnect() API.
  2. Wait for EVT_GAP_DEVICE_DISCONNECTED event.
  3. if BLE state is CYBLE_STATE_ADVERTISING, call CyBle_GappStopAdvertisement() API.
  4. Wait for CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP event.
  5. Call CyBle_GapRemoveBondedDevice() API.

You can use flag variables to wait until you receive EVT_GAP_DEVICE_DISCONNECTED or CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP events after calling

CyBle_GapDisconnect() or CyBle_GappStopAdvertisement() respectively.

Thanks and Regards,

Sudheesh

View solution in original post

0 Likes
5 Replies
SudheeshK
Moderator
Moderator
Moderator
250 sign-ins First question asked 750 replies posted

Hello,

I reviewed your project. Please see my comments below.

1. The API CyBle_GapRemoveBondedDevice() will return error code "CYBLE_ERROR_INVALID_OPERATION" when you try to remove the bonded devices before disconnection.

2. As you can see in the API documentation the APIs CyBle_GappStopAdvertisement() and CyBle_GapDisconnect() are non-blocking functions. You have to monitor the corresponding events to know the successful completion of these APIs.

CyBle_GappStopAdvertisement(): Event CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP is generated after successful completion.

CyBle_GapDisconnect(): Event CYBLE_EVT_GAP_DEVICE_DISCONNECTED is generated after successful completion.

3. In your code, after calling CyBle_GapDisconnect() you are not waiting for CYBLE_EVT_GAP_DEVICE_DISCONNECTED event before calling CyBle_GapRemoveBondedDevice(). Please see the log messages below.

Screenshot.PNG

4. Please make the necessary changes in your code to wait for CYBLE_EVT_GAP_DEVICE_DISCONNECTED event after calling CyBle_GapDisconnect() API. Call CyBle_GapRemoveBondedDevice() only after you receive CYBLE_EVT_GAP_DEVICE_DISCONNECTED event. Same procedure applies for CyBle_GappStopAdvertisement() API also, wait for the event CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP after calling this API.

Please let me know the results after making above changes.

As this thread is related to another thread that you created (How to fix CYBLE_ERROR_INVALID_OPERATION ), we can continue our discussions in this tread. I will lock the other one. I hope it is OK with you.

Thanks and Regards,

Sudheesh

0 Likes

Hi Sudheesh,

As I understand it, CyBle_GetState will return the state of the BLE.   There are seven states listed (Stopped, Initializing, Connected, Advertising, Scanning, Connecting, Disconnected), of which, I think, only one will be returned when GetState is called.  If it's Advertising, it can't be stopped, initializing, connected or scanning, but it must then be either connecting or disconnected.  It seems like it must then be in two of the states at the same time, advertising and something else.   So what are we looking for before calling CyBle_GapRemoveBondedDevice() or CyBle_GapClearResolvingList()?

Thanks,

Dennis

0 Likes

Hello,

BLE component will be in a single state at a time and CyBle_GetState() API will return the current state. Please see below explanation for CyBle_GetState() from BLE component datasheet.

The component is in the state CYBLE_STATE_INITIALIZING after CyBle_Start() function is called and until

CYBLE_EVT_STACK_ON event is not received. After successful initialization the state is changed to

CYBLE_STATE_DISCONNECTED. For GAP Peripheral role if CyBle_GappStartAdvertisement() is called and

CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP event received the state is changed to the

CYBLE_STATE_ADVERTISING. For GAP Central role if CyBle_GapcStartScan() API function is called and

CYBLE_EVT_GAPC_SCAN_START_STOP event received the state is changed to the

CYBLE_STATE_SCANNING. When CyBle_GapcConnectDevice() is called the state is changed to

CYBLE_STATE_CONNECTING. After successfully connection indicated by

CYBLE_EVT_GAP_DEVICE_CONNECTED or CYBLE_EVT_GAP_ENHANCE_CONN_COMPLETE event the

state is changed to CYBLE_STATE_CONNECTED. If CyBle_GapDisconnect() API function is called and

EVT_GAP_DEVICE_DISCONNECTED event received the state is changed to the

CYBLE_STATE_DISCONNECTED. If CyBle_Stop() is called state of component is changed to the

CYBLE_STATE_STOPPED.

You have to implement below sequence of operations before calling CyBle_GapRemoveBondedDevice() to make sure that the device is not in connected state and not in advertising state.

  1. If BLE state is CYBLE_STATE_CONNECTED, call CyBle_GapDisconnect() API.
  2. Wait for EVT_GAP_DEVICE_DISCONNECTED event.
  3. if BLE state is CYBLE_STATE_ADVERTISING, call CyBle_GappStopAdvertisement() API.
  4. Wait for CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP event.
  5. Call CyBle_GapRemoveBondedDevice() API.

You can use flag variables to wait until you receive EVT_GAP_DEVICE_DISCONNECTED or CYBLE_EVT_GAPP_ADVERTISEMENT_START_STOP events after calling

CyBle_GapDisconnect() or CyBle_GappStopAdvertisement() respectively.

Thanks and Regards,

Sudheesh

0 Likes

HI Sudheesh,

That information enabled me to get it working.   Plus one other thing that I had overlooked, which I think is true.  Perhaps you can confirm that it is true or not.   i had been thinking of events as interrupts which, when they occur, would call the various CYBLE_EVT segments of code.  In fact (I think) they will not do anything until process events is called.  So looping with a while statement waiting for the event to occur won't work.  The solution, as you've indicated, is to use flags, or, maybe, to call process events inside the loop.

Thanks for all your help,

Dennis

0 Likes

Hello,

Yes, your understanding is correct. BLE events do not work as interrupts. You have to call the API CyBle_ProcessEvents() periodically to process all the pending events of BLE stack. Make sure you call this API at least once every interval 't' where:

1. 't' is equal to connection interval or scan interval, whichever is smaller, if the device is in GAP Central mode of operation, or

2. 't' is equal to connection interval or advertisement interval, whichever is smaller, if the device is in GAP Peripheral mode of operation.

Please refer PSoC Creator BLE component datasheet for more information.

Thanks and Regards,

Sudheesh

0 Likes