FSM – a simple implementation
We will jump straight into an example of an FSM
handling Bluetooth Low Energy (BLE) device connection states, analyze its shortcomings, and see how we can improve it using the State design pattern.
The example FSM
will be simplified for the purpose of clarity and easier understanding. We will have three states – idle
, advertising
, and connected
. Here is a state diagram of the example FSM
:

Figure 16.1 – BLE device connection state diagram
Figure 16.1 depicts the state diagram of the BLE device connection FSM
. The diagram depicts transitions between states and actions described as follows:
- The default state is
idle
. It transitions to theadvertising
state on able_button_pressed
event. During the transition, thestart_advertising
action is executed. In simple words, this means that if the device is in anidle
state and a user presses a designated button, it will start advertising and change state. - From...