About iOS Bluetooth development two or three things

Source: Internet
Author: User

iOS Bluetooth fast development One, background

Recently, due to the company a set of Bluetooth device updates, communication protocols need to be modified, the function is also perfect, so need to update the app. The pit daddy is, this app has been developed to now has a year time, out of source code and app nothing. Helpless, the higher level of the task is difficult to do. Spent about three days familiar with the whole project, because the main function of the app is to interact with the company's supporting equipment, so, the interface on the things I have been around, the main understanding of Bluetooth interactive content.

After careful understanding, I found that the developer of this app is also the best, do not know the basic knowledge of C, or do not use, the entire Bluetooth interactive data all use string manipulation, which is really not for a developer. Some data headers still write dead in the string! No way, refactoring! So there's this article.

Second, Bluetooth

Bluetooth technology has developed to the present 4.0, which is often said low power Bluetooth. Bluetooth 4.0 is the latest Bluetooth version of 2012, is a 3.0 upgrade version, more than 3.0 version of power saving, low cost, 3 milliseconds low latency, long effective connection distance, AES-128 encryption, etc., usually used in Bluetooth headphones, Bluetooth speakers and other devices.

The above introduction from Baidu, we are relative Bluetooth specific understanding, please Google, here I mainly introduce my experience of this refactoring.

Bluetooth is a long link in the socket, follow the TCP/IP protocol, ios-Corebluetooth Framework has helped us do the internal package, we do not need to know the connection device, data transmission and other related details, only need to customize the application layer of the communication protocol. Of course, this protocol is compatible with the protocol on the device and is generally a private protocol. Protect equipment on the one hand and avoid malicious use on the other.

Third, communication protocol

Now, to do more and more intelligent home, the inevitable use of Bluetooth, but, Bluetooth communication has its own private protocol, this Protocol in my understanding is the way data is arranged. Sorting commands and data in a certain format, plus verification is the main part of the communication protocol. Generally speaking, this agreement is divided into two parts, part is the data head, part is the data problem. Header frame, command, check and so on, the data body is the specific data and data validation. Of course there may not be a calibration, which needs to be negotiated with the device developer.

Iv. sending data and receiving data

  The Bluetooth packet can only contain 20 bytes of data, and then it will be sub-package, this need attention. Many times we need to send a lot of data to the device, which requires subcontracting, this is the problem we need to be vigilant. Accept the same time, the data length of more than 20 bytes will be waiting for the next read, splicing data, so that the data obtained is complete. Of course, you also need to verify that the data is correct, and avoid receiving incorrect data to interfere with the app.

Because the data are all bytes, it is cumbersome to operate, whether it is sending or receiving is a torment for some friends who are not good at C. How to become simple? The answer is structure! Yes, it's a lot easier to wrap data in a structure, whether it's sending data or accepting data, and using structs to manipulate it.

We can follow the communication protocol to build our structural data structure, although sometimes a lot of structure, it doesn't matter you can split the data structure, the same data structure can not be duplicated. malloc plus memset (or calloc) initializes the struct and assigns the data, noting that the order of the data in the struct needs to match the communication protocol. In this way, we can easily create a send data, and then we convert the data into NSData, now is the domain of OC. Concrete how to cut data, splicing data and so on to use NSData to solve.

Accept the data is the same reason, it is necessary to note that we need to put the received data directly in the NSData, Long data need to be stitched into the nsmutabledata inside. Then use this data to initialize the structure to accept the data, and then from the structure of the data, if the data is correct, you can use a model to receive the data, and then how to use the data is OC content. This is much like the data-to-model we used to say.

Finally, it is necessary to note that the general external equipment may be small-end communication, but also the use of big-endian communication, the structure of the package after the NSData after the transfer of more than two bytes of data are small, so we have to be wary of this.

end of the five.

No more! Want to see the code of the small partner is not up, because the project also needs secrecy, engineering management is very strict, so it is not affixed. But I did, and one day I wrote the whole Bluetooth interaction module. I hope my experience will be useful to everyone.

About iOS Bluetooth development two or three things

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.