npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

mf-ble-instruct

v1.1.1

Published

蓝牙指令集工具,用于生成和解析蓝牙设备的指令

Readme

Bluetooth Instruct API 文档

1. 概述

mf-ble-instruct 是一个完整的蓝牙解决方案,提供蓝牙指令的生成、解析以及蓝牙设备的操作功能。采用单例模式设计,确保整个应用中只有一个实例。

2. 核心类:BluetoothInstruct

2.1 单例模式

该类采用单例模式设计,确保整个应用中只有一个实例。当首次引入模块时,会创建一个实例并导出,后续的引入都会返回同一个实例。

const bluetoothInstruct = require('mf-ble-instruct');

3. 主要功能模块

3.1 写入指令(writeInstruct)

writeInstruct 是一个对象,包含各种蓝牙设备操作的指令生成方法。

3.1.1 light - 灯光控制

| 对象 | 描述 | 输入参数 | 输出参数 | | ------------- | -------- | ---- | -------------------------- | | light.open | 生成灯光开启指令 | 无 | Array<number> - 灯光开启指令数组 | | light.close | 生成灯光关闭指令 | 无 | Array<number> - 灯光关闭指令数组 |

示例:

const lightOnCommand = bluetoothInstruct.writeInstruct.light.open;
console.log('灯光开启指令:', lightOnCommand);
// 输出: [85, 170, 0, 6, 0, 5, 1, 1, 0, 1, 1, 14]

3.1.2 bluetoothMode - 蓝牙模式控制

| 对象 | 描述 | 输入参数 | 输出参数 | | --------------------- | ---------- | ---- | ---------------------------- | | bluetoothMode.open | 获取蓝牙模式开启指令 | 无 | Array<number> - 蓝牙模式开启指令数组 | | bluetoothMode.close | 获取蓝牙模式关闭指令 | 无 | Array<number> - 蓝牙模式关闭指令数组 |

示例:

const bluetoothOffCommand = bluetoothInstruct.writeInstruct.bluetoothMode.close;
console.log('蓝牙模式关闭指令:', bluetoothOffCommand);
// 输出: [85, 170, 0, 6, 0, 5, 2, 1, 0, 1, 0, 14]

3.1.3 musicSwitch - 音乐开关控制

| 对象 | 描述 | 输入参数 | 输出参数 | | ------------------- | -------- | ---- | -------------------------- | | musicSwitch.open | 获取音乐开启指令 | 无 | Array<number> - 音乐开启指令数组 | | musicSwitch.close | 获取音乐关闭指令 | 无 | Array<number> - 音乐关闭指令数组 |

3.1.4 musicControl - 音乐控制

| 对象 | 描述 | 输入参数 | 输出参数 | | ------------------- | ------- | ---- | ------------------------- | | musicControl.next | 获取下一首指令 | 无 | Array<number> - 下一首指令数组 | | musicControl.prev | 获取上一首指令 | 无 | Array<number> - 上一首指令数组 |

3.1.5 countDown - 倒计时设置

| 方法 | 描述 | 输入参数 | 输出参数 | | ---------------------- | ------- | ------------------------------- | ------------------------------------------------ | | countDown(timestamp) | 生成倒计时指令 | timestamp: number - 倒计时时间(分钟) | Array<number> - 倒计时指令数组;如果输入不是数字,返回 Error 对象 |

参数约束:

  • timestamp 必须是数字类型

示例:

const countdownCommand = bluetoothInstruct.writeInstruct.countDown(5); // 5分钟
console.log('5分钟倒计时指令:', countdownCommand);
// 输出: [85, 170, 0, 6, 0, 8, 6, 2, 0, 4, 0, 0, 1, 44, 70]

3.1.6 childLock - 童锁控制

| 对象 | 描述 | 输入参数 | 输出参数 | | ----------------- | -------- | ---- | -------------------------- | | childLock.open | 获取童锁开启指令 | 无 | Array<number> - 童锁开启指令数组 | | childLock.close | 获取童锁关闭指令 | 无 | Array<number> - 童锁关闭指令数组 |

