chrome.bluetoothLowEnergy

说明

chrome.bluetoothLowEnergy API 用于通过通用属性配置文件 (GATT) 与 Bluetooth Smart(低功耗)设备进行通信。

可用性

仅限 ChromeOS

清单

必须在清单中声明以下键,才能使用此 API。

"bluetooth"

类型

Advertisement

Chrome 47 及更高版本

属性

  • manufacturerData

    要包含在通告数据的“制造商特定数据”字段中的制造商特定数据列表。

  • serviceData

    ServiceData[] 可选

    要包含在通告数据的“服务数据”字段中的服务数据列表。

  • serviceUuids

    string[] 可选

    要包含在通告数据的“服务 UUID”字段中的 UUID 列表。这些 UUID 可以采用 16 位、32 位或 128 位格式。

  • solicitUuids

    string[] 可选

    要包含在广告数据的“Solicit UUIDs”字段中的 UUID 列表。这些 UUID 可以采用 16 位、32 位或 128 位格式。

  • 广告类型。

AdvertisementType

Chrome 47 及更高版本

广告类型。如果选择“广播”,则发送的通告类型将为 ADV_NONCONN_IND,并且设备将使用随机 MAC 地址进行广播。如果设置为“peripheral”,通告类型将为 ADV_IND 或 ADV_SCAN_IND,并且设备将使用真实的蓝牙适配器 MAC 地址进行广播。

枚举

"broadcast"

"peripheral"

Characteristic

属性

  • instanceId

    字符串 可选

    返回分配给此特征的标识符。使用实例 ID 来区分具有相同 UUID 的外围设备的特征,并进行接受特征标识符的函数调用。如果此实例表示远程特征,则存在。

  • 此特征的属性。

  • 服务

    服务 (可选)

    此特征所属的 GATT 服务。

  • uuid

    字符串

    特征的 UUID,例如 00002a37-0000-1000-8000-00805f9b34fb。

  • 值

    ArrayBuffer 可选

    当前缓存的特征值。当通过通知或指示读取或更新特征值时,此值会更新。

CharacteristicProperty

表示特征可能属性的值。特征权限是根据这些属性推断出来的。请参阅蓝牙 4.x 规范,了解每个属性的含义。

枚举

"broadcast"

"read"

"writeWithoutResponse"

"write"

"notify"

"indicate"

"authenticatedSignedWrites"

"extendedProperties"

"reliableWrite"

"writableAuxiliaries"

"encryptRead"

"encryptWrite"

"encryptAuthenticatedRead"

"encryptAuthenticatedWrite"

ConnectProperties

属性

  • 持久性

    布尔值

    一个标志,用于指示在应用的事件页面卸载时是否保持与设备的连接处于打开状态(请参阅管理应用生命周期)。默认值为 false.。

Descriptor

属性

  • 特征

    特征 可选

    此描述符所属的 GATT 特征。

  • instanceId

    字符串 可选

    返回分配给此描述符的标识符。使用实例 ID 区分具有相同 UUID 的外围设备的描述符,并进行接受描述符标识符的函数调用。如果此实例表示远程特征,则存在。

  • Chrome 52 及更高版本

    此描述符的权限。

  • uuid

    字符串

    特征描述符的 UUID,例如 00002902-0000-1000-8000-00805f9b34fb。

  • 值

    ArrayBuffer 可选

    当前缓存的描述符值。读取描述符的值时,此值会更新。

DescriptorPermission

Chrome 52 及更高版本

表示描述符可能具有的权限的值。请参阅蓝牙 4.x 规范,了解每项权限的含义。

枚举

"read"

"write"

"encryptedRead"

"encryptedWrite"

"encryptedAuthenticatedRead"

"encryptedAuthenticatedWrite"

Device

Chrome 52 及更高版本

属性

  • 地址

    字符串

    设备的地址,格式为“XX:XX:XX:XX:XX:XX”。

  • deviceClass

    number 可选

    设备的类别,由 http://www.bluetooth.org/en-us/specification/assigned-numbers/baseband 定义的位字段。

  • name

    字符串 可选

    设备的人类可读名称。

ManufacturerData

Chrome 47 及更高版本

属性

  • 数据

    number[]

  • id

    数值

Notification

Chrome 52 及更高版本

属性

  • shouldIndicate

    布尔值 (可选)

    用于发送指示(而非通知)的可选标志。

  • 值

    ArrayBuffer

    特征的新值。

