@ohos.bluetoothManager (蓝牙)(系统接口)

蓝牙模块提供了基础的传统蓝牙能力以及BLE的扫描、广播等功能。

说明:

本模块首批接口从API version 9开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 从API Version 10 开始,该接口不再维护,推荐使用@ohos.bluetooth.ble等相关profile接口。 当前页面仅包含本模块的系统接口,其他公开接口参见@ohos.bluetoothManager

导入模块

import bluetoothManager from '@ohos.bluetoothManager';

bluetoothManager.cancelPairedDevice(deprecated)

cancelPairedDevice(deviceId: string): void

删除配对的远程设备。

说明:
从API version 9开始支持,从API version 10开始废弃。建议使用connection.cancelPairedDevice替代。

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

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

参数:

参数名 类型 必填 说明
deviceId string 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。

错误码

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

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    bluetoothManager.cancelPairedDevice("XX:XX:XX:XX:XX:XX");
} catch (err) {
    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
}

connect

connect(device: string): void

连接设备的HidHost服务。

说明:
从API version 9开始支持,从API version 10开始废弃。建议使用hid.HidHostProfile#connect替代。

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

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

参数:

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

错误码

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

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900004 Profile is not supported.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    let hidHostProfile: bluetoothManager.HidHostProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile;
    hidHostProfile.connect('XX:XX:XX:XX:XX:XX');
} catch (err) {
    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
}

disconnect(deprecated)

disconnect(device: string): void

断开连接设备的HidHost服务。

说明:
从API version 9开始支持,从API version 10开始废弃。建议使用hid.HidHostProfile#disconnect替代。

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

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

参数:

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

错误码

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

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900004 Profile is not supported.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    let hidHostProfile: bluetoothManager.HidHostProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_HID_HOST) as bluetoothManager.HidHostProfile;
    hidHostProfile.disconnect('XX:XX:XX:XX:XX:XX');
} catch (err) {
    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
}

disconnect(deprecated)

disconnect(device: string): void

断开连接设备的Pan服务。

说明:
从API version 9开始支持,从API version 10开始废弃。建议使用pan.PanProfile#disconnect替代。

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

需要权限:ohos.permission.USE_BLUETOOTH

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

参数:

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

错误码

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

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900004 Profile is not supported.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile;
    panProfile.disconnect('XX:XX:XX:XX:XX:XX');
} catch (err) {
    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
}

setTethering(deprecated)

setTethering(enable: boolean): void

设置网络共享状态。

说明:
从API version 9开始支持,从API version 10开始废弃。替代接口仅向系统应用开放。

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

需要权限:ohos.permission.DISCOVER_BLUETOOTH

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

参数:

参数名 类型 必填 说明
value boolean 是否设置蓝牙共享。

错误码

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

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900004 Profile is not supported.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile;
    panProfile.setTethering(true);
} catch (err) {
    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
}

isTetheringOn(deprecated)

isTetheringOn(): boolean

获取网络共享状态。

说明:
从API version 9开始支持,从API version 10开始废弃。建议使用pan.PanProfile#isTetheringOn替代。

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

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

返回值:

类型 说明
boolean 网络共享开启返回true,网络共享关闭返回false。

示例:

import { BusinessError } from '@ohos.base';
try {
    let panProfile: bluetoothManager.PanProfile = bluetoothManager.getProfileInstance(bluetoothManager.ProfileId.PROFILE_PAN_NETWORK) as bluetoothManager.PanProfile;
    panProfile.isTetheringOn();
} catch (err) {
    console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message);
}