3.1.7 customScene - 自定义场景切换

| 方法 | 描述 | 输入参数 | 输出参数 | | ----------------------- | -------- | ------------------------- | -------------------------- | | customScene(sceneIdx) | 生成场景切换指令 | sceneIdx: number - 场景索引 | Array<number> - 场景切换指令数组 |

参数约束:

  • sceneIdx 必须是数字类型,表示场景的索引

3.1.8 allAisle - 所有通道控制

| 方法 | 描述 | 输入参数 | 输出参数 | | ---------------- | ---------- | ------------------------------------------------------------- | ---------------------------- | | allAisle(data) | 生成所有通道控制指令 | data: Array<Array<number>> - 通道数据,格式为 [[音量, 歌曲, 状态], ...] | Array<number> - 所有通道控制指令数组 |

参数约束:

  • data 必须是二维数组,每个子数组包含三个数字:音量、歌曲、状态
  • 最多支持6个通道,不足6个通道时会自动用0填充

示例:

const allAisleData = [
  [20, 20, 1],  // 通道1:音量20,歌曲20,状态 1开启 2暂停 0关闭
  [60, 21, 1],  // 通道2:音量60,歌曲21,状态 1开启 2暂停 0关闭
  [100, 14, 1]  // 通道3:音量100,歌曲14,状态 1开启 2暂停 0关闭
];
const allAisleCommand = bluetoothInstruct.writeInstruct.allAisle(allAisleData);
console.log('所有通道控制指令:', allAisleCommand);

3.1.9 aisle - 单通道控制

| 方法 | 描述 | 输入参数 | 输出参数 | | ------------- | --------- | -------------------------------------------------------------------------- | --------------------------- | | aisle(data) | 生成单通道控制指令 | data: Object - 通道数据,格式为 { aisleIdx: number, aisleData: Array<number> } | Array<number> - 单通道控制指令数组 |

参数约束:

  • data.aisleIdx 必须是数字类型,表示通道索引(0-5)
  • data.aisleData 必须是数组,包含三个数字:音量、歌曲、状态

示例:

const aisleData = {
  aisleIdx: 0,  // 通道1
  aisleData: [50, 10, 1]  // 音量50,歌曲10,状态1
};
const aisleCommand = bluetoothInstruct.writeInstruct.aisle(aisleData);
console.log('单通道控制指令:', aisleCommand);

3.1.10 customOption - 场景操作

| | | 方法 | 描述 | 输入参数 | 输出参数 | | :------------------- | :------- | ------------------------------------------- | ------ | ---------------------------------------------------------------------- | -------------------------- | | customOption(data) | 生成场景操作指令 | data: Object - 场景操作数据,格式为 `{ type: 'add' | 'edit' | 'del', sceneIdx: number, volume: number, customData: Array }` | Array<number> - 场景操作指令数组 |

参数约束:

  • data.type 必须是字符串,值为 'add'、'edit' 或 'del'
  • data.sceneIdx 必须是数字类型,表示场景索引
  • data.volume 必须是数字类型,表示音量值(删除场景时可忽略)
  • data.customData 必须是数组,表示通道数据(删除场景时可为空)

示例:

// 增加场景,最多支持6条通道,最少一条
const addSceneData = {
  type: 'add',           // 操作类型:add-新增,edit-编辑,del-删除
  sceneIdx: 0,           // 场景索引
  volume: 50,            // 音量值
  customData: [          // 通道数据
    20, 20, 1,  // 通道1:音量20,歌曲20,状态 1开启 2暂停 0关闭
    60, 21, 1,  // 通道2:音量60,歌曲21,状态 1开启 2暂停 0关闭
    100, 14, 1  // 通道3:音量100,歌曲14,状态 1开启 2暂停 0关闭
    20, 20, 1,  // 通道4:音量20,歌曲20,状态 1开启 2暂停 0关闭
    60, 21, 1,  // 通道5:音量60,歌曲21,状态 1开启 2暂停 0关闭
    100, 14, 1  // 通道6:音量100,歌曲14,状态 1开启 2暂停 0关闭
  ]
};
const addSceneCommand = bluetoothInstruct.writeInstruct.customOption(addSceneData);
console.log('增加场景指令:', addSceneCommand);