NotificationProperties

属性

  • 持久性

    布尔值

    一个标志,用于指示当应用的事件页面卸载时,应用是否应收到通知(请参阅管理应用生命周期)。默认值为 false。

Request

Chrome 52 及更高版本

属性

  • 设备

    发送此请求的设备。

  • requestId

    数值

    此请求的唯一 ID。在回复此请求时,请使用此 ID。

  • 值

    ArrayBuffer 可选

    要写入的值(如果这是写入请求)。

Response

Chrome 52 及更高版本

属性

  • isError

    布尔值

    如果这是错误响应,则应为 true。

  • requestId

    数值

    此响应所对应的请求的 ID。

  • 值

    ArrayBuffer 可选

    响应值。写入请求和错误响应将忽略此参数。

Service

属性

  • deviceAddress

    字符串 可选

    GATT 服务所属的远程外围设备的设备地址。如果此实例表示远程服务,则存在。

  • instanceId

    字符串 可选

    返回分配给此服务的标识符。使用实例 ID 区分具有相同 UUID 的外围设备中的服务,并进行接受服务标识符的函数调用。如果此实例表示远程服务,则存在。

  • isPrimary

    布尔值

    指明此服务的类型是主要还是次要。

  • uuid

    字符串

    服务的 UUID,例如 0000180d-0000-1000-8000-00805f9b34fb。

ServiceData

Chrome 47 及更高版本

属性

  • 数据

    number[]

  • uuid

    字符串

方法

connect()

Promise
chrome.bluetoothLowEnergy.connect(
  deviceAddress: string,
  properties?: ConnectProperties,
  callback?: function,
)
: Promise<void>

在应用与具有给定地址的设备之间建立连接。即使不调用 connect,设备也可能已连接,并且其 GATT 服务可用,但想要访问设备 GATT 服务的应用应调用此函数,以确保与设备的连接保持不变。如果设备未连接,则在成功调用 connect 后,系统会发现设备的所有 GATT 服务。

参数

  • deviceAddress

    字符串

    应打开 GATT 连接的远程设备的蓝牙地址。

  • 属性

    连接属性(可选)。

  • callback

    函数可选

    callback 参数如下所示:

    () => void

返回

  • 承诺<void>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

createCharacteristic()

Promise Chrome 52 及更高版本
chrome.bluetoothLowEnergy.createCharacteristic(
  characteristic: Characteristic,
  serviceId: string,
  callback?: function,
)
: Promise<string>

创建本地托管的 GATT 特征。此特征必须托管在有效的服务下。如果服务 ID 无效,系统会设置 lastError。仅当应用的 bluetooth:low_energy 和 bluetooth:peripheral 权限均设为 true 时,此函数才可用。外围设备权限可能不适用于所有应用。

参数

  • 特征

    要创建的特征。

  • serviceId

    字符串

    要为其创建此特征的服务的 ID。

  • callback

    函数可选

    callback 参数如下所示:

    (characteristicId: string) => void

    • characteristicId

      字符串

返回

  • Promise<string>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

createDescriptor()

Promise Chrome 52 及更高版本
chrome.bluetoothLowEnergy.createDescriptor(
  descriptor: Descriptor,
  characteristicId: string,
  callback?: function,
)
: Promise<string>

创建本地托管的 GATT 描述符。此描述符必须托管在有效的特征下。如果特征 ID 无效,系统会设置 lastError。仅当应用的 bluetooth:low_energy 和 bluetooth:peripheral 权限均设为 true 时,此函数才可用。外围设备权限可能不适用于所有应用。

参数

  • 描述符

    要创建的描述符。

  • characteristicId

    字符串

    要为其创建此描述符的特征的 ID。

  • callback

    函数可选

    callback 参数如下所示:

    (descriptorId: string) => void

    • descriptorId

      字符串

返回

  • Promise<string>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

createService()

Promise Chrome 52 及更高版本
chrome.bluetoothLowEnergy.createService(
  service: Service,
  callback?: function,
)
: Promise<string>

创建本地托管的 GATT 服务。此服务可以注册为在本地 GATT 服务器上可用。仅当应用的 bluetooth:low_energy 和 bluetooth:peripheral 权限均设为 true 时,此函数才可用。外围设备权限可能不适用于所有应用。

参数

  • 服务

    要创建的服务。

  • callback

    函数可选

    callback 参数如下所示:

    (serviceId: string) => void

    • serviceId

      字符串

