Bluetooth Low Energy (BLE, Bluetooth Low Energy) serial port pass-through is widely used. On Apple's iOS platform, Classic Bluetooth requires MFi certification to connect with Apple's iOS devices. The Bluetooth low energy device does not have this restriction.
The protocol stack and principle of Bluetooth low energy will not be repeated here, there are many related articles and videos. In short, the bluetooth service is provided in the form of a profile, and there are N characters with independent IDs (UUID) under the profile of each service. Each character has different permissions (read, write, notify, indicate). After the user defines the character and combines it with the authority, a complete service can be provided.
What BLE pass-through is actually to establish a BLE Service, and there are 2 characters under this profile.
One for TX (transmit data) and one for RX (receive data). For this they have different permissions. The following code is to create a new service and character:
Next are two callback functions, which are performed when there is a connection and when there is a write RX character:
Finally, the main loop is the control of the connection, which determines whether there is a connection and whether it is disconnected.
For the complete code, see the example of the official library: ble_uart, and the debugging tool can use LightBlue.