3.1.11 totalVolume - 总音量控制

| 方法 | 描述 | 输入参数 | 输出参数 | | --------------------- | --------- | ---------------------- | --------------------------- | | totalVolume(volume) | 生成总音量控制指令 | volume: number - 音量值 | Array<number> - 总音量控制指令数组 |

参数约束:

  • volume 必须是数字类型,表示音量值

3.1.12 aisleVolume - 单通道音量控制

| 方法 | 描述 | 输入参数 | 输出参数 | | ------------------- | ----------- | ------------------------------------------------------------------ | ----------------------------- | | aisleVolume(data) | 生成单通道音量控制指令 | data: Object - 通道音量数据,格式为 { aisleIdx: number, volume: number } | Array<number> - 单通道音量控制指令数组 |

参数约束:

  • data.aisleIdx 必须是数字类型,表示通道索引(0-5)
  • data.volume 必须是数字类型,表示音量值

3.1.13 customSceneReported - 场景数据上报

| 方法 | 描述 | 输入参数 | 输出参数 | | --------------------------- | ---------- | ----------------------------------------------------------------------------------------- | ---------------------------- | | customSceneReported(data) | 生成场景数据上报指令 | data: Array<Object> - 场景数据,格式为 [{ volume: number, aisle: Array<Array<number>> }, ...] | Array<number> - 场景数据上报指令数组 |

参数约束:

  • data 必须是数组,每个元素是一个对象,包含 volume 和 aisle 属性
  • volume 必须是数字类型,表示音量值
  • aisle 必须是二维数组,每个子数组包含三个数字:音量、歌曲、状态

3.2 解析指令(parseInstruct)

parseInstruct 是一个对象,包含各种蓝牙设备数据的解析方法。

3.2.1 subFrameHeader - 去除帧头

| 方法 | 描述 | 输入参数 | 输出参数 | | ---------------------- | ------ | ------------------------- | ---------------------- | | subFrameHeader(data) | 去除指令帧头 | data: string - 完整的指令字符串 | string - 去除帧头后的指令字符串 |

参数约束:

  • data 必须是字符串类型

3.2.2 light - 解析灯光状态

| 方法 | 描述 | 输入参数 | 输出参数 | | ------------- | ------ | -------------------------- | ----------------------------- | | light(data) | 解析灯光状态 | data: string - 灯光状态指令字符串 | number - 灯光状态(0: 关闭, 1: 开启) |

参数约束:

  • data 必须是字符串类型

3.2.3 bluetoothMode - 解析蓝牙模式

| 方法 | 描述 | 输入参数 | 输出参数 | | --------------------- | ------ | -------------------------- | ----------------- | | bluetoothMode(data) | 解析蓝牙模式 | data: string - 蓝牙模式指令字符串 | number - 蓝牙模式状态 |

参数约束:

  • data 必须是字符串类型

3.2.4 musicSwitch - 解析音乐开关

| 方法 | 描述 | 输入参数 | 输出参数 | | ------------------- | -------- | -------------------------- | ------------------------------- | | musicSwitch(data) | 解析音乐开关状态 | data: string - 音乐开关指令字符串 | number - 音乐开关状态(0: 关闭, 1: 开启) |

参数约束:

  • data 必须是字符串类型

3.2.5 bluetoothControl - 解析蓝牙控制状态

| 方法 | 描述 | 输入参数 | 输出参数 | | ------------------------ | -------- | -------------------------- | ---------------------------------------- | | bluetoothControl(data) | 解析蓝牙控制状态 | data: string - 蓝牙控制指令字符串 | number - 蓝牙控制状态(0: 回连中, 1: 广播, 2: 已连接) |

参数约束:

  • data 必须是字符串类型

3.2.6 countDown - 解析倒计时

