wx.getBLEDeviceCharacteristics WeChat applet Bluetooth WeChat applet thermal printer

1 Overview of Bluetooth function development of WeChat applet
  • The first step is to determine whether the current WeChat version supports Bluetooth communication
  • The second step is to turn on bluetooth
  • Step 3 Scan the device
  • Step 4 Connect the device
  • The fifth step is to obtain the service and characteristic value to see if it supports read and write data operations
  • Step 6 Send data

This article is the content point in the fifth step of the record

2 wx.getBLEDeviceCharacteristics

wx.getBLEDeviceCharacteristics is used to get
Get all the characteristic values ​​(characteristic) in a service of the Bluetooth device

The key core code is as follows

2.1 Get the supported services connected to Bluetooth

In this step, I have scanned the available bluetooth devices, obtained the deviceId of one of the devices, and callBack is a callback function I set

//获取蓝牙设备所有服务(service)
function getBLEDeviceServices(deviceId, callBack) {
    
    
  console.log("连接低功耗蓝牙设备成功 开始获取蓝牙服务"+deviceId)
  wx.getBLEDeviceServices({
    
    
    deviceId,
    success: (res) => {
    
    
      console.log("连接低功耗蓝牙设备成功 获取蓝牙服务成功"+deviceId)
      console.log("getBLEDeviceServices success")
      for (let i = 0; i < res.services.length; i++) {
    
    
        if (res.services[i].isPrimary) {
    
    
          getBLEDeviceCharacteristics(deviceId, res.services[i].uuid, callBack)
        }
      }
    },
    fail: (res) => {
    
    
      console.log("连接低功耗蓝牙设备成功 获取蓝牙服务失败"+deviceId)
      console.log("getBLEDeviceServices fail")
      console.log("获取蓝牙服务失败:" + JSON.stringify(res))
    }
  })
}

There is something wrong with the official WeChat documentation. My practice is that it is available on some Android phones, and there are multiple services available. If you return here, it will cause the problem that the data cannot be output in the next step. In fact, it can be output. of

insert image description here

2.2 Get the key core code of eigenvalues

It is necessary to obtain the characteristic value according to the deviceId and serviceId service ID of the Bluetooth device, such as whether to support reading the data of the Bluetooth device or whether to support writing data to the Bluetooth device, which are all judged by the characteristic value.

In some Android phones, there may be multiple available services, that is, multiple serviceIds. The characteristic values ​​provided in each serviceId are different and need to be used in combination.
insert image description here

function getBLEDeviceCharacteristics(deviceId, serviceId, callBack) {
    
    
  wx.getBLEDeviceCharacteristics({
    
    
    deviceId,
    serviceId,
    success: (res) => {
    
    
      console.log('getBLEDeviceCharacteristics success', res.characteristics)
      for (let i = 0; i < res.characteristics.length; i++) {
    
    
        let item = res.characteristics[i]

        console.log("characteristics "+gloableBlueName+" read "+item.properties.read)
        console.log("characteristics "+gloableBlueName+" write "+item.properties.write)
        console.log("characteristics "+gloableBlueName+" notify "+item.properties.notify)
        console.log("characteristics "+gloableBlueName+" indicate "+item.properties.indicate)

        if (item.properties.read) {
    
    
        //可读数据
          wx.readBLECharacteristicValue({
    
    
            deviceId,
            serviceId,
            characteristicId: item.uuid,
          })
        }
        if (item.properties.write) {
    
    
          // 可写数据
        }
        }
      }
    },
    fail(res) {
    
    
      console.error('获取特征值失败:', res)
    },
    complete() {
    
    
      if (callBack) {
    
    
        console.log('回调 canWrite '+canWrite.toString());
        //我这里主要是用到了可写数据操作
        callBack(canWrite);
      }
    }
  })

Guess you like

Origin blog.csdn.net/zl18603543572/article/details/122349767