返回

  • Promise<string>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

disconnect()

Promise
chrome.bluetoothLowEnergy.disconnect(
  deviceAddress: string,
  callback?: function,
)
: Promise<void>

关闭应用与具有指定地址的设备的连接。请注意,这不一定会销毁物理链路本身,因为可能还有其他应用具有打开的连接。

参数

  • deviceAddress

    字符串

    远程设备的蓝牙地址。

  • callback

    函数可选

    callback 参数如下所示:

    () => void

返回

  • 承诺<void>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

getCharacteristic()

Promise
chrome.bluetoothLowEnergy.getCharacteristic(
  characteristicId: string,
  callback?: function,
)
: Promise<Characteristic>

获取属于给定 GATT 服务的具有给定实例 ID 的 GATT 特征(如果该特征存在)。

参数

  • characteristicId

    字符串

    所请求 GATT 特征的实例 ID。

  • callback

    函数可选

    callback 参数如下所示:

    (result: Characteristic) => void

返回

  • Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

getCharacteristics()

Promise
chrome.bluetoothLowEnergy.getCharacteristics(
  serviceId: string,
  callback?: function,
)
: Promise<Characteristic[]>

获取属于给定服务的所有已发现 GATT 特征的列表。

参数

  • serviceId

    字符串

    应返回其特征的 GATT 服务的实例 ID。

  • callback

    函数可选

    callback 参数如下所示:

    (result: Characteristic[]) => void

返回

  • Promise<Characteristic[]>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

getDescriptor()

Promise
chrome.bluetoothLowEnergy.getDescriptor(
  descriptorId: string,
  callback?: function,
)
: Promise<Descriptor>

获取具有给定实例 ID 的 GATT 特征描述符。

参数

  • descriptorId

    字符串

    所请求的 GATT 特征描述符的实例 ID。

  • callback

    函数可选

    callback 参数如下所示:

    (result: Descriptor) => void

返回

  • Promise<Descriptor>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

getDescriptors()

Promise
chrome.bluetoothLowEnergy.getDescriptors(
  characteristicId: string,
  callback?: function,
)
: Promise<Descriptor[]>

获取属于给定特征的 GATT 特征描述符列表。

参数

  • characteristicId

    字符串

    应返回其描述符的 GATT 特征的实例 ID。

  • callback

    函数可选

    callback 参数如下所示:

    (result: Descriptor[]) => void

返回

  • Promise<Descriptor[]>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

getIncludedServices()

Promise
chrome.bluetoothLowEnergy.getIncludedServices(
  serviceId: string,
  callback?: function,
)
: Promise<Service[]>

获取给定服务包含的 GATT 服务列表。

参数

  • serviceId

    字符串

    应返回其所含服务的 GATT 服务的实例 ID。

  • callback

    函数可选

    callback 参数如下所示:

    (result: Service[]) => void

返回

  • Promise<Service[]>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

getService()

Promise
chrome.bluetoothLowEnergy.getService(
  serviceId: string,
  callback?: function,
)
: Promise<Service>

获取具有给定实例 ID 的 GATT 服务。

参数

  • serviceId

    字符串

    所请求 GATT 服务的实例 ID。

  • callback

    函数可选

    callback 参数如下所示:

    (result: Service) => void

返回

  • Promise<Service>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

getServices()

Promise
chrome.bluetoothLowEnergy.getServices(
  deviceAddress: string,
  callback?: function,
)
: Promise<Service[]>

获取在具有给定设备地址的远程设备上发现的所有 GATT 服务。

注意:如果设备上的服务发现尚未完成,此 API 将返回一部分服务(可能为空)。解决方法是添加基于时间的延迟和/或重复调用,直到返回预期的服务数量。

参数

  • deviceAddress

    字符串

    应返回其 GATT 服务的远程设备的蓝牙地址。

  • callback

    函数可选

    callback 参数如下所示:

    (result: Service[]) => void

返回

  • Promise<Service[]>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

notifyCharacteristicValueChanged()

Promise Chrome 52 及更高版本
chrome.bluetoothLowEnergy.notifyCharacteristicValueChanged(
  characteristicId: string,
  notification: Notification,
  callback?: function,
)
: Promise<void>

