Alipay JSSDK V3.1.1

ap.notifyBLECharacteristicValueChange(OPTION, CALLBACK)

启用低功耗蓝牙设备特征值变化时的 notify 功能。注意:必须设备的特征值支持notify才可以成功调用,具体参照 characteristic 的 properties 属性 另外,必须先启用notify才能监听到设备 characteristicValueChange 事件。

OPTION 参数说明

名称类型必填描述
deviceIdString蓝牙设备 id,参考 device 对象
serviceIdString蓝牙特征值对应 service 的 uuid
characteristicIdString蓝牙特征值的 uuid
descriptorIdStringnotify 的 descriptor 的 uuid
stateBoolean是否启用notify或indicate

错误码说明

error描述
12启动失败

代码示例

<script src="https://gw.alipayobjects.com/as/g/h5-lib/alipayjsapi/3.1.1/alipayjsapi.inc.min.js"></script>

<button id="J_btn" class="btn btn-default">启用 notify 功能</button>
<script>
  var btn = document.querySelector('#J_btn');
  btn.addEventListener('click', function(){
    ap.notifyBLECharacteristicValueChange({
      // 这里的 deviceId 需要在 getBluetoothDevices 或 onBluetoothDeviceFound 接口中获取
      deviceId: 'deviceId',
      // 这里的 serviceId 需要在 getBLEDeviceServices 接口中获取
      serviceId: 'serviceId',
      // 这里的 characteristicId 需要在 getBLEDeviceCharacteristics 接口中获取
      characteristicId: 'characteristicId',
      success: function(res){
        ap.alert('启动通知成功');
      },
      fail: function(res) {
        ap.showToast('启动通知失败');
      }
    });
  });
</script>

其他说明

  • 订阅操作成功后需要设备主动更新特征值的 value,才会触发 ap.onBLECharacteristicValueChange 。
  • 订阅方式效率比较高,推荐使用订阅代替 read 方式。