preface

I have been doing Bluetooth development for one year unconsciously, from BLE (low power) Bluetooth at the beginning to OTA of Bluetooth in the cloud and now to Bluetooth Mesh. The purpose of this article is to record and summarize the bluetooth Mesh development process and some problems encountered in the development, as well as to provide some help to those who are developing iOS Bluetooth Mesh.

Bluetooth Mesh concept

I think you have already been familiar with the concept of Bluetooth Mesh. After all, there are a lot of CV concepts on Baidu, so I won’t go into them here. If you haven’t already, take a look at the Bluetooth Mesh concept here.

How to develop Bluetooth Mesh for iOS?

Using third-party libraries, I recommend Nordic’s ios-NRF-Mesh-Library on GitHub. The second version of this library is now complete, so there is no need to worry about how disgusting the first version is 🤮. Although the second version is much better than the first version, there may be some problems in using it:

  • This library uses the swift language development, for the use of OC development may not be very friendly, mixed up or familiar with swift should know that OC content is basically swift has been able to support, but there are some things in Swift OC can not support, such as structs, enumerated types.
  • The swift framework supports OC by adding the @objc keyword in front of public classes or methods. Unfortunately, this library does not.

Therefore, if the developer is familiar with swift language, it is recommended to use SWIFT to develop Bluetooth Mesh, while OC development will be necessary to change the third-party library code. The specific use of your own choice.

What does iOS do to develop Bluetooth Mesh? How to do?

This is very important. When you first started to do a Bluetooth Mesh, you couldn’t find anything about iOS Bluetooth Mesh on Baidu. What you could find was a bunch of CV’s explaining what a Bluetooth Mesh is and what it does. What do we need to do with iOS and how do we do it? But don’t worry, after reading this article, you will know what iOS needs to do when it comes to bluetooth Mesh and how to get started.

Again, the concept

  • Node: The so-called node is actually a Bluetooth device, which was called device before the network configuration, and changed to node after the configuration in the Mesh network. As for the characteristics of the node, it is not up to us to make iOS, it depends on how the hardware engineers burn the code.

  • Four nodes: relay node, proxy node, low power node, friend node. But as iOS developers, we don't have to worry about that much, just worry about proxy nodes, because basically all the commands we send from our phones are forwarded through proxy nodes.

  • Network creation: To create a Mesh, initialize some network configuration information, such as the unique network identifier (meshUUID) and message transfer key (AppKeys). As for where to save the information after creation, it depends on the actual situation. The third-party library is saved locally by default. When we did this, we saved the network information in the cloud, which is convenient to bind users to the Mesh network.

  • Mobile phones in the role of bluetooth Mesh: the Mesh network in the main role is to configure the device, the second mobile phone can also be regarded as a special node in the Mesh, because mobile phones can be any one node within and network communication, particularity is reflected in the mobile phone only when connected to the agent node and other nodes communication.

  • Network configuration: Network configuration refers to a series of operations. The purpose of these operations is to add a Bluetooth device that supports bluetooth Mesh to the Mesh network created by mobile phones.

We know the concept of the distribution network above, and then we will talk about the specific content of the distribution network. The distribution network is generally divided into 5 steps:

  • Step 1: The device that supports Bluetooth Mesh has a service whose UUID is 1827 before it is configured to connect to the network. This service has two features, one is read and one is write. Bluetooth devices continuously send broadcast packets for the phone to search. Mobile phones can scan bluetooth devices with 1827UUID through the bluetooth scanning method provided by the iOS system library CoreBluetooth, establish a connection after scanning, find service features, and subscribe to bluetooth devices to read features.
  • Step 2: On the basis of step 1, send an invitation message to the Bluetooth device to join the Mesh. After receiving the invitation message, the device sends its own information to the mobile phone, such as the number and type of elements, public key type, and encryption algorithm of the device.
  • Step 3: On the basis of step 2, since lang is in love and concubine is interested, then the next is to exchange tokens of love (public key).
  • The fourth step: the exchange of tokens of love, soon to be together, or to test the other party is not really better, after all, it is still too late to regret, so there is this step of random number authentication link, similar to the key confirmation.
  • The fifth step: after the certification, always let the new device has some points ah, it is necessary to allocate some data to the device, such as: device unicast address, netKey, keyIndex, ivIndex. Ha ha, finished discovered inside the Mesh network equipment distribution network is not only a device, but regret is late, mobile phone is a cheat philandering the feelings of men.

Having said the steps of network configuration, we don’t need to implement every step ourselves when we write the code of iOS, right? Of course not. There’s a third party library. The above third-party library encapsulates the 5 steps of network provisioning much better, the first step of scanning the device is written in the official demo, and the second step is to call the Identify method using the ProvisioningManager object. The next three steps are encapsulated in a method, the provision method in the ProvisioningManager class.

At this point, you have a general idea of what we iOS developers should be doing when developing bluetooth Mesh? To summarize, we basically need to do the following three things when developing Bluetooth Mesh:

  • Create a Mesh
  • Configure the nodes for the Mesh
  • Mobile phones send messages or commands to nodes in the Mesh.

In general, there are three things we want to do with iOS. Are you asking me why I just said the first two points and didn’t tell you how to do it? Don’t worry, let me slow down. I have more to say about the news in the next article.