更新记录

1.0.2(2026-10-14) 下载此版本

  • 承接项目侧下沉:WithWeightPush(有线秤重量推送 mixin)

1.0.1(2026-10-14) 下载此版本

  • 承接 WeightError(自 pro-device-core 下沉)

1.0.0(2026-10-14) 下载此版本

  • 首次发布
  • 提供 WeightReading、IWeighable、ScaleDevice 类型定义
  • 依赖 pro-device-core
查看更多

平台兼容性

uni-app(5.06)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
√ √ √ √ √ - √ - -
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
√ - - - - - - - - - - -

pro-device-scale — 电子秤设备接口

定义电子秤设备的能力接口 IWeighable 和组合类型 ScaleDevice。本包只包含接口定义,不含任何具体设备实现。

依赖 pro-device-core。具体电子秤实现在项目层 src/devices/scale/ 中。


导出内容

import type { IWeighable, ScaleDevice, WeightReading } from '@/uni_modules/pro-device-scale'
导出 类型 说明
WeightReading interface 称重读数(值、单位、稳定状态)
IWeighable interface 称重能力接口
ScaleDevice type IConnectable & IWeighable & IIdentifiable 组合

API 参考

WeightReading — 称重读数

interface WeightReading {
  value: number              // 重量数值
  unit: 'KG' | 'G' | 'LB' | 'OZ'  // 单位
  stable: boolean            // true = 读数已稳定(可采样)
}

IWeighable — 称重能力接口

interface IWeighable {
  /** 单次读取当前重量(等待下一次稳定读数) */
  readWeight(): Promise<WeightReading>
  /** 去皮 / 归零 */
  resetWeight(): Promise<void>
  /** 订阅重量变化,每次稳定读数推送一次,返回取消订阅函数 */
  onWeightChange(cb: (reading: WeightReading) => void): Unsubscribe
}

ScaleDevice — 电子秤设备完整类型

// 等价于 IConnectable & IWeighable & IIdentifiable
type ScaleDevice = IConnectable & IWeighable & IIdentifiable

使用示例

1. 连接电子秤并读取一次重量

import { createDeviceSession } from '@/devices'

const session = createDeviceSession()

// 法亚 BX380,通过广播 MAC 地址发现(matchByAdvertisMAC)
const scale = await session.acquire('scale', {
  brand: 'Faya',
  model: 'BX380',
  ident: 'AA:BB:CC:DD:EE:FF',  // 秤的 MAC 地址(标签/系统设置中查看)
})

console.log('电子秤型号:', scale.info.brand, scale.info.model)

// 单次读重(等待稳定读数)
const reading = await scale.readWeight()
if (reading.stable) {
  console.log(`重量: ${reading.value} ${reading.unit}`)  // 例:重量: 1.25 KG
}

2. 订阅实时重量变化

import { onMounted, onUnmounted, ref } from 'vue'
import type { WeightReading } from '@/uni_modules/pro-device-scale'

const weight = ref<WeightReading | null>(null)
let unsubWeight: (() => void) | null = null

onMounted(async () => {
  const scale = await session.acquire('scale', config)

  // 订阅稳定读数推送
  unsubWeight = scale.onWeightChange((reading) => {
    weight.value = reading
    console.log(`稳定重量: ${reading.value} ${reading.unit}`)
  })
})

onUnmounted(() => {
  unsubWeight?.()
  session.releaseAll()
})

3. 采重上报(等待稳定读数后提交)

async function handleWeigh() {
  const scale = appDeviceStore.scale
  if (!scale) {
    uni.showToast({ title: '电子秤未连接', icon: 'none' })
    return
  }

  uni.showLoading({ title: '等待稳定读数...' })
  try {
    const reading = await scale.readWeight()
    uni.hideLoading()
    if (!reading.stable) {
      uni.showToast({ title: '读数不稳定,请重试', icon: 'none' })
      return
    }
    // 提交重量数据
    await submitWeight(reading.value, reading.unit)
    uni.showToast({ title: `采重 ${reading.value} ${reading.unit}`, icon: 'success' })
  } catch (e: any) {
    uni.hideLoading()
    uni.showToast({ title: e?.message ?? '称重失败', icon: 'none' })
  }
}

4. 去皮操作

async function handleTare() {
  const scale = appDeviceStore.scale
  if (!scale) return
  try {
    await scale.resetWeight()
    uni.showToast({ title: '已去皮归零', icon: 'success' })
  } catch (e: any) {
    uni.showToast({ title: e?.message ?? '去皮失败', icon: 'none' })
  }
}

5. 监听连接状态

import { ConnectionState } from '@/uni_modules/pro-device-core'

// ScaleDevice 同时是 IConnectable,可监听连接状态
const unsub = scale.onStateChange((state) => {
  if (state === ConnectionState.Error) {
    uni.showToast({ title: '电子秤连接断开', icon: 'none' })
  }
})

// 记得在 onUnmounted 中取消订阅
onUnmounted(() => unsub())

实现新电子秤

在项目层 src/devices/scale/ 中创建具体实现并注册:

// src/devices/scale/my-scale.ts
import { BLEDeviceBase } from '../base/ble-device-base'
import type { IWeighable, WeightReading } from '@/uni_modules/pro-device-scale'
import type { DeviceConfig } from '@/uni_modules/pro-device-core'
import { WeightError } from '@/uni_modules/pro-device-core'

export class MyScale extends BLEDeviceBase implements IWeighable {
  constructor(config: DeviceConfig) {
    super(config)
  }

  async readWeight(): Promise<WeightReading> {
    this.assertConnected()
    // 发送读重指令,等待回调数据
    return new Promise((resolve, reject) => {
      const unsub = this._transport.onReceive((data) => {
        unsub()
        const bytes = new Uint8Array(data)
        const value = parseWeight(bytes)  // 设备专用协议解析
        resolve({ value, unit: 'KG', stable: true })
      })
      this._transport.send(READ_WEIGHT_CMD).catch(reject)
    })
  }

  async resetWeight(): Promise<void> {
    this.assertConnected()
    await this._transport.send(TARE_CMD)
  }

  onWeightChange(cb: (reading: WeightReading) => void) {
    return this._transport.onReceive((data) => {
      const bytes = new Uint8Array(data)
      if (isWeightPacket(bytes)) {
        cb({ value: parseWeight(bytes), unit: 'KG', stable: isStable(bytes) })
      }
    })
  }
}

// src/devices/index.ts 中注册
const scaleRegistry = new DeviceRegistry<ScaleDevice>()
  .registerModel('Faya', 'BX380', cfg => new FayaBX380(cfg))
  .registerModel('MyBrand', 'S100', cfg => new MyScale(cfg))  // 新增

平台支持

本包为纯 TypeScript 接口定义,无平台依赖。

具体设备的平台支持取决于其传输层:法亚 BX380 使用 pro-device-adp-ble,支持微信/支付宝小程序和 APP。

隐私、权限声明

1. 本插件需要申请的系统权限列表:

无

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

无

许可协议

MIT协议

暂无用户评论。