优秀的编程知识分享平台

网站首页 > 技术文章 正文

uni-app 开发实战:蓝牙设备读写(uniapp 蓝牙串口)

nanyue 2024-08-06 18:10:50 技术文章 9 ℃

现阶段蓝牙技术已在现实生活和工作中得到广泛使用,各平台为我们提供了上手实践的方法,带上你的装备,一起来看看怎么控制你的蓝牙设备。

目录:

一、蓝牙连接流程

二、蓝牙读写操作


蓝牙连接流程


连接流程闭坑指南:

1、获取在蓝牙模块生效期间所有已发现的蓝牙设备(uni.getBluetoothDevices),添加有效设备且需要去重,如:

uni.getBluetoothDevices({
  success: res => {
    // 查询所有已知设备
    res.devices.forEach(item => {
      if (item.deviceId && item.name) this.baseList.push(item)
    })
    // 对象去重
    this.bleList = this.$utils.uniqueObj(this.baseList)
  }
})


2、连接低功耗蓝牙设备(uni.createBLEConnection)后,需要延迟1.5秒以上调用,才能拿到设备服务信息。停止搜寻附近的蓝牙外围设备(uni.stopBluetoothDevicesDiscovery),因为一直搜寻附近的蓝牙外围设备,比较耗费系统资源,如:

uni.createBLEConnection({
  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  deviceId: item.deviceId,
  success: res => {
    // 需要延迟调用才能拿到设备服务信息
    setTimeout(() => {
      this.getBLEDeviceServices(item.deviceId)
      this.stopBluetoothDevicesDiscovery()
    }, 1500)
  }
})


3、获取蓝牙设备所有服务(uni.getBLEDeviceServices),获取设备服务列表含义“FFF”的服务,如:

uni.getBLEDeviceServices({
  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  deviceId,
  success: res => {
    // FFF uuid 微信硬件平台协议
    res.services.forEach((item) => {
      item.uuid.indexOf('FFF') > -1 && this.getBLEDeviceCharacteristics(deviceId, item.uuid)
    })
  }
})


4、获取蓝牙设备某个服务中所有特征值(uni.getBLEDeviceCharacteristics),如:

uni.getBLEDeviceCharacteristics({
  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  deviceId,
  // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  serviceId,
  success: res => {
    res.characteristics.forEach((item, index) => {
      // 读取低功耗蓝牙设备的特征值的二进制数据值。注意:必须设备的特征值支持 read 才可以成功调用。
      if (item.properties.read && index == 0) {
        uni.readBLECharacteristicValue({
          // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
          deviceId,
          // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
          serviceId,
          // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
          characteristicId: item.uuid
        })
      }
      // 向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持 write 才可以成功调用。
      if (item.properties.write && index == 1) {
        this.deviceId = deviceId
        this.serviceId = serviceId
        this.characteristicId = item.uuid
        // 向蓝牙中写入对应的值
        setTimeout(() => {
          this.writeBLECharacteristicValue('00000000')
        }, 500)
      }
      // 启用低功耗蓝牙设备特征值变化时的 notify 功能,订阅特征值。注意:必须设备的特征值支持 notify 或者 indicate 才可以成功调用。另外,必须先启用 notifyBLECharacteristicValueChange 才能监听到设备 characteristicValueChange 事件
      if ((item.properties.notify || item.properties.indicate) && index == 0) {
        uni.notifyBLECharacteristicValueChange({
          state: true, // 启用 notify 功能
          // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
          deviceId,
          // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
          serviceId,
          // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
          characteristicId: item.uuid,
          success: res => {
            // 安卓平台上,成功后立即调用,在部分机型上会发生 10008 系统错误
            setTimeout(() => {
              this.onBLECharacteristicValueChange()
            }, 300)
          }
        })
      }
    })
  }
})

获取某个特征值时,之所以下标值写死,是通过不断尝试得到,可能不同设备存在差异,需要自己去调试。


5、监听低功耗蓝牙设备的特征值变化事件(uni.onBLECharacteristicValueChange),记得转义后打印,直接打印值为空,如:

uni.onBLECharacteristicValueChange(res => {
  // 写入蓝牙后的应答值
  console.log('写入蓝牙后的应答值----------', this.$utils.ab2hex(res.value))
})


以上一顿操作完成,能看到应答值,恭喜你,现在可以随意操作你的蓝牙设备了;如需要完整代码,欢迎评论区留言;


蓝牙读写操作

读写操作时,设备需要提供一份蓝牙通信协议;协议采用一问一答方式进行交互,协议格式大致为:帧头+命令码+数据组号+数据长度+数据+某种校验+帧尾;

最近发表
标签列表