likes
comments
collection
share

uni-app实现微信小程序蓝牙打印

作者站长头像
站长
· 阅读数 14

打印流程

小程序连接蓝牙打印大致可以分为九步:1.初始化蓝牙模块、2.开始搜索附近的蓝牙设备、3.获取搜索到的蓝牙列表、4.监听寻找到新设备的事件、5.连接蓝牙设备、6.关闭搜索蓝牙设备事件、7.获取蓝牙设备的所有服务、8.获取服务的所有特征值、9.向蓝牙设备写入数据

1.初始化蓝牙模块 uni.openBluetoothAdapter

注意:其他蓝牙相关 API 必须在 uni.openBluetoothAdapter 调用之后使用。

uni.openBluetoothAdapter({
  success(res) {
    console.log(res)
  }
})

2.开始搜索附近的蓝牙设备 uni.startBluetoothDevicesDiscovery

此操作比较耗费系统资源,请在搜索并连接到设备后调用 uni.stopBluetoothDevicesDiscovery 方法停止搜索。

uni.startBluetoothDevicesDiscovery({
  success(res) {
    console.log(res)
  }
})

3.获取搜索到的蓝牙列表 uni.getBluetoothDevices

获取在蓝牙模块生效期间所有已发现的蓝牙设备。包括已经和本机处于连接状态的设备(不是很准确,有时会获取不到)。

uni.getBluetoothDevices({
  success(res) {
    console.log(res)
  }
})

4.监听寻找到新设备的事件 uni.onBluetoothDeviceFound

监听寻找到新设备的事件,跟第三步一起使用,确保能获取附近所有蓝牙设备。

uni.onBluetoothDeviceFound(function (devices) {
  console.log(devices)
})

5.连接蓝牙设备 uni.createBLEConnection

若APP在之前已有搜索过某个蓝牙设备,并成功建立连接,可直接传入之前搜索获取的 deviceId 直接尝试连接该设备,避免用户每次都要连接才能打印,省略二三四步减少资源浪费。

uni.createBLEConnection({
  deviceId:获取到蓝牙的deviceId,
  success(res) {
    console.log(res)
  }
})

6.关闭搜索蓝牙设备事件 uni.stopBluetoothDevicesDiscovery

停止搜寻附近的蓝牙外围设备。若已经找到需要的蓝牙设备并不需要继续搜索时,建议调用该接口停止蓝牙搜索。

uni.stopBluetoothDevicesDiscovery({
  success(res) {
    console.log(res)
  }
})

7.获取蓝牙设备的所有服务 uni.getBLEDeviceServices

uni.getBLEDeviceServices({
  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  deviceId,
  success(res) {
    console.log('device services:', res.services)
  }
})

8.获取服务的所有特征值 uni.getBLEDeviceCharacteristics

uni.getBLEDeviceCharacteristics({
  // 这里的 deviceId 需要已经通过 createBLEConnection 与对应设备建立链接
  deviceId,
  // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  serviceId,
  success(res) {
    console.log('device getBLEDeviceCharacteristics:', res.characteristics)
  }
})
三种不同特征值的id
for (var i = 0; i < res.characteristics.length; i++) {
					if (!notify) {
						notify = res.characteristics[i].properties.notify;
						if (notify) readId = res.characteristics[i].uuid;
					}
					if (!indicate) {
						indicate = res.characteristics[i].properties.indicate;
						if (indicate) readId = res.characteristics[i].uuid;
					}
					if (!write) {
						write = res.characteristics[i].properties.write;
						writeId = res.characteristics[i].uuid;
					}
					if ((notify || indicate) && write) {
						/* 获取蓝牙特征值uuid */
						success &&
							success({
								serviceId,
								writeId: writeId,
								readId: readId,
							});
						finished = true;
						break;
					}

9.向蓝牙设备写入数据 uni.writeBLECharacteristicValue

向低功耗蓝牙设备特征值中写入二进制数据。注意:必须设备的特征值支持 write 才可以成功调用。

并行调用多次会存在写失败的可能性。

APP不会对写入数据包大小做限制,但系统与蓝牙设备会限制蓝牙4.0单次传输的数据大小,超过最大字节数后会发生写入错误,建议每次写入不超过20字节。

若单次写入数据过长,iOS 上存在系统不会有任何回调的情况(包括错误回调)。

安卓平台上,在调用 notifyBLECharacteristicValueChange 成功后立即调用 writeBLECharacteristicValue 接口,在部分机型上会发生 10008 系统错误

// 向蓝牙设备发送一个0x00的16进制数据
const buffer = new ArrayBuffer(1)
const dataView = new DataView(buffer)
dataView.setUint8(0, 0)
uni.writeBLECharacteristicValue({
  // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
  deviceId,
  // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  serviceId,
  // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  characteristicId,
  // 这里的value是ArrayBuffer类型
  value: buffer,
  success(res) {
    console.log('writeBLECharacteristicValue success', res.errMsg)
  }
})

写在最后

DEMO地址:gitee.com/zhou_xuhui/… (plus可能会报错,demo中注释掉就好,不影响流程)

打印机CPCL编程参考手册(CPCL 语言):www.docin.com/p-216010502…