Modify the name of a bluetooth device.
The VID app can modify the name of a bluetooth device that is currently connected to it to add personalized elements.
It is not difficult to modify the name of a bluetooth device. Follow the AT command specified in the Manual to send it to the bluetooth device through the serial port. However, it is not easy for a mobile phone to change the name of a bluetooth device through wireless communication. The reason is:
- Data sent by mobile phones in the AT command format will be transmitted to the microcontroller as common data content by bluetooth;
- When a bluetooth device is connected, the AT command cannot modify the device name.
Solution:
- The single-chip microcomputer must use serial ports to execute AT commands;
- The App sends a command containing the user-entered custom name information to the microcontroller, and then automatically cancels the Bluetooth connection;
- After interpreting the commands sent by the mobile phone, the MCU uses the AT command to change the name of the bluetooth device to the name entered by the user.
A problem occurred, which took two days:
After ensuring the correctness of the Microcontroller program,
The iOS source code is as follows:
-(Void) setDevice_Name { [Self initialize th_send: command]; // send a command with user-defined name information; If (sensor. activePeripheral. state = CBPeripheralStateConnected) {// determine whether the connection status is [Sensor. manager cancelPeripheralConnection: sensor. activePeripheral]; // cancel the connection Sensor. activePeripheral = nil; } } |
Ps: the code for Bluetooth connection and other related operations directly references the routine provided by the bluetooth module supplier, so you do not have to provide it.
Symptom: the chances of successfully modifying the name of a bluetooth device are extremely low!
Tracking the running of single-chip microcomputer programs found that the single-chip microcomputer did not receive commands sent from the mobile phone.
After the code is modified, the Code is as follows:
-(Void) setDevice_Name { [Self initialize th_send: command]; // send a command with user-defined name information; [NSTimer scheduledTimerWithInterval: 1.0f target: self selector: @ selector (disconnectBLE) userInfo: nil repeats: NO]; // after 1 s delay, execute disconnectBLE; } -(Void) disconnectBLE { If (sensor. activePeripheral. state = CBPeripheralStateConnected) {// determine whether the connection status is [Sensor. manager cancelPeripheralConnection: sensor. activePeripheral]; // cancel the connection Sensor. activePeripheral = nil; } } |
The name modification function is successfully implemented.
As a non-computer/app development professional, I cannot understand the reasons. I assume that the program does not cancel the connection after sending the command. Instead, the command is read to the sending buffer and sent in another thread, while the canceling operation is in synchronization, resulting in the failure to completely issue the command.
Hope to get advice from professionals.