向远程设备通知特征的新值。如果通知对象中的 shouldIndicate 标志为 true,系统将发送指示,而不是通知。请注意,特征需要在创建期间正确设置“notify”或“indicate”属性,此调用才能成功。仅当应用的 bluetooth:low_energy 和 bluetooth:peripheral 权限均设为 true 时,此函数才可用。外围设备权限可能不适用于所有应用。

参数

  • characteristicId

    字符串

    要发送通知的特征。

  • 通知
  • callback

    函数可选

    callback 参数如下所示:

    () => void

返回

  • 承诺<void>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

readCharacteristicValue()

Promise
chrome.bluetoothLowEnergy.readCharacteristicValue(
  characteristicId: string,
  callback?: function,
)
: Promise<Characteristic>

从远程外围设备检索指定特征的值。

参数

  • characteristicId

    字符串

    应从远程设备读取其值的 GATT 特征的实例 ID。

  • callback

    函数可选

    callback 参数如下所示:

    (result: Characteristic) => void

返回

  • Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

readDescriptorValue()

Promise
chrome.bluetoothLowEnergy.readDescriptorValue(
  descriptorId: string,
  callback?: function,
)
: Promise<Descriptor>

从远程外围设备检索指定特征描述符的值。

参数

  • descriptorId

    字符串

    应从远程设备读取其值的 GATT 特征描述符的实例 ID。

  • callback

    函数可选

    callback 参数如下所示:

    (result: Descriptor) => void

返回

  • Promise<Descriptor>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

registerAdvertisement()

Promise Chrome 47 及更高版本
chrome.bluetoothLowEnergy.registerAdvertisement(
  advertisement: Advertisement,
  callback?: function,
)
: Promise<number>

创建通告并注册以进行通告。如需调用此函数,应用必须将 bluetooth:low_energy 和 bluetooth:peripheral 权限设置为 true。此外,此 API 仅适用于在自助服务终端模式下自动启动的应用,或通过设置“--enable-ble-advertising-in-apps”命令行开关来启用。请参阅 https://developer.chrome.com/apps/manifest/bluetooth。注意:在某些硬件上,同时支持中央模式和外围模式,但在不支持此模式的硬件上,进行此调用会将设备切换到外围模式。如果硬件不支持同时使用中央模式和外围模式,则尝试在两种模式下使用设备会导致未定义的行为,或导致其他中央角色应用无法正常运行(包括发现低功耗蓝牙设备)。

参数

  • 广告

    要通告的通告。

  • callback

    函数可选

    callback 参数如下所示:

    (advertisementId: number) => void

    • advertisementId

      数值

返回

  • <number>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

registerService()

Promise Chrome 52 及更高版本
chrome.bluetoothLowEnergy.registerService(
  serviceId: string,
  callback?: function,
)
: Promise<void>

向本地 GATT 服务器注册指定服务。如果服务 ID 无效,系统会设置 lastError。仅当应用的 bluetooth:low_energy 和 bluetooth:peripheral 权限均设为 true 时,此函数才可用。外围设备权限可能不适用于所有应用。

参数

  • serviceId

    字符串

    所创建服务的唯一 ID。

  • callback

    函数可选

    callback 参数如下所示:

    () => void

返回

  • 承诺<void>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

removeService()

Promise Chrome 52 及更高版本
chrome.bluetoothLowEnergy.removeService(
  serviceId: string,
  callback?: function,
)
: Promise<void>

移除指定的服务,如果该服务已注册,则将其取消注册。如果服务 ID 无效,系统会设置 lastError。仅当应用的 bluetooth:low_energy 和 bluetooth:peripheral 权限均设为 true 时,此函数才可用。外围设备权限可能不适用于所有应用。

参数

  • serviceId

    字符串

    当前已注册服务的唯一 ID。

  • callback

    函数可选

    callback 参数如下所示:

    () => void

返回

  • 承诺<void>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

resetAdvertising()

Promise Chrome 61 及更高版本
chrome.bluetoothLowEnergy.resetAdvertising(
  callback?: function,
)
: Promise<void>

重置当前设备上的广告。它将取消注册并停止所有现有通告。

参数

  • callback

    函数可选

    callback 参数如下所示:

    () => void

返回

  • 承诺<void>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

sendRequestResponse()

Chrome 52 及更高版本
chrome.bluetoothLowEnergy.sendRequestResponse(
  response: Response,
)
: void

发送对特征或描述符读/写请求的响应。仅当应用的 bluetooth:low_energy 和 bluetooth:peripheral 权限均设为 true 时,此函数才可用。外围设备权限可能不适用于所有应用。