| 方法 | 描述 | 输入参数 | 输出参数 | | ----------------- | ----- | ------------------------- | -------------------- | | countDown(data) | 解析倒计时 | data: string - 倒计时指令字符串 | number - 倒计时时间(毫秒) |

参数约束:

  • data 必须是字符串类型

3.2.7 countDownReported - 解析倒计时上报

| 方法 | 描述 | 输入参数 | 输出参数 | | ------------------------- | --------- | --------------------------- | -------------------- | | countDownReported(data) | 解析倒计时上报数据 | data: string - 倒计时上报指令字符串 | number - 倒计时时间(毫秒) |

参数约束:

  • data 必须是字符串类型

3.2.8 childLock - 解析童锁状态

| 方法 | 描述 | 输入参数 | 输出参数 | | ----------------- | ------ | ------------------------ | ----------------------------- | | childLock(data) | 解析童锁状态 | data: string - 童锁指令字符串 | number - 童锁状态(0: 关闭, 1: 开启) |

参数约束:

  • data 必须是字符串类型

3.2.9 customScene - 解析自定义场景

| 方法 | 描述 | 输入参数 | 输出参数 | | ------------------- | ------- | --------------------------- | --------------- | | customScene(data) | 解析自定义场景 | data: string - 自定义场景指令字符串 | number - 场景索引 |

参数约束:

  • data 必须是字符串类型

3.2.10 customSceneReported - 解析自定义场景上报

| 方法 | 描述 | 输入参数 | 输出参数 | | --------------------------- | ----------- | ----------------------------- | ---------------------------------------------------------------------- | | customSceneReported(data) | 解析自定义场景上报数据 | data: string - 自定义场景上报指令字符串 | Object - 场景数据,格式为 { sceneCount: number, sceneData: Array<Object> } |

参数约束:

  • data 必须是字符串类型,且包含 '0a' 字符串

返回数据结构:

{
  sceneCount: number,  // 场景数量
  sceneData: [         // 场景数据数组
    {
      scene: number,     // 场景索引
      sceneName: string, // 场景名称
      imgUrl: string,    // 场景图片URL
      volume: number,    // 音量
      aisle: Array<Array<number>> // 通道数据
    },
    // 更多场景...
  ]
}

3.2.11 allAisle - 解析所有通道

| 方法 | 描述 | 输入参数 | 输出参数 | | ---------------- | -------- | -------------------------- | ------------------------------- | | allAisle(data) | 解析所有通道数据 | data: string - 所有通道指令字符串 | Array<Array<number>> - 通道数据数组 |

参数约束:

  • data 必须是字符串类型,且包含 '6b' 字符串

返回数据结构:

[
  [音量, 歌曲, 状态],  // 通道1
  [音量, 歌曲, 状态],  // 通道2
  // 更多通道...
]

3.2.12 totalVolume - 解析音量

| 方法 | 描述 | 输入参数 | 输出参数 | | ------------------- | ------ | ------------------------ | -------------- | | totalVolume(data) | 解析音量数据 | data: string - 音量指令字符串 | number - 音量值 |

参数约束:

  • data 必须是字符串类型

3.2.13 aisleVolume - 解析通道音量上报

| 方法 | 描述 | 输入参数 | 输出参数 | | ------------------- | ---------- | -------------------------- | ----------------------------------------- | | aisleVolume(data) | 解析通道音量上报数据 | data: string - 通道音量指令字符串 | Array<number> - 通道音量数据,格式为 [通道索引, 音量] |

参数约束:

  • data 必须是字符串类型

3.2.14 deviceUnBind - 解析设备解绑

| 方法 | 描述 | 输入参数 | 输出参数 | | -------------------- | -------- | -------------------------- | ------------------------------------ | | deviceUnBind(data) | 解析设备解绑数据 | data: string - 设备解绑指令字符串 | number - 解绑状态(0: 设备解绑, 1: 解绑并清除数据) |

参数约束:

  • data 必须是字符串类型

3.2.15 aisle - 解析单通道信息上报

