BluetoothCharacteristicProperties

可用性有限

此特性不是基线特性,因为它在一些最广泛使用的浏览器中不起作用。

安全上下文: 此功能仅在安全上下文(HTTPS)中可用,且支持此功能的浏览器数量有限。

实验性: 这是一项实验性技术
在生产中使用此技术之前,请仔细检查浏览器兼容性表格

BluetoothCharacteristicProperties 接口是 Web Bluetooth API 的一部分,它提供了在给定的 BluetoothRemoteGATTCharacteristic 上有效的操作。

通过调用 BluetoothRemoteGATTCharacteristic.properties 来返回此接口。

实例属性

authenticatedSignedWrites 只读 实验性

返回一个 boolean 值,如果允许对特性值进行签名写入,则为 true

broadcast 只读 实验性

返回一个 boolean 值,如果允许使用服务器特性配置描述符广播特性值,则为 true

indicate 只读 实验性

返回一个 boolean 值,如果允许带有确认的特性值指示,则为 true

notify 只读 实验性

返回一个 boolean 值,如果允许不带确认的特性值通知,则为 true

read 只读 实验性

返回一个 boolean 值,如果允许读取特性值,则为 true

reliableWrite 只读 实验性

返回一个 boolean 值,如果允许对特性进行可靠写入,则为 true

writableAuxiliaries 只读 实验性

返回一个 boolean 值,如果允许对特性的描述符进行可靠写入,则为 true

write 只读 实验性

返回一个 boolean 值,如果允许带有响应的特性写入,则为 true

writeWithoutResponse 只读 实验性

返回一个 boolean 值,如果允许不带响应的特性写入,则为 true

示例

以下示例展示了如何检查 GATT 特性是否支持值更改通知。

js
let device = await navigator.bluetooth.requestDevice({
  filters: [{ services: ["heart_rate"] }],
});
let gatt = await device.gatt.connect();
let service = await gatt.getPrimaryService("heart_rate");
let characteristic = await service.getCharacteristic("heart_rate_measurement");
if (characteristic.properties.notify) {
  characteristic.addEventListener(
    "characteristicvaluechanged",
    async (event) => {
      console.log(`Received heart rate measurement: ${event.target.value}`);
    },
  );
  await characteristic.startNotifications();
}

规范

规范
Web 蓝牙
# characteristicproperties-interface

浏览器兼容性