harmony 鸿蒙@ohos.bluetooth.wearDetection(蓝牙佩戴检测模块)(系统接口)

  • 2025-06-12
  • 浏览 (4)

@ohos.bluetooth.wearDetection(蓝牙佩戴检测模块)(系统接口)

佩戴检测模块提供了获取蓝牙音频设备(如蓝牙耳机)是否支持佩戴检测、是否开启佩戴检测的方法。

说明:

本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 本模块为系统接口。

导入模块

import { wearDetection } from '@kit.ConnectivityKit';

wearDetection.enableWearDetection11+

enableWearDetection(deviceId: string, callback: AsyncCallback<void>): void

使能佩戴检测。使用Callback异步回调。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH和ohos.permission.MANAGE_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core

参数:

参数名 类型 必填 说明
deviceId string 远端设备地址。
callback AsyncCallback<void> 通过注册回调函数获取使能佩戴检测的结果。如果成功,err为undefined,否则为错误对象。

错误码

以下错误码的详细介绍请参见通用错误码说明文档蓝牙服务子系统错误码

错误码ID 错误信息
201 Permission denied.
202 Non-system applications are not allowed to use system APIs.
401 Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801 Capability not supported.
2900001 Service stopped.
2900003 Bluetooth disabled.
2900099 Operation failed.

示例:

try {
    wearDetection.enableWearDetection('XX:XX:XX:XX:XX:XX', (err) => {
        if (err) {
            console.error("enableWearDetection error");
        }
    });
} catch (err) {
    console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}

wearDetection.enableWearDetection11+

enableWearDetection(deviceId: string): Promise<void>

使能佩戴检测。使用Promise异步回调。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH和ohos.permission.MANAGE_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core

参数:

参数名 类型 必填 说明
deviceId string 远端设备地址。

返回值:

类型 说明
Promise<void> 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。

错误码

以下错误码的详细介绍请参见通用错误码说明文档蓝牙服务子系统错误码

错误码ID 错误信息
201 Permission denied.
202 Non-system applications are not allowed to use system APIs.
401 Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801 Capability not supported.
2900001 Service stopped.
2900003 Bluetooth disabled.
2900099 Operation failed.

示例:

try {
    wearDetection.enableWearDetection('XX:XX:XX:XX:XX:XX').then(() => {
        console.info("enableWearDetection");
    });
} catch (err) {
    console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}

wearDetection.disableWearDetection11+

disableWearDetection(deviceId: string, callback: AsyncCallback<void>): void

禁用佩戴检测。使用Callback异步回调。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH和ohos.permission.MANAGE_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core

参数:

参数名 类型 必填 说明
deviceId string 远端设备地址。
callback AsyncCallback<void> 通过注册回调函数获取禁用佩戴检测的结果。如果成功,err为undefined,否则为错误对象。

错误码

以下错误码的详细介绍请参见通用错误码说明文档蓝牙服务子系统错误码

错误码ID 错误信息
201 Permission denied.
202 Non-system applications are not allowed to use system APIs.
401 Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801 Capability not supported.
2900001 Service stopped.
2900003 Bluetooth disabled.
2900099 Operation failed.

示例:

try {
    wearDetection.disableWearDetection('XX:XX:XX:XX:XX:XX', (err) => {
        if (err) {
            console.error("disableWearDetection error");
        }
    });
} catch (err) {
    console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}

wearDetection.disableWearDetection11+

disableWearDetection(deviceId: string): Promise<void>

禁用佩戴检测。使用Promise异步回调。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH和ohos.permission.MANAGE_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core

参数:

参数名 类型 必填 说明
deviceId string 远端设备地址。

返回值:

类型 说明
Promise<void> 以Promise的形式返回结果。如果成功,err为undefined,否则为错误对象。

错误码

以下错误码的详细介绍请参见通用错误码说明文档蓝牙服务子系统错误码

错误码ID 错误信息
201 Permission denied.
202 Non-system applications are not allowed to use system APIs.
401 Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801 Capability not supported.
2900001 Service stopped.
2900003 Bluetooth disabled.
2900099 Operation failed.

示例:

try {
    wearDetection.disableWearDetection('XX:XX:XX:XX:XX:XX').then(() => {
        console.info("disableWearDetection");
    });
} catch (err) {
    console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}

wearDetection.isWearDetectionSupported11+

isWearDetectionSupported(deviceId: string, callback: AsyncCallback<boolean>): void

获取设备是否支持佩戴检测。使用Callback异步回调。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core

参数:

