# Bluetooth Low Energy Mesh Network (BLE Mesh)
Bluetooth Low Energy Mesh Network (BLE Mesh) is built on theBluetooth Low Energy (BLE)Protocol based on a communication protocol that allows a large number of BLE Devices form a mesh network to achieve interconnection and cooperative control between devices in a large enough physical coverage area, so as to meet the communication needs of multi-device scenarios.
BLE Mesh Communication protocols support low power consumption, scalability, flexibility, high reliability, security and other outstanding features, in smart home, intelligent lighting, industrial automation, medical health, environmental monitoring and other fields has a wide range of application value.
# 1. Basic concept
# BLE Mesh network
BLE Mesh A network is a many-to-many network topology in which device nodes pass throughrelease / Subscription mechanismSend and receive messages.
# Equipment distribution network
Undistributed network BLE Mesh Equipment needs to complete the distribution network operation before joining the BLE Mesh The network and becomes a device node that communicates messages with other nodes in the network. help BLE Mesh Equipment The equipment that completes the distribution network operation is calledStart configuration device。
# Device node
Undistributed network BLE Mesh Equipment through the distribution network operation, it becomes the BLE Mesh Device nodes in the network. Device nodes generally have one or more of the characteristics of relay, proxy, friend, low power consumption and so on.
The device node is composed of multiple elements, each element contains multiple models, and each model defines the basic functions of the node, such as the state required by the node, the message of the control state, and the action generated by the processing of the message. The implementation of node functions is model-based. The model can be divided into SIG Model and a custom model, the former by the SIG Definition, while the latter is defined by the developer. Model may also be based on the sending of messages / The receiver is divided into client model and server model.
# Mesh address
BLE Mesh If the device nodes in the network want to communicate with each other, they need to assign an address to each node for sending and receiving messages. Mesh Address is divided into unicast address, multicast address, virtual address three.
The unicast address is assigned by theStart configuration deviceAssigned. Unicast address may appear at the source of the message / In the destination address field. Messages sent to a unicast address can only be processed by the element that owns the unicast address.
The multicast address is BLE Mesh A multicast address in a network, usually used to group device nodes. If you send a message with a multicast address, all device nodes that have subscribed to the multicast address will receive the message.
The virtual address is associated with a specific UUID Label that can be used as a publication or subscription address for the model.
# Mesh news
Mesh Messages are divided into control messages and access messages. The control message is associated with the BLE Mesh Messages related to network operations, such as heartbeat and friend request messages. Access messages allow the client model to retrieve or set status values in the server model, or are used by the server to report status values.
Mesh The message is BLE Mesh The basic unit of data transmission in the network, by the operation code (opcode) and carry parameters (parameters), the former is used to identify the purpose of the message unique, which can store valid data, such as destination address, device state, etc.
# Agent equipment
If you want to not BLE Mesh Other devices of the device (such as a mobile phone) can also become BLE Mesh A member of the network that can be performed by communicating with a proxy device node GATT Connection, implemented with the aid of a proxy device in the BLE Mesh Sending and receiving messages in the network.
# 2.Mini Program BLE Mesh plug-inUse Process
In the Mini Program, based on the standard BLE Mesh Communication protocol, in the form of an Mini Program plug-in provides BLE Mesh The basic capabilities. Developers can useMini Program BLE Mesh plug-inRealization BLE Mesh The local rapid distribution network, control management, network sharing and other functions. The platform providesInterface documentandExample Mini ProgramEasy access for developers.
# 2.1 Access plug-in
Before using the plug-in, you first need to open the Mini Program management backgroundSet up - Third-Party Services - Plugin managementAdd plug-ins, developers can log in to the Mini Program management background, through the appid - wx013447465d3aa024 Find the plugin and add it. And then in app.json
File to declare the specified version of the plug-in. On the Mini Program plug-in specific introduction can refer toUsing plugins。
So far, only applications.toolEquipment managementService category Mini Program to useMini Program BLE Mesh plug-in。
"plugins": {
"ble-mesh-plugin": {
"version": "latest",
"provider": "wx013447465d3aa024"
}
}
adopt requirePlugin
Method to obtainMini Program BLE Mesh plug-inThe relevant interface. For example, it can be called as follows:
const { getMeshBLEManager } = requirePlugin('ble-mesh-plugin')
# 2.2 to initialize BLE Mesh Bluetooth Configurator
Using Small Programs BLE Mesh Before the relevant capabilities, you need to initialize BLE Mesh Bluetooth configurator for scanning to find the surrounding BLE Mesh Equipment.
const meshBLEManager = getMeshBLEManager ()
meshBLEManager.init().then(({ enabled }) => {
if (!enabled) {
wx.showModal({
title: "Error,"
content: 'Bluetooth is not enabled, Please turn on Bluetooth and try again ',
showCancel: false,
})
}
})
# 2.3 Scan found BLE Mesh equipment
BLE Mesh After the Bluetooth configurator is initialized, you need to go through the meshBLEManager.startScanMeshDevice
scanning BLE Mesh Device. If the device is scanned nearby, it will call back meshBLEManager.onMeshDeviceFound
Event, returns the scan to the BLE Mesh Device instance. Because the scanning device operation is more expensive system resources, please call in time after searching for the required equipment. meshBLEManager.stopScanMeshDevice
Stop scanning.
meshBLEManager.onMeshDeviceFound((res) => {
// The scan. BLE Mesh equipment
console.log(res.device)
// When you find the equipment you need, stop scanning.
meshBLEManager.stopScanMeshDevice()
})
// Start scanning. BLE Mesh equipment
meshBLEManager.startScanMeshDevice({
allowDuplicatesKey: false,
})
# 2.4 create BLE Mesh network
Want to implement multiple BLE Mesh Interconnection and collaborative control between devices, it is necessary to first create a BLE Mesh Network.
createMeshNetwork({ name: 'mesh_network' }).then((res) => {
// create BLE Mesh After the network, the unique network identifier ID will be generated, which is used for the follow-up network management, equipment distribution network, equipment message communication and other functions.
console.log(res.networkId)
})
# 2.5 to share BLE Mesh network
Developers can use exportMeshNetworks
and importMeshNetworks
Realization BLE Mesh Import and export of network data, so that multiple Mini Program users can control and manage the same network under the same BLE Mesh Equipment. In addition to calling getMeshNetworks
You can also get all the currently existing BLE Mesh Network list.
// to derive BLE Mesh Network data
const { restoreData } = exportMeshNetworks({ data: [{ name: 'mesh_network' }] })
// to introduce into BLE Mesh Network data
importMeshNetworks({ restoreData })
// Gets the currently existing BLE Mesh List of networks
const networks = getMeshNetworks() // [{ name: 'network_name', networkId: 'network_id' }]
# Be careful
In passing the exportMeshNetworks
to derive BLE Mesh After the network data, it may be necessary to persist the data. To ensure network security, developers should perform data encryption and storage operations, and then decrypt the data at import time.
In passing the importMeshNetworks
will BLE Mesh When the network data is imported into the new user Mini Program, because the iOS System restrictions, the same BLE Mesh Devices, different users get the Bluetooth deviceId Different, resulting in the inability to establish a proxy device connection. At this time, the device side needs to continue broadcasting after the successful distribution network. Node Identity
Bluetooth broadcast package to facilitate Mini Program plug-in device node identification, update Bluetooth deviceId 。
# 2.6 BLE Mesh network management
adopt getMeshNetworkManager
Can obtain BLE Mesh Network management configurator, which includes the creation of / delete BLE Mesh Groups, Get BLE Mesh Unicast address list and multicast address list of all nodes in the network.
// Obtain BLE Mesh Network Management Configurator
const meshNetworkManager = getMeshNetworkManager({ networkId: 'network_id' })
// create BLE Mesh group
const group = meshNetworkManager.createGroup({ name: 'group_name' })
// delete BLE Mesh group
meshNetworkManager.removeGroup({ name: 'group_name' })
// Get the Multicast Address List
const groups = meshNetworkManager.getGroups()
[{
name, // BLE Mesh Group name
address, // BLE Mesh Multicast address
nodes, // BLE Mesh Device node information bound under multicast address
}]
// Get the unicast address list
const unicasts = meshNetworkManager.getUnicasts()
[{
deviceId, // BLE Mesh Device id
name, // BLE Mesh Name of equipment
local // BLE Mesh In the device broadcast data segment Local data segment
address, // BLE Mesh Device unicast address
}]
# 2.7 BLE Mesh Equipment distribution network
Want to achieve BLE Mesh Equipment distribution network, you first need to obtain BLE Mesh Device Access Configurator provisioningManager
, and then through the provisioningManager.provision
Complete the distribution network operation. It can also be done through provisioningManager.batchProvision
Achieve a one-click distribution network with multiple BLE Mesh Equipment. when BLE Mesh After the equipment completes the distribution network operation, it is really joined BLE Mesh Network.
// Obtain BLE Mesh Device Access Configurator
const provisioningManager = getProvisioningManager ({ networkId: 'network_id' })
// single BLE Mesh Equipment distribution network
await provisioningManager.provision({
unprovisionedDevice, // Unwired BLE Mesh Examples of equipment
authenticationMethod: AuthenticationMethod.NoOOB, // At the time of distribution OOB Security authentication method
})
// many BLE Mesh Equipment batch distribution network
await provisioningManager.batchProvision({
unprovisionedDevices, // Unwired BLE Mesh Array of Device Instances
})
# 2.8 BLE Mesh Device message communication
In completing the BLE Mesh After the equipment is distributed, if you want to communicate with the equipment, you can control the equipment and obtain the equipment information. The first thing to do is get BLE Mesh Proxy device client configurator proxyClientManager
, with Bluetooth scan turned on proxyClientManager.hasProxyServer
Confirm that there are available proxy devices around, and then go through the proxyClientManager.addAppKeyToNode
and proxyClientManager.bindAppKeyToModel
will BLE Mesh In the network AppKey Bound to the target device Server On the model, and finally send the standard BLE Mesh Message for changing the target device Server State on the model or retrieve relevant information.
// Obtain BLE Mesh Proxy device client configurator
const proxyClientManager = getProxyClientManager ({ networkId: 'network_id' })
// Check for available proxy devices around Server
const hasProxyServer = proxyClientManager.hasProxyServer
if (hasProxyServer) {
// Add to AppKey To the target device
await this.proxyClientManager.addAppKeyToNode({
destination, // Unicast address of the target device
})
// binding AppKey To the target device's GenericOnOffServer On the model
await proxyClientManager.bindAppKeyToModel({
destination, // Unicast address of the target device
modelId: ModelIds.GenericOnOffServer, // standard GenericOnOffServer Model's modelId
})
// Gets the target device's GenericOnOffServer Switching states on the model
const resGet = await proxyClientManager.getOnOffStatus ({
destination, // Unicast or multicast address of the target device
})
console.log(resGet.source, resGet.message.isOn)
// Control the target device's GenericOnOffServer Switching states on the model
const resSet = await proxyClientManager.setOnOffStatus ({
destination, // Unicast or multicast address of the target device
status, // Switch state to be set
})
console.log(resSet.source, resSet.message.isOn)
// Developers can also customize BLE Mesh Message, send and receive the most basic opcode and parameters , to achieve for example Vendor Model Of the function.
proxyClientManager.onMessage(({ source, opcode, parameters }) => {
// handle mesh message
})
proxyClientManager.send({
destination, // Unicast or multicast address of the target device
opcode, // Operation code
parameters, // Carry parameters
})
}
# 3. Note
Developers can useMini Program BLE Mesh plug-inExample Mini Program , Quick Access Mini Program BLE Mesh Ability.
About Small Programs BLE Mesh A detailed description of the capabilities, please refer to theMini Program BLE Mesh plug-inInterface document。
Please try to useMini Program BLE Mesh plug-in
latest
Version to ensure that the function is perfect and stable.at presentMini Program BLE Mesh plug-inOnly provided. BLE Mesh The basic capabilities will be continuously updated according to the needs of the developer.