# tap.writeBLECharacteristicValue(Object object)

以 Promise 风格调用:支持

# 功能描述

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

# 参数

# Object object

属性类型默认值必填说明
deviceIdstring蓝牙设备 id
serviceIdstring蓝牙特征对应服务的 UUID
characteristicIdstring蓝牙特征的 UUID
valueArrayBuffer蓝牙设备特征对应的二进制值
writeTypestring蓝牙特征值的写模式设置,有两种模式,iOS 优先 write,安卓优先 writeNoResponse 。
合法值说明
write强制回复写,不支持时报错
writeNoResponse强制无回复写,不支持时报错
successfunction接口调用成功的回调函数
failfunction接口调用失败的回调函数
completefunction接口调用结束的回调函数(调用成功、失败都会执行)

# 错误

错误码错误信息说明
0ok正常
-1already connect已连接
10000not init未初始化蓝牙适配器
10001not available当前蓝牙适配器不可用
10002no device没有找到指定设备
10003connection fail连接失败
10004no service没有找到指定服务
10005no characteristic没有找到指定特征
10006no connection当前连接已断开
10007property not support当前特征不支持此操作
10008system error其余所有系统上报的异常
10009system not supportAndroid 系统特有,系统版本低于 4.3 不支持 BLE
10012operate time out连接超时
10013invalid_data连接 deviceId 为空或者是格式不正确

# 注意

  • 并行调用多次会存在写失败的可能性。
  • 小游戏不会对写入数据包大小做限制,但系统与蓝牙设备会限制蓝牙 4.0 单次传输的数据大小,超过最大字节数后会发生写入错误,建议每次写入不超过 20 字节。
  • 若单次写入数据过长,iOS 上存在系统不会有任何回调的情况(包括错误回调)。
  • 安卓平台上,在调用 tap.notifyBLECharacteristicValueChange 成功后立即调用本接口,在部分机型上会发生 10008 系统错误

# 示例代码

// 向蓝牙设备发送一个0x00的16进制数据
let buffer = new ArrayBuffer(1)
let dataView = new DataView(buffer)
dataView.setUint8(0, 0)

tap.writeBLECharacteristicValue({
  // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
  deviceId,
  // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
  serviceId,
  // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
  characteristicId,
  // 这里的value是ArrayBuffer类型
  value: buffer,
  success (res) {
    console.log('writeBLECharacteristicValue success', res.errMsg)
  }
})