参数名 类型 必填 说明
deviceId string 远端设备地址。
callback AsyncCallback<boolean> 通过注册回调函数获取设备是否支持佩戴检测。如果成功,值在supported中返回。如果失败,返回对应错误码。

错误码

以下错误码的详细介绍请参见通用错误码说明文档蓝牙服务子系统错误码

错误码ID 错误信息
201 Permission denied.
202 Non-system applications are not allowed to use system APIs.
401 Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801 Capability not supported.
2900001 Service stopped.
2900003 Bluetooth disabled.
2900099 Operation failed.

示例:

try {
    wearDetection.isWearDetectionSupported('XX:XX:XX:XX:XX:XX', (err, supported) => {
        console.info('device support wear detection ' + supported);
    });
} catch (err) {
    console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}

wearDetection.isWearDetectionSupported11+

isWearDetectionSupported(deviceId: string): Promise<boolean>

获取设备是否支持佩戴检测。使用Promise异步回调。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core

参数:

参数名 类型 必填 说明
deviceId string 远端设备地址。

返回值:

类型 说明
Promise<boolean> 以Promise的形式获取设备是否支持佩戴检测。如果成功,值在supported中返回。如果失败,返回对应错误码。

错误码

以下错误码的详细介绍请参见通用错误码说明文档蓝牙服务子系统错误码

错误码ID 错误信息
201 Permission denied.
202 Non-system applications are not allowed to use system APIs.
401 Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801 Capability not supported.
2900001 Service stopped.
2900003 Bluetooth disabled.
2900099 Operation failed.

示例:

try {
    wearDetection.isWearDetectionSupported('XX:XX:XX:XX:XX:XX').then((supported) => {
        console.info('device support wear detection ' + supported);
    });
} catch (err) {
    console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}

wearDetection.isWearDetectionEnabled11+

isWearDetectionEnabled(deviceId: string, callback: AsyncCallback<boolean>): void

获取设备佩戴检测是否开启。使用Callback异步回调。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core

参数:

参数名 类型 必填 说明
deviceId string 远端设备地址。
callback AsyncCallback<boolean> 通过注册回调函数获取设备佩戴检测是否开启。如果成功,值在enabled中返回。如果失败,返回对应错误码。

错误码

以下错误码的详细介绍请参见通用错误码说明文档蓝牙服务子系统错误码

错误码ID 错误信息
201 Permission denied.
202 Non-system applications are not allowed to use system APIs.
401 Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801 Capability not supported.
2900001 Service stopped.
2900003 Bluetooth disabled.
2900099 Operation failed.

示例:

try {
    wearDetection.isWearDetectionEnabled('XX:XX:XX:XX:XX:XX', (err, enabled) => {
        console.info('device enable wear detection ' + enabled);
    });
} catch (err) {
    console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}

wearDetection.isWearDetectionEnabled11+

isWearDetectionEnabled(deviceId: string): Promise<boolean>

获取设备佩戴检测是否开启。使用Promise异步回调。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core

参数:

参数名 类型 必填 说明
deviceId string 远端设备地址。

返回值:

类型 说明
Promise<boolean> 以Promise的形式获取设备佩戴检测是否开启。如果成功,值在enabled中返回。如果失败,返回对应错误码。

错误码

以下错误码的详细介绍请参见通用错误码说明文档蓝牙服务子系统错误码

错误码ID 错误信息
201 Permission denied.
202 Non-system applications are not allowed to use system APIs.
401 Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801 Capability not supported.
2900001 Service stopped.
2900003 Bluetooth disabled.
2900099 Operation failed.

示例:

try {
    wearDetection.isWearDetectionEnabled('XX:XX:XX:XX:XX:XX').then((enabled) => {
        console.info('device enable wear detection ' + enabled);
    });
} catch (err) {
    console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}

你可能感兴趣的鸿蒙文章

harmony 鸿蒙Connectivity Kit(短距通信服务)

harmony 鸿蒙Bluetooth

harmony 鸿蒙Wifi

harmony 鸿蒙蓝牙服务子系统错误码

harmony 鸿蒙NFC错误码

harmony 鸿蒙SE(secureElement)错误码

harmony 鸿蒙WIFI错误码

harmony 鸿蒙@ohos.bluetooth.a2dp (蓝牙a2dp模块)(系统接口)

harmony 鸿蒙@ohos.bluetooth.a2dp (蓝牙a2dp模块)

harmony 鸿蒙@ohos.bluetooth.access (蓝牙access模块)(系统接口)

0  赞