This WeChat mini program open-source project code library is used to manage Bluetooth functionality in WeChat mini programs. It supports initialization of Bluetooth adapters, scanning and connecting Bluetooth devices, obtaining device services and characteristics, monitoring characteristic value changes, reading and writing characteristic values, and disconnecting operations. By setting different listeners, you can flexibly handle Bluetooth connection status changes, device discovery, service and characteristic discovery events, making it suitable for WeChat mini program development scenarios that require data interaction with Bluetooth devices.
Latest Version: latest
Edit package.json and add the mcblekit dependency:
{
"dependencies": {
"mcblekit": "latest"
}
}Execute the command to install the npm package in the directory where the mini program package.json is located:
cd /the/path/to/package.json/file/
npm installImport the mcblekit module into the JS file that references the Bluetooth management class, and assign the exported content of this module to the variable MCBleKit:
const MCBleKit = require('mcblekit')var writeServiceId = '108A' // Write property service identifier
var writeCharacteristicsId = '909C' // Write property Characteristic identifier
var serviceId = null
var characteristicsId = null
var mcblekit = MCBleKit.getSampleInstance("YOUR_BLUETOOTH_NAME")
mcblekit.onCharacteristicFound((serviceId, res) => {
for (var i = 0; i < res.characteristics.length; i++) {
if (res.characteristics[i].uuid.toUpperCase().indexOf(writeCharacteristicsId.toUpperCase()) >= 0) {
characteristicsId = res.characteristics[i].uuid
// Find the write Characteristic and start writing data
// Determine if it is connected
if (mcblekit.connected) {
// Send data - write data Value
let buffer = new ArrayBuffer(2);
let view = new DataView(buffer);
view.setUint8(0, 0x0A);
view.setUint8(1, 0x01);
mcblekit.writeValue(buffer, 'writeNoResponse', serviceId, characteristicsId)
}
break
}
}
console.log(`Found characteristics for service ${serviceId}:`, res.characteristics);
});
mcblekit.onServicesFound((services) => {
for (var i = 0; i < services.length; i++) {
if (services[i].uuid.toUpperCase().indexOf(writeServiceId.toUpperCase()) >= 0) {
serviceId = services[i].uuid
break
}
console.log(`Found service ${services[i].uuid}`);
}
});
mcblekit.bluetoothDeviceFound(() => {
console.log('bluetoothDeviceFound:' + mcblekit.devices[mcblekit.devices.length - 1].name);
});
mcblekit.onConnectedStatusChange((connected) => {
console.log('onConnectedStatusChange:' + (connected ? "connected" : "disconnected"));
});This library is licensed under the MIT License.
该微信小程序开源项目代码库用于管理微信小程序中的蓝牙功能。支持初始化蓝牙适配器、扫描和连接蓝牙设备、获取设备服务和特征、监听特征值变化、读写特征值以及断开连接等操作。通过设置不同的监听器,可灵活处理蓝牙连接状态变化、设备发现、服务和特征发现等事件,适用于需要与蓝牙设备进行数据交互的微信小程序开发场景。
最新版本: latest
编辑 package.json,添加 mcblekit 依赖:
{
"dependencies": {
"mcblekit": "latest"
}
}在小程序 package.json 所在的目录中执行命令安装 npm 包:
cd /the/path/to/package.json/file/
npm install在引用蓝牙管理类的 JS 内引入 mcblekit 的模块,并将该模块的导出内容赋值给变量 MCBleKit:
const MCBleKit = require('mcblekit')var writeServiceId = '108A' // 写入属性服务标识符
var writeCharacteristicsId = '909C' // 写入属性特征标识符
var serviceId = null
var characteristicsId = null
var mcblekit = MCBleKit.getSampleInstance("YOUR_BLUETOOTH_NAME")
mcblekit.onCharacteristicFound((serviceId, res) => {
for (var i = 0; i < res.characteristics.length; i++) {
if (res.characteristics[i].uuid.toUpperCase().indexOf(writeCharacteristicsId.toUpperCase()) >= 0) {
characteristicsId = res.characteristics[i].uuid
// 找到写入特征,开始写入数据
// 判断是否已连接
if (mcblekit.connected) {
// 发送数据
let buffer = new ArrayBuffer(2);
let view = new DataView(buffer);
view.setUint8(0, 0x0A);
view.setUint8(1, 0x01);
mcblekit.writeValue(buffer, 'writeNoResponse', serviceId, characteristicsId)
}
break
}
}
console.log(`发现服务 ${serviceId} 的特征:`, res.characteristics);
});
mcblekit.onServicesFound((services) => {
for (var i = 0; i < services.length; i++) {
if (services[i].uuid.toUpperCase().indexOf(writeServiceId.toUpperCase()) >= 0) {
serviceId = services[i].uuid
break
}
console.log(`发现服务 ${services[i].uuid}`);
}
});
mcblekit.bluetoothDeviceFound(() => {
console.log('bluetoothDeviceFound:' + mcblekit.devices[mcblekit.devices.length - 1].name);
});
mcblekit.onConnectedStatusChange((connected) => {
console.log('onConnectedStatusChange:' + (connected ? "connected" : "disconnected"));
});本项目基于 MIT License 开源协议。
