Modify the name of a bluetooth device.

Source: Internet
Author: User
Tags custom name

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.