| 方法 | 描述 | 输入参数 | 输出参数 | | ------------- | ----------- | ------------------------- | --------------------------------------------------- | | aisle(data) | 解析单通道信息上报数据 | data: string - 单通道指令字符串 | Array<number> - 通道信息数据,格式为 [通道索引, 音量, 音乐, 通道开关] |

参数约束:

  • data 必须是字符串类型

3.3 BLE 功能

3.3.1 initBle - 初始化BLE SDK

| 方法 | 描述 | 输入参数 | 输出参数 | | ------------------ | ---------- | ------------------------------------------------------------------------- | ------------------------------- | | initBle(options) | 初始化BLE SDK | options: Object - 配置选项,格式为 { autoReconnect: boolean, debug: boolean } | Promise<void> - 初始化完成的Promise |

参数约束:

  • options.autoReconnect 可选,布尔值,是否自动重连,默认 false
  • options.debug 可选,布尔值,是否开启调试日志,默认 false

示例:

await bluetoothInstruct.initBle({
  autoReconnect: true,
  debug: true
});

3.3.2 getBleSdk - 获取BLE SDK实例

| 方法 | 描述 | 输入参数 | 输出参数 | | ------------- | ----------- | ---- | -------------------- | | getBleSdk() | 获取BLE SDK实例 | 无 | BleSdk - BLE SDK实例 |

示例:

const bleSdk = bluetoothInstruct.getBleSdk();

3.4 其他方法

3.4.1 hasHeader - 检查是否有帧头

| 方法 | 描述 | 输入参数 | 输出参数 | | ----------------- | --------- | ---------------------- | ----------------- | | hasHeader(data) | 检查指令是否有帧头 | data: string - 指令字符串 | boolean - 是否有帧头 |

参数约束:

  • data 必须是字符串类型

3.4.2 getDpIdKey - 获取dpId对应的映射

| | 方法 | 描述 | 输入参数 | 输出参数 | | :-------------------------- | --------------- | ------------------------------------------------------------ | -------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | getDpIdKey(data, isParse) | 根据指令获取dpId对应的映射 | data: string - 指令字符串isParse: boolean - 是否同时解析数据,默认为false | `Object | Error- 当isParse为false时,返回dpId映射对象,格式为{ target: string, name: string };当isParse为true时,返回包含解析数据的对象,格式为 { parseData: any, target: string, name: string }`;如果输入数据无效,返回Error对象 |

参数约束:

  • data 必须是字符串类型
  • isParse 可选,布尔值,是否同时解析数据

示例:

// 仅获取dpId映射
const lightData = '55aa000700040100000101';
const lightDpIdMap = bluetoothInstruct.getDpIdKey(lightData);
console.log('灯光指令的dpId映射:', lightDpIdMap);
// 输出: { target: 'light', name: '指示灯' }

// 同时获取dpId映射和解析数据
const volumeData = '55aa000700080b02000400000012';
const volumeResult = bluetoothInstruct.getDpIdKey(volumeData, true);
console.log('音量指令的解析结果:', volumeResult);
// 输出: { parseData: 18, target: 'totalVolume', name: '音量操作' }

3.4.3 parseAisleOfCustomSceneReported - 解析自定义场景上报的通道数据

| 方法 | 描述 | 输入参数 | 输出参数 | | ----------------------------------------------------- | -------------- | ---------------------------------------------------------- | ---------------------------- | | parseAisleOfCustomSceneReported(hexStr, groupCount) | 解析自定义场景上报的通道数据 | hexStr: string - 通道数据的十六进制字符串groupCount: number - 场景数量 | Array<Object> - 解析后的场景数据数组 |

参数约束:

  • hexStr 必须是字符串类型
  • groupCount 必须是数字类型,且大于0

返回数据结构:

[
  {
    scene: number,     // 场景索引
    sceneName: string, // 场景名称
    imgUrl: string,    // 场景图片URL
    volume: number,    // 音量
    aisle: Array<Array<number>> // 通道数据
  },
  // 更多场景...
]

3.4.4 splitStringByInterval - 按指定长度分割字符串

