1.0.14 • Published 2 years ago
wechat-ble-swap-comm-kit v1.0.14
概述
该sdk 是为 微信小程序平台提供的 蓝牙通信工具;
1. 连接目标柜子,获取蓝牙接口所提示的deviceId (这一步为可选)
2. 开始与已连接设备蓝牙通信 1. 初始化对蓝牙服务的数据监听回调 2. 向已连接设备发送 鉴权请求 3. 向已连接设备发送 相关数据消息
1. 连接目标柜子,获取蓝牙接口所提供的deviceid (可选)
注意: 已连接设备所获取的 deviceId 与 柜子编码 是两个概念
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
dev_sn | string | 是 | 待连接的设备编号 | |
open_comm_retry | boolean | false | 否 | 是否开启设备连接失败的重试功能(即:会提示失败信息以及重试按钮) |
discovery_timeout | number | 5000 | 否 | 搜索设备的超时时长(单位:ms),最短不低于5秒,最长不高于60秒 |
conn_timeout | number | 5000 | 否 | 连接设备的超时时长(单位:ms),最短不低于5秒,最长不高于60秒 |
success | function | 否 | 接口调用成功的回调函数(函数参数: 已连接设备所获取的deviceid) | |
fail | function | 否 | 接口调用失败的回调函数 |
const {connectTargetDevice} = require("wechat-ble-swap-comm-kit");
connectTargetDevice({
"dev_sn": "123445",
"open_comm_retry": true,
"success": (deviceId) => {
//连接成功
},
"fail": (err)=>{
//连接失败
}
})
2. 开始蓝牙换电通信
一 、开始蓝牙离线通信之前,了解一个新概念messageId
1. 解释: 消息id 表示每一条完整消息(即 一侧的 请求数据流 + 另一侧的 响应数据流)的标识;
2. 消息id的产生: 是由 发送请求数据流 的一侧生成, 且 0 为无效值
3. 应用:当手机端想要发送 响应数据流 时,就需要知道该条完整消息的 messageId
, 例如函数 sendServerRespondMsg
的传参
二、以下示例是,声明了一个 数据传输处理对象;
const {DataTransferProcessor} = require("wechat-ble-swap-comm-kit");
class BleSwapController{
//设备连接成功后所获取的deviceId
constructor(deviceId){
this._transferProcessor = new DataTransferProcessor(deviceId);
//设置监听函数(监听来着柜子端发送的蓝牙数据)
this._transferProcessor.setEventNotiyFunc(this.listenCommProcessInfo.bind(this));
}
/**
* //监听通信过程中的信息
* @param {string} type 消息类型
* @param {*} info 消息内容
*/
listenCommProcessInfo(type, info) {
switch (type) {
case "comm_server_request_stream": {
const bleServerReqInfo = info.data || {};
switch (bleServerReqInfo["msgType"]) {
case 410: {
//@todo 上传服务端换取 响应数据命令
break;
}
default:
Log.log("[COMM_SERVER_REQUEST_STREAM] not need to be reported msgtype:", bleServerReqInfo['msgType']);
}
return;
}
case "comm_server_respond_stream": {
const bleServerRespInfo = info.data || {};
switch (bleServerRespInfo["msgType"]) {
case 501: {
//@todo 上传服务端
break;
}
default:
Log.log("[COMM_SERVER_RESPOND_STREAM]not need to be reported msgtype:", bleServerRespInfo['msgType']);
}
break;
}
}
}
}
setEventNotiyFunc
绑定监听回调函数:
- callback 参数(消息类型):
comm_server_request_stream
监听来着柜子蓝牙服务上报的 请求数据流;comm_server_respond_stream
监听由手机端发送的请求,柜子蓝牙服务进行回应的 响应数据流comm_err
监听蓝牙通信传输过程的报错(详见3.1)
- callback 参数(数据):
- 报错信息:
{
massageId: [number],
data: {
err_code: [number], //负数表示:通信过程中报错;正数表示: 柜子的错误响应code
err_msg: [string]
}
}
2. 有效数据:
{
massageId: [number],
data: [object] //已解析的mqtt 信息例如:
//{"oid":1637053341876,"resp_info":"{\"msgType\":501,\"devId\":\"ZK-B50100051\",\"result\":6,\"txnNo\":1637053341876,\"extVal\":\"errorCode=0\"}"}
}
2.1 手机端向服务端获取token,并向已连接的柜子发起 “鉴权请求”
属性 | 类型 | 默认值 | 必填 | 说明 |
---|---|---|---|---|
txn_no | number | 是 | 时间戳 | |
token | string | 是 | 鉴权所需的token | |
success | function | 否 | 接口调用成功的回调函数 | |
fail | function | 否 | 接口调用失败的回调函数 |
//sendAuthInfo 发送鉴权信息
this._transferProcessor.sendAuthInfo({
"txn_no": res.data['txnNo'] || 0,
"token": (res.data['token'] || "").trim(),
success: (bytes)=>{
//鉴权成功
},
fail: err => {
//鉴权失败 //{errCode: [number], errMsg: "xxx"}
}
});
2.2 向蓝牙服务发送消息(发送方式分为两种,需合理调用)
2.2.1 向蓝牙服务发送 “请求类型”的消息
例如: 向服务器获取“换电指令”(格式:JSON),并向柜子正式发起“换电指令”
/**
* 发送服务端的请求消息
* @param {string} requestMsg 请求消息的内容
* @return {number} messageId 返回每条完整消息的id(注:0为无效值)
*/
this._transferProcessor.sendServerRequestMsg(requestMsg);
2.2.2 向蓝牙服务发送 “响应类型” 的消息
例如: 等待接收柜子端通过蓝牙上报的mqtt 指令,获取成功后并交于服务端换取 回应mqtt 指令
/**
* 发送服务端响应消息
* @param {number} messageId 每条完整消息的id
* @param {string} respondMsg 响应消息的内容
* @return {boolean} 是否发送成功
*/
this._transferProcessor.sendServerRespondMsg(messageId,respondMsg);
3. 其他事项
3.1 该工具不提供对换电流程的成功、失败结果的监听,只提供蓝牙换电通信过程中 出现异常的监听;
数据格式: { "errCode": number, "errMsg": string }
function listenCommProcessInfo(type, info) {
switch (type) {
case "comm_err": {
Log.error("[COMM_ERR] comm error info:", info);
break;
}
default:
Log.warn("[listenCommProcessInfo] unkown type:", type);
}
}
3.2 该工具内置对 蓝牙通信的日志本地存储;提供设置 是否显示console 蓝牙通信的log;
const {showConsoleLog} = require("wechat-ble-swap-comm-kit");
showConsoleLog(true); //开启蓝牙通信日志显示
4. 错误列表
错误code | 错误描述 |
---|---|
10000 | 未初始化蓝牙适配器 |
10001 | 当前蓝牙适配器不可用 |
10002 | 没有找到指定设备 |
10003 | 连接失败 |
10004 | 没有找到指定服务 |
10005 | 没有找到指定特征 |
10006 | 当前连接已断开 |
10007 | 当前特征不支持此操作 |
10008 | 系统上报的异常 |
10009 | 系统版本过低不支持BLE |
10012 | 连接超时 |
10013 | 连接deviceId为空或者是格式不正确 |
20001 | 搜索超时,未能找到目标设备 |
20002 | 未发现有效的通信服务 |
20003 | 未发现目标通信通道 |
20004 | 未发现鉴权通道 |
20005 | 等待鉴权结果超时 |
20006 | 鉴权结果未通过 |
20007 | 设备编号无效 |
5. 历史版本记录
版本号 | 版本描述 |
---|---|
1.0.13 | 1. 修复回调函数调用误差的bug2. 更正部分log 打印3. 针对连接部分的自动重连回调函数优化处理 |