建议增加writeBLECharacteristicValue 蓝牙写类型type参数设置
发布于 5 年前 作者 yong94 5168 次浏览 来自 问答
一、微信小程序代码段
https://developers.weixin.qq.com/miniprogram/dev/api/device/bluetooth-ble/wx.writeBLECharacteristicValue.html// 向蓝牙设备发送一个0x00的16进制数据
let buffer = new ArrayBuffer(1)
let dataView = new DataView(buffer)
dataView.setUint8(0, 0)

wx.writeBLECharacteristicValue({
  // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
  deviceId,
  // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  serviceId,
  // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  characteristicId,
  // 这里的value是ArrayBuffer类型
  value: buffer,
  success (res) {
    console.log('writeBLECharacteristicValue success', res.errMsg)
  }
})
二、根据蓝牙BLE核心规范(针对4.0以上所有版本适用),writeBLECharacteristicValue是有三种种写参数设置。
第一种是WriteWithoutResponse(BLUETOOTH SPECIFICATION Version 5.0 | Vol 3, Part G 4.9.1 Write Without Response)
第二种是Signed Write Without Response(BLUETOOTH SPECIFICATION Version 5.0 | Vol 3, Part G 4.9.2 Signed Write Without Response)
第三种是WriteRequest是有WriteRsponse。(BLUETOOTH SPECIFICATION Version 5.0 | Vol 3, Part G 4.9.3 Write Characteristic Value)

三、对应的安卓和IOS手机系统都有相关的API,
1、安卓如下:
/**
 * Write characteristic, requesting acknoledgement by the remote device
 */
public static final int WRITE_TYPE_DEFAULT = 0x02;
/**
 * Wrtite characteristic without requiring a response by the remote device
 */
public static final int WRITE_TYPE_NO_RESPONSE = 0x01;
/**
 * Write characteristic including authentication signature
 */
public static final int WRITE_TYPE_SIGNED = 0x04;
characteristic_ota_update.setWriteType(BluetoothGattCharacteristic.WRITE_TYPE_DEFAULT);
2、IOS如下
[connectPeripheral writeValue:data forCharacteristic:writeCBCharacteristic type:CBCharacteristicWriteWithoutResponse];//第三个参数type是通过此响应不判断是否成功
[connectPeripheral writeValue:data forCharacteristic:writeCBCharacteristic type:CBCharacteristicWriteWithResponse];//第三个参数type是通过此响应会判断是否成功

四,希望微信小程序writeBLECharacteristicValue的API增加相关的Type类型设置,
目前微信小程序type在都是使用系统默认的设置(安卓的默认type为WriteWithoutResponse,IOS的默认type为WriteWithResponse),这就导致安卓的BLE传输数据比IOS的BLE传输数据快很多。
使得用户体验不佳,希望增加相应type设置,让开发者自己选择type类型,以解决相关IOS传输BLE数据慢的问题。

回到顶部