参数

  • Response

    对请求的响应。

setAdvertisingInterval()

Promise Chrome 55 及更高版本
chrome.bluetoothLowEnergy.setAdvertisingInterval(
  minInterval: number,
  maxInterval: number,
  callback?: function,
)
: Promise<void>

设置两次连续通告之间的时间间隔。注意:这是一项尽力而为的操作。实际间隔可能与请求的间隔有很大差异。在某些硬件上,最小间隔为 100 毫秒。最小值和最大值不能超出蓝牙 4.2 规范允许的范围。

参数

  • minInterval

    数值

    通告之间的最短间隔(以毫秒为单位)。此值不能低于 20 毫秒(根据规范)。

  • maxInterval

    数值

    通告之间的最大时间间隔(以毫秒为单位)。此值不能超过 10240 毫秒(根据规范)。

  • callback

    函数可选

    callback 参数如下所示:

    () => void

返回

  • 承诺<void>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

startCharacteristicNotifications()

Promise
chrome.bluetoothLowEnergy.startCharacteristicNotifications(
  characteristicId: string,
  properties?: NotificationProperties,
  callback?: function,
)
: Promise<void>

启用来自指定特征的值通知/指示。启用后,应用可以使用 onCharacteristicValueChanged 事件监听通知。

参数

  • characteristicId

    字符串

    应启用通知的 GATT 特征的实例 ID。

  • 属性

    通知会话属性(可选)。

  • callback

    函数可选

    callback 参数如下所示:

    () => void

返回

  • 承诺<void>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

stopCharacteristicNotifications()

Promise
chrome.bluetoothLowEnergy.stopCharacteristicNotifications(
  characteristicId: string,
  callback?: function,
)
: Promise<void>

停用来自指定特征的值通知/指示。成功调用后,应用将停止接收来自此特征的通知/指示。

参数

  • characteristicId

    字符串

    应停止此应用的通知会话的 GATT 特征的实例 ID。

  • callback

    函数可选

    callback 参数如下所示:

    () => void

返回

  • 承诺<void>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

unregisterAdvertisement()

Promise Chrome 47 及更高版本
chrome.bluetoothLowEnergy.unregisterAdvertisement(
  advertisementId: number,
  callback?: function,
)
: Promise<void>

取消注册通告并停止其通告。如果广告未能取消注册,则停止通告的唯一方法可能是重启设备。

参数

  • advertisementId

    数值

    要取消注册的通告的 ID。

  • callback

    函数可选

    callback 参数如下所示:

    () => void

返回

  • 承诺<void>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

unregisterService()

Promise Chrome 52 及更高版本
chrome.bluetoothLowEnergy.unregisterService(
  serviceId: string,
  callback?: function,
)
: Promise<void>

从本地 GATT 服务器中取消注册给定服务。如果服务 ID 无效,系统会设置 lastError。仅当应用的 bluetooth:low_energy 和 bluetooth:peripheral 权限均设为 true 时,此函数才可用。外围设备权限可能不适用于所有应用。

参数

  • serviceId

    字符串

    当前已注册服务的唯一 ID。

  • callback

    函数可选

    callback 参数如下所示:

    () => void

返回

  • 承诺<void>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

writeCharacteristicValue()

Promise
chrome.bluetoothLowEnergy.writeCharacteristicValue(
  characteristicId: string,
  value: ArrayBuffer,
  callback?: function,
)
: Promise<void>

写入远程外围设备中指定特征的值。

参数

  • characteristicId

    字符串

    应写入其值的 GATT 特征的实例 ID。

  • 值

    ArrayBuffer

    应作为写入请求的一部分发送到远程特征的值。

  • callback

    函数可选

    callback 参数如下所示:

    () => void

返回

  • 承诺<void>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

writeDescriptorValue()

Promise
chrome.bluetoothLowEnergy.writeDescriptorValue(
  descriptorId: string,
  value: ArrayBuffer,
  callback?: function,
)
: Promise<void>

从远程外围设备写入指定特征描述符的值。

参数

  • descriptorId

    字符串

    应写入其值的 GATT 特征描述符的实例 ID。

  • 值

    ArrayBuffer

    应作为写入请求的一部分发送到远程描述符的值。

  • callback

    函数可选

    callback 参数如下所示:

    () => void

