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

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

说明:

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

导入模块

import wearDetection from '@ohos.bluetooth.wearDetection';

wearDetection.enableWearDetection11+

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

使能佩戴检测。

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

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

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

参数:

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

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
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>

使能佩戴检测。

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

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

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

参数:

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

返回值:

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

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
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

禁用佩戴检测。

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

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

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

参数:

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

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
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>

禁用佩戴检测。

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

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

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

参数:

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

返回值:

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

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
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

获取设备是否支持佩戴检测。

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

需要权限:ohos.permission.ACCESS_BLUETOOTH

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

参数:

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

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
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>

获取设备是否支持佩戴检测。

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

需要权限:ohos.permission.ACCESS_BLUETOOTH

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

参数:

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

返回值:

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

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
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

获取设备佩戴检测是否开启。

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

需要权限:ohos.permission.ACCESS_BLUETOOTH

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

参数:

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

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
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>

获取设备佩戴检测是否开启。

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

需要权限:ohos.permission.ACCESS_BLUETOOTH

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

参数:

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

返回值:

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

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
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);
}