| 方法 | 描述 | 输入参数 | 输出参数 | | -------------------------------------- | ---------- | ------------------------------------------------ | --------------------------- | | splitStringByInterval(str, interval) | 按指定长度分割字符串 | str: string - 要分割的字符串interval: number - 分割长度 | Array<string> - 分割后的字符串数组 |

参数约束:

  • str 必须是字符串类型
  • interval 必须是数字类型,且大于0

4. 使用说明

4.1 基本使用流程

  1. 安装包
    npm install mf-ble-instruct
  2. 引入包
    const bluetoothInstruct = require('mf-ble-instruct');
  3. 使用指令功能
    • 生成指令:使用 writeInstruct 对象的方法
    • 解析指令:使用 parseInstruct 对象的方法
  4. 使用BLE功能
    • 初始化BLE SDK:await bluetoothInstruct.initBle(options)
    • 获取BLE SDK实例:const bleSdk = bluetoothInstruct.getBleSdk()
    • 使用BLE SDK的方法操作蓝牙设备

4.2 环境要求

  • Node.js:v12.0.0 或更高版本
  • uni-app:用于BLE功能的运行环境(小程序、App等)
  • 蓝牙权限:需要在应用中获取蓝牙使用权限
  • Android 6.0+:需要位置权限才能扫描蓝牙设备

4.3 完整使用示例

const bluetoothInstruct = require('mf-ble-instruct');

// 初始化BLE SDK
async function initBluetooth() {
  try {
    await bluetoothInstruct.initBle({
      autoReconnect: true,
      debug: true
    });
    console.log('BLE SDK初始化成功');
  } catch (error) {
    console.error('BLE SDK初始化失败:', error);
  }
}

// 扫描并连接设备
async function scanAndConnect() {
  const bleSdk = bluetoothInstruct.getBleSdk();
  
  try {
    // 开始扫描
    await bleSdk.startScan({}, (device) => {
      console.log('发现设备:', device);
      // 找到目标设备后停止扫描并连接
      if (device.name === '目标设备名称') {
        bleSdk.stopScan();
        connectDevice(device.deviceId);
      }
    });
  } catch (error) {
    console.error('扫描失败:', error);
  }
}

// 连接设备
async function connectDevice(deviceId) {
  const bleSdk = bluetoothInstruct.getBleSdk();
  
  try {
    await bleSdk.connect(deviceId);
    console.log('设备连接成功:', deviceId);
    // 连接成功后发送指令
    sendCommand(deviceId);
  } catch (error) {
    console.error('设备连接失败:', error);
  }
}

// 发送指令
async function sendCommand(deviceId) {
  const bleSdk = bluetoothInstruct.getBleSdk();
  
  try {
    // 获取服务和特征值
    const services = await bleSdk.getServices(deviceId);
    const serviceId = services[0].uuid; // 假设第一个服务是目标服务
    const characteristics = await bleSdk.getCharacteristics(deviceId, serviceId);
    const characteristicId = characteristics.find(c => c.properties.write).uuid; // 找到可写的特征值
    
    // 生成指令
    const lightOnCommand = bluetoothInstruct.writeInstruct.light.open;
    console.log('灯光开启指令:', lightOnCommand);
    
    // 发送指令
    const uint8Array = new Uint8Array(lightOnCommand);
    await bleSdk.writeCharacteristic(deviceId, serviceId, characteristicId, uint8Array.buffer);
    console.log('指令发送成功');
    
    // 启用通知
    await bleSdk.enableNotifyWithCallback(deviceId, serviceId, characteristicId, (value) => {
      // 解析返回的数据
      const data = Array.from(new Uint8Array(value)).map(b => b.toString(16).padStart(2, '0')).join('');
      console.log('收到数据:', data);
      
      // 解析数据
      if (bluetoothInstruct.hasHeader(data)) {
        const result = bluetoothInstruct.getDpIdKey(data, true);
        console.log('解析结果:', result);
      }
    });
  } catch (error) {
    console.error('发送指令失败:', error);
  }
}

