蓝牙设置
====

#### 简介

本指南主要提供了开启蓝牙、关闭蓝牙和获取蓝牙开关状态的开发指导。开发者在使用蓝牙其他功能时，应确保蓝牙子系统已正常开启。

#### 开发步骤

#### \[h2\]申请蓝牙权限

需要申请权限ohos.permission.ACCESS\_BLUETOOTH。如何配置和申请权限，具体操作请参考[声明权限](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/declare-permissions)和[向用户申请授权](https://developer.huawei.com/consumer/cn/doc/harmonyos-guides/request-user-authorization)。

#### \[h2\]导入所需API模块

导入access和错误码模块。

import { access } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';

#### \[h2\]订阅蓝牙开关状态变化事件

通过订阅开关状态变化事件，可以获取实时的蓝牙开关状态。蓝牙开启或者关闭过程中，涉及多种蓝牙状态的跃迁。其中[STATE\_OFF](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-bluetooth-access#bluetoothstate)表示蓝牙已关闭，此状态下，应用不可以使用蓝牙其他功能；[STATE\_ON](https://developer.huawei.com/consumer/cn/doc/harmonyos-references/js-apis-bluetooth-access#bluetoothstate)表示蓝牙已打开，此状态下，应用可以使用蓝牙其他功能。

// 定义蓝牙开关状态变化函数回调
function onReceiveEvent(data: access.BluetoothState) {
  let btStateMessage = '';
  switch (data) {
    case access.BluetoothState.STATE\_OFF:
      // 表示蓝牙是关闭的
      btStateMessage += 'STATE\_OFF';
      break;
    case access.BluetoothState.STATE\_TURNING\_ON:
      btStateMessage += 'STATE\_TURNING\_ON';
      break;
    case access.BluetoothState.STATE\_ON:
      // 表示蓝牙是开启的，此时应用才可以使用蓝牙其他功能
      btStateMessage += 'STATE\_ON';
      break;
    case access.BluetoothState.STATE\_TURNING\_OFF:
      btStateMessage += 'STATE\_TURNING\_OFF';
      break;
    case access.BluetoothState.STATE\_BLE\_TURNING\_ON:
      btStateMessage += 'STATE\_BLE\_TURNING\_ON';
      break;
    case access.BluetoothState.STATE\_BLE\_ON:
      btStateMessage += 'STATE\_BLE\_ON';
      break;
    case access.BluetoothState.STATE\_BLE\_TURNING\_OFF:
      btStateMessage += 'STATE\_BLE\_TURNING\_OFF';
      break;
    default:
      btStateMessage += 'unknown state';
      break;
  }
  console.info('bluetooth state: ' + btStateMessage);
}

try {
    // 发起订阅
    access.on('stateChange', onReceiveEvent);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

#### \[h2\]开启蓝牙

若应用获取到的蓝牙开关状态是关闭的，当需要使用蓝牙时，则需要主动开启蓝牙。通过订阅蓝牙开关状态，判断蓝牙是否开启成功。

系统弹出对话框并提示应用“想要开启蓝牙”，如下图1。若用户同意授权，将开启蓝牙。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/c2/v3/x87ORqP0QcqGDvOgwn57Gg/zh-cn_image_0000002625188029.png?HW-CC-KV=V1&HW-CC-Date=20260612T073427Z&HW-CC-Expire=86400&HW-CC-Sign=5CF3FFB706B79137A9A23098B96E52F0BB2CD4896E183A04A32804E51A3FAE66)

**图1** 开启蓝牙对话框

try {
    // 主动获取蓝牙当前的开关状态
    let state = access.getState();
    if (state == access.BluetoothState.STATE\_OFF) {
        // 若蓝牙是关闭的，则主动开启蓝牙
        access.enableBluetooth();
    }
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

#### \[h2\]关闭蓝牙

若应用不需要使用蓝牙时，可根据实际情况判断是否需要主动关闭蓝牙。通过订阅蓝牙开关状态，判断蓝牙是否关闭成功。

系统弹出对话框并提示应用“想要关闭蓝牙”，如下图2。若用户同意授权，将关闭蓝牙。

![](https://contentcenter-vali-drcn.dbankcdn.cn/pvt_2/DeveloperAlliance_scene_100_1/15/v3/fU5W8uwGRUCZexMvOdXv3A/zh-cn_image_0000002625108173.png?HW-CC-KV=V1&HW-CC-Date=20260612T073427Z&HW-CC-Expire=86400&HW-CC-Sign=DFCF6DFB9D3B14FF7880E5194EB7354D6270E029EAC0F145C40B9D5F905D3324)

**图2** 关闭蓝牙对话框

try {
   // 主动获取蓝牙当前的开关状态
   let state = access.getState();
   if (state == access.BluetoothState.STATE\_ON) {
     // 若蓝牙是开启的，则主动关闭蓝牙
     access.disableBluetooth();
  }
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

#### 完整示例

import { access } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';

export class AdapterManager {
  // 定义蓝牙开关状态变化函数回调
  onReceiveEvent = (data: access.BluetoothState) => {
    let btStateMessage = '';
    switch (data) {
      case access.BluetoothState.STATE\_OFF:
        // 表示蓝牙是关闭的
        btStateMessage += 'STATE\_OFF';
        break;
      case access.BluetoothState.STATE\_TURNING\_ON:
        btStateMessage += 'STATE\_TURNING\_ON';
        break;
      case access.BluetoothState.STATE\_ON:
        // 表示蓝牙是开启的，此时应用才可以使用蓝牙其他功能
        btStateMessage += 'STATE\_ON';
        break;
      case access.BluetoothState.STATE\_TURNING\_OFF:
        btStateMessage += 'STATE\_TURNING\_OFF';
        break;
      case access.BluetoothState.STATE\_BLE\_TURNING\_ON:
        btStateMessage += 'STATE\_BLE\_TURNING\_ON';
        break;
      case access.BluetoothState.STATE\_BLE\_ON:
        btStateMessage += 'STATE\_BLE\_ON';
        break;
      case access.BluetoothState.STATE\_BLE\_TURNING\_OFF:
        btStateMessage += 'STATE\_BLE\_TURNING\_OFF';
        break;
      default:
        btStateMessage += 'unknown state';
        break;
    }
    console.info('bluetooth state: ' + btStateMessage);
  };

  // 开启蓝牙
  public openBluetooth() {
    try {
      access.on('stateChange', this.onReceiveEvent);
    } catch (err) {
      console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
    }
    try {
      // 主动获取蓝牙当前的开关状态
      let state = access.getState();
      if (state == access.BluetoothState.STATE\_OFF) {
        // 若蓝牙是关闭的，则主动开启蓝牙
        access.enableBluetooth();
      }
    } catch (err) {
      console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
    }
  }

  // 关闭蓝牙
  public closeBluetooth() {
    try {
      // 主动获取蓝牙当前的开关状态
      let state = access.getState();
      if (state == access.BluetoothState.STATE\_ON) {
        // 若蓝牙是开启的，则主动关闭蓝牙
        access.disableBluetooth();
      }
    } catch (err) {
      console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
    }
  }
}

let adapterManager = new AdapterManager();
export default adapterManager as AdapterManager;