It doesn’t matter if you’ve been working for three years, five years, if you haven’t been exposed to peripherals like Bluetooth, NFC. Then this article will open the door to a new world.

In the usual work, we are through the way of adjusting the interface and back-end data interaction, then if there is no back-end, how is the front end and hardware directly data interaction?

When thinking clearly about this problem, will use our university to learn a course “computer network foundation”, many people do not think of it, the front need to learn computer network? The answer is yes, or at least yes. In fact, this question is just like the college entrance examination, when a lot of people questioned “do we need to use math to buy food in the future?”

I remember that I read a saying that you have to wait at least seven or eight years before you realize the meaning of what you learn in college.

As we all know, now is the front and back end separation era, whether it is the front end or the back end, we face the work every day is to adjust the interface. But have you ever wondered how he sets up a connection with us? What is the nature of their communication with each other?

So that’s a common interview question, and it’s often asked on both the front end and the back end. ① How many types of HTTP requests are there? What’s the difference? ② Three waves and four handshakes. Can you tell me?

First point: you don’t think there are only two types of HTTP requests: GET and POST. There are actually 8 types of HTTP requests, and I only use get, POST, PUT and DELETE in my work. Number two: You may be wondering why the interviewer is asking such a boring question that you don’t normally need to ask, but the interviewer isn’t asking you to write an HTTP protocol by hand. He’s just asking you to know how much you know about HTTP.

Again, you may be thinking, these are just some interview questions to memorize, but you still won’t need them in the workplace. Let me ask you again, have you ever been exposed to a peripheral like Bluetooth? Without a back end, how does the front end interact directly with the hardware? Have you done any work related to network security?

It doesn’t work because you’re stuck in an ivory tower of HTTP. University “computer network foundation” there is an OSI seven layer model and TCP/IP four layer model, presumably we all have some impression of it.

There are many protocols involved in this, and the HTTP protocol that we usually use for the front and back end call interface is just one of them. There are many protocols in OSI, and each protocol has a different role to play. So the course “Fundamentals of Computer Networks” is involved in our work, but it is weakened, weakened to the point that no one notices its existence, but you can not completely not understand it, that is why the interviewer asked, but there is almost no practical reason for the work. It gives us more of a big picture, and as we work, we understand more about the world of the Internet, the world of zeros and ones.

Get down to business:

Small program Bluetooth unlock can be summarized into the following five problems: a: how to connect bluetooth? Two: how to transmit data? Three: how to subcontract? Three: How to receive data? Four: How to pack?

One: How to connect bluetooth?

How to connect bluetooth, actually there is a demo in the official wechat document, you can download it directly to view. Developers.weixin.qq.com/miniprogram…

Basically, it’s about initializing the Bluetooth device, the equivalent of the bluetooth life cycle, connecting to bluetooth. And then you have two apis. One for sending data, one for receiving data. I won’t talk about the details, but you can pull down the code and look at the API related to wechat documents. You can figure it out if you spend half a day sorting out your ideas. The demo provided by wechat is native. How about the other frameworks and apis used in your project, just change their names. The apis of other frameworks are the same as wechat, but a few apis are different in data processing. I’m using the UNI-PP framework here.

Here are some renderings of my Bluetooth connection: ① : Search to nearby Bluetooth.

② Click the Bluetooth to select. You can perform some of the operations shown in the figure.

Two: how to transmit data?

So we have the front end and the back end through interfaces, which is HTTP, and the front end and the back end through protocols, but the protocol here is not HTTP, it’s BLE. Wechat applet also gives us corresponding API.

A COPY of BLE communication protocol is attached as follows:

This protocol must be simple for the hardware guys, but for our front end, it’s really hard to understand. Hahaha, I was confused when I first saw this protocol. Later, after communicating with hardware engineers, I finally understood.

To concatenate data packets, use a new ArrayBuffer, and then add data to it with the DataView. So this is a pretty neat way of adding data to the ArrayBuffer with the DataView, and don’t think I made a mistake, but that’s it.

You can try to pass a character first and see if the hardware engineers can receive it. Make sure you can communicate.

However, a protocol is usually not one or two characters, 100-200 characters are normal, and you will fail to pass them directly. Because data transfer is larger than 20 bytes will lose the packet, all need to subcontract.

Iii. How to subcontract?

This is my encapsulation of the subcontracting method, in simple terms, is to split the data, and then send intervals to the hardware, let the hardware receive packets in batches.

In real mode, the console queries the transmitted data.

You’ll see that protocols are strings, ha ha ha. However, if no one gives you a point, you will have to study for a long time, because this is very different from my usual HTTP protocol, protocol this thing is agreed by both parties.

How to receive data?

Wechat also provides apis.

Five, how to pack?

Why do you want to combine packets, you send to the hardware is subcontracted, he sent to you, will also be subcontracted, so you need to put these packets together, and then process the relevant business logic.

Finally: this article is just for you to open ideas, understand the front-end and hardware how to communicate about the process, and so you really encountered, I hope this article can help you, in the enterprise, will also add checksum encryption in the protocol, is not as simple as this article oh, and this processing data is particularly troublesome.

Previous articles of the author:

What is the lifecycle execution flow for parent and child components?

One day, I found a bug in the Nuggets official.

8 ways for components to communicate, have you figured it out?