文档管理中心
您当前浏览的HarmonyOS 5.0.0(API 12)文档归档不再维护,推荐您使用最新版本。详细请参考文档维护策略变更
API参考系统网络Connectivity Kit(短距通信服务)ArkTS API@ohos.bluetooth.access (蓝牙access模块)

@ohos.bluetooth.access (蓝牙access模块)

access模块提供了打开和关闭蓝牙、获取蓝牙状态的方法。

说明

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

导入模块

收起
自动换行
深色代码主题
复制
  1. import { access } from '@kit.ConnectivityKit';

access.enableBluetooth

enableBluetooth(): void

开启蓝牙。

需要权限:ohos.permission.ACCESS_BLUETOOTH

元服务API:从API version 12开始,该接口支持在元服务中使用。

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

错误码

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

展开
错误码ID 错误信息
201 Permission denied.
801 Capability not supported.
2900001 Service stopped.
2900099 Operation failed.

示例:

收起
自动换行
深色代码主题
复制
  1. import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
  2. try {
  3. access.enableBluetooth();
  4. } catch (err) {
  5. console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  6. }

access.disableBluetooth

disableBluetooth(): void

关闭蓝牙。

需要权限:ohos.permission.ACCESS_BLUETOOTH

元服务API:从API version 12开始,该接口支持在元服务中使用。

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

错误码

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

展开
错误码ID 错误信息
201 Permission denied.
801 Capability not supported.
2900001 Service stopped.
2900099 Operation failed.

示例:

收起
自动换行
深色代码主题
复制
  1. import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
  2. try {
  3. access.disableBluetooth();
  4. } catch (err) {
  5. console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  6. }

access.getState

getState(): BluetoothState

获取蓝牙开关状态。

需要权限:ohos.permission.ACCESS_BLUETOOTH

元服务API:从API version 11开始,该接口支持在元服务中使用。

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

返回值:

展开
类型 说明
BluetoothState 表示蓝牙开关状态。

错误码

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

展开
错误码ID 错误信息
201 Permission denied.
801 Capability not supported.
2900001 Service stopped.
2900099 Operation failed.

示例:

收起
自动换行
深色代码主题
复制
  1. import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
  2. try {
  3. let state = access.getState();
  4. } catch (err) {
  5. console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  6. }

access.on('stateChange')

on(type: 'stateChange', callback: Callback<BluetoothState>): void

订阅蓝牙设备开关状态事件。使用Callback异步回调。

需要权限:ohos.permission.ACCESS_BLUETOOTH

元服务API:从API version 12开始,该接口支持在元服务中使用。

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

参数:

展开
参数名 类型 必填 说明
type string 填写"stateChange"字符串,表示蓝牙状态改变事件。
callback Callback<BluetoothState> 表示回调函数的入参,蓝牙状态。回调函数由用户创建通过该接口注册。

错误码

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

展开
错误码ID 错误信息
201 Permission denied.
401 Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801 Capability not supported.
2900099 Operation failed.

示例:

收起
自动换行
深色代码主题
复制
  1. import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
  2. function onReceiveEvent(data: access.BluetoothState) {
  3. console.info('bluetooth state = '+ JSON.stringify(data));
  4. }
  5. try {
  6. access.on('stateChange', onReceiveEvent);
  7. } catch (err) {
  8. console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  9. }

access.off('stateChange')

off(type: 'stateChange', callback?: Callback<BluetoothState>): void

取消订阅蓝牙设备开关状态事件。

需要权限:ohos.permission.ACCESS_BLUETOOTH

元服务API:从API version 12开始,该接口支持在元服务中使用。

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

参数:

展开
参数名 类型 必填 说明
type string 填写"stateChange"字符串,表示蓝牙状态改变事件。
callback Callback<BluetoothState> 表示取消订阅蓝牙状态改变事件上报。不填该参数则取消订阅该type对应的所有回调。

错误码

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

展开
错误码ID 错误信息
201 Permission denied.
401 Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed.
801 Capability not supported.
2900099 Operation failed.

示例:

收起
自动换行
深色代码主题
复制
  1. import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
  2. function onReceiveEvent(data: access.BluetoothState) {
  3. console.info('bluetooth state = '+ JSON.stringify(data));
  4. }
  5. try {
  6. access.on('stateChange', onReceiveEvent);
  7. access.off('stateChange', onReceiveEvent);
  8. } catch (err) {
  9. console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  10. }

BluetoothState

枚举,蓝牙开关状态。

元服务API: 从API version 11开始,该接口支持在元服务中使用。

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

展开
名称 说明
STATE_OFF 0 表示蓝牙已关闭。
STATE_TURNING_ON 1 表示蓝牙正在打开。
STATE_ON 2 表示蓝牙已打开。
STATE_TURNING_OFF 3 表示蓝牙正在关闭。
STATE_BLE_TURNING_ON 4 表示蓝牙正在打开LE-only模式。
STATE_BLE_ON 5 表示蓝牙正处于LE-only模式。
STATE_BLE_TURNING_OFF 6 表示蓝牙正在关闭LE-only模式。
在 API参考 中进行搜索
请输入您想要搜索的关键词