// 启动流程
async function start() {
  await initBluetooth();
  await scanAndConnect();
}

start();

5. 错误处理

5.1 常见错误

| 错误信息 | 原因 | 解决方案 | | ----------------------- | --------------------------- | -------------------- | | countDown:请传入数字类型的时间戳 | 调用 countDown 方法时传入了非数字类型的参数 | 确保传入数字类型的时间戳 | | 请传入有效的指令 | 调用 getDpIdKey 方法时传入了无效数据 | 确保传入有效的指令字符串 | | 蓝牙适配器初始化失败 | BLE SDK 初始化失败 | 确保设备蓝牙已开启,且应用有蓝牙使用权限 | | 当前蓝牙不可用 | 设备蓝牙未开启 | 开启设备蓝牙 | | 没有找到指定设备 | 设备不在范围内或已关闭 | 确保设备在范围内且已开启 | | 连接失败 | 设备连接失败 | 重试连接或检查设备状态 | | 设备未连接 | 操作设备前未连接或连接已断开 | 确保设备已连接 |

5.2 错误处理示例

// 处理指令生成错误
try {
  const countdownCommand = bluetoothInstruct.writeInstruct.countDown('300'); // 传入字符串,会出错
  console.log('倒计时指令:', countdownCommand);
} catch (error) {
  console.error('生成指令失败:', error.message);
}

// 处理BLE操作错误
try {
  const bleSdk = bluetoothInstruct.getBleSdk();
  await bleSdk.connect('无效设备ID');
} catch (error) {
  console.error('BLE操作失败:', error.message);
}

6. 最佳实践

  1. 单例模式:由于采用单例模式,建议在应用启动时初始化一次,然后在整个应用中共享使用
  2. 错误处理:所有异步操作都应该使用 try-catch 进行错误处理
  3. 资源管理:使用完毕后,应该调用 BLE SDK 的 destroy 方法释放资源
  4. 指令生成:生成指令后,可以使用 ab2hex 工具函数将指令转换为十六进制字符串进行调试
  5. 数据解析:解析数据时,应该先检查数据格式是否正确,再进行解析
  6. 性能优化:对于频繁使用的指令,可以缓存生成的结果,避免重复计算
  7. 调试模式:在开发阶段,可以开启 debug 模式查看详细的日志信息

7. 总结

mf-ble-instruct 是一个功能完整的蓝牙解决方案,提供了:

  • 指令生成:支持各种蓝牙设备操作的指令生成
  • 指令解析:支持解析设备返回的各种数据
  • BLE操作:集成了完整的蓝牙设备操作功能
  • 单例模式:确保整个应用中只有一个实例
  • 校验码自动计算:所有指令都会自动添加校验码

通过本文档的指导,您可以轻松地在项目中集成和使用该包,实现蓝牙设备的控制和数据交互。

8. 备注

  • 硬件设备限制:硬件设备不支持场景名称以及图片的存储,因此在解析自定义场景上报数据时,sceneNameimgUrl 字段会被设置为默认值(sceneName 为 "场景N",imgUrl 为空字符串)。
  • 本地数据存储:用户可以将设备上报的数据存储到本地,然后在本地对数据进行处理,例如添加自定义的场景名称和图片。
  • 多用户场景:当多个用户使用同一台设备时,可使用本地数据 + 包中提供的 customSceneReported 方法来进行数据覆写,即使用当前连接的用户数据覆盖设备的场景数据。
  • 持久化存储:根据业务场景需要,可将 userId 和场景数据存储到数据库中,以便在不同设备或会话之间保持用户的场景配置。
  • 状态管理:建议将设备数据状态存储到 Pinia 或 Vuex 等状态管理库中,方便多页面共享设备数据状态,提高应用的响应速度和用户体验。
  • 操作日志存储:如需要将操作日志存储到数据库,请注意:部分操作,设备可能会有多次上报,请注意日志存储的时机,避免重复存储相同的操作日志。建议在收到设备的最终确认或状态稳定后再进行日志存储。