返回

  • 承诺<void>

    Chrome 91 及更高版本

    只有 Manifest V3 及更高版本支持 Promise,其他平台需要使用回调。

事件

onCharacteristicReadRequest

Chrome 52 及更高版本
chrome.bluetoothLowEnergy.onCharacteristicReadRequest.addListener(
  callback: function,
)

当已连接的中心设备请求读取在本地 GATT 服务器上注册的特征值时触发。如果长时间不回应此请求,可能会导致连接断开。仅当应用的 bluetooth:low_energy 和 bluetooth:peripheral 权限均设为 true 时,此事件才可用。外围设备权限可能不适用于所有应用。

参数

  • callback

    函数

    callback 参数如下所示:

    (request: Request, characteristicId: string) => void

    • request
    • characteristicId

      字符串

onCharacteristicValueChanged

chrome.bluetoothLowEnergy.onCharacteristicValueChanged.addListener(
  callback: function,
)

当远程 GATT 特征的值发生变化时(由于读取请求或值变化通知/指示),系统会触发此事件。只有在应用通过调用 startCharacteristicNotifications 启用了通知时,系统才会发送此事件。

参数

  • callback

    函数

    callback 参数如下所示:

    (characteristic: Characteristic) => void

onCharacteristicWriteRequest

Chrome 52 及更高版本
chrome.bluetoothLowEnergy.onCharacteristicWriteRequest.addListener(
  callback: function,
)

当已连接的中央设备请求写入在本地 GATT 服务器上注册的特征值时触发。如果长时间不回应此请求,可能会导致连接断开。仅当应用的 bluetooth:low_energy 和 bluetooth:peripheral 权限均设为 true 时,此事件才可用。外围设备权限可能不适用于所有应用。

参数

  • callback

    函数

    callback 参数如下所示:

    (request: Request, characteristicId: string) => void

    • request
    • characteristicId

      字符串

onDescriptorReadRequest

Chrome 52 及更高版本
chrome.bluetoothLowEnergy.onDescriptorReadRequest.addListener(
  callback: function,
)

当已连接的中央设备请求读取在本地 GATT 服务器上注册的描述符的值时触发。如果长时间不回应此请求,可能会导致连接断开。仅当应用的 bluetooth:low_energy 和 bluetooth:peripheral 权限均设为 true 时,此事件才可用。外围设备权限可能不适用于所有应用。

参数

  • callback

    函数

    callback 参数如下所示:

    (request: Request, descriptorId: string) => void

    • request
    • descriptorId

      字符串

onDescriptorValueChanged

chrome.bluetoothLowEnergy.onDescriptorValueChanged.addListener(
  callback: function,
)

当远程 GATT 特征描述符的值发生变化时(通常是由于读取请求)触发。此事件主要是为了方便而存在,并且始终会在成功调用 readDescriptorValue 后发送。

参数

  • callback

    函数

    callback 参数如下所示:

    (descriptor: Descriptor) => void

onDescriptorWriteRequest

Chrome 52 及更高版本
chrome.bluetoothLowEnergy.onDescriptorWriteRequest.addListener(
  callback: function,
)

当已连接的中央设备请求写入在本地 GATT 服务器上注册的描述符的值时触发。如果长时间不回应此请求,可能会导致连接断开。仅当应用的 bluetooth:low_energy 和 bluetooth:peripheral 权限均设为 true 时,此事件才可用。外围设备权限可能不适用于所有应用。

参数

  • callback

    函数

    callback 参数如下所示:

    (request: Request, descriptorId: string) => void

    • request
    • descriptorId

      字符串

onServiceAdded

chrome.bluetoothLowEnergy.onServiceAdded.addListener(
  callback: function,
)

在远程设备上发现新的 GATT 服务时触发。

参数

  • callback

    函数

    callback 参数如下所示:

    (service: Service) => void

onServiceChanged

chrome.bluetoothLowEnergy.onServiceChanged.addListener(
  callback: function,
)

在远程 GATT 服务的状态发生变化时触发。这包括从服务中添加或移除的任何特征和/或描述符,以及来自远程设备的“ServiceChanged”通知。

参数

  • callback

    函数

    callback 参数如下所示:

    (service: Service) => void

onServiceRemoved

chrome.bluetoothLowEnergy.onServiceRemoved.addListener(
  callback: function,
)

当之前在远程设备上发现的 GATT 服务被移除时触发。

参数

  • callback

    函数

    callback 参数如下所示:

    (service: Service) => void