• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.enterprise.bluetoothManager(蓝牙管理)(系统接口)
2
3本模块提供设备蓝牙管理的能力,包括设置和查询蓝牙信息等。
4
5> **说明:**
6>
7> 本模块首批接口从API version 11开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8>
9> 本模块接口仅可在Stage模型下使用。
10>
11> 本模块接口仅对[设备管理应用](enterpriseDeviceManagement-overview.md#基本概念)开放,需将[设备管理应用激活](js-apis-enterprise-adminManager-sys.md#adminmanagerenableadmin)后调用,实现相应功能。
12>
13> 本模块接口均为系统接口。
14
15## 导入模块
16
17```ts
18import bluetoothManager from '@ohos.enterprise.bluetoothManager';
19```
20
21## bluetoothManager.getBluetoothInfo
22
23getBluetoothInfo(admin: Want): BluetoothInfo;
24
25以同步方法查询设备蓝牙信息。成功返回设备蓝牙信息,失败抛出对应异常。
26
27**需要权限:** ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH
28
29**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
30
31
32
33**参数:**
34
35| 参数名      | 类型                                       | 必填   | 说明                       |
36| -------- | ---------------------------------------- | ---- | ------------------------------- |
37| admin    | [Want](../apis-ability-kit/js-apis-app-ability-want.md)     | 是    | 设备管理应用。                  |
38
39**返回值:**
40
41| 类型             | 说明                                                   |
42| ---------------- | ------------------------------------------------------ |
43| [BluetoothInfo](#bluetoothinfo)| 蓝牙信息,包含蓝牙名称、蓝牙状态和蓝牙连接状态。 |
44
45**错误码**:
46
47以下错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)
48
49| 错误码ID | 错误信息                                                                       |
50| ------- | ---------------------------------------------------------------------------- |
51| 9200001 | the application is not an administrator of the device.                        |
52| 9200002 | the administrator application does not have permission to manage the device. |
53
54**示例:**
55
56```ts
57import Want from '@ohos.app.ability.Want';
58import bluetoothManager from '@ohos.enterprise.bluetoothManager';
59let wantTemp: Want = {
60  bundleName: 'com.example.myapplication',
61  abilityName: 'EntryAbility',
62};
63
64try {
65    let result: bluetoothManager.BluetoothInfo = bluetoothManager.getBluetoothInfo(wantTemp);
66    console.info(`Succeeded in getting bluetooth info. `);
67} catch(err) {
68    console.error(`Failed to get bluetooth info. Code: ${err.code}, message: ${err.message}`);
69}
70```
71
72## BluetoothInfo
73
74设备的蓝牙信息。
75
76**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
77
78
79
80**模型约束:** 此接口仅可在Stage模型下使用
81
82| 名称         | 类型     | 必填 | 说明                            |
83| ----------- | --------| ---- | ------------------------------- |
84| name        | string   | 是   | 表示设备的蓝牙名称。 |
85| state |[access.BluetoothState](../apis-connectivity-kit/js-apis-bluetooth-access.md#bluetoothstate)  | 是   | 表示设备的蓝牙状态。 |
86| connectionState | [constant.ProfileConnectionState](../apis-connectivity-kit/js-apis-bluetooth-constant.md#profileconnectionstate)  | 是   | 表示设备的蓝牙连接状态。 |
87
88## bluetoothManager.isBluetoothDisabled
89
90isBluetoothDisabled(admin: Want): boolean
91
92以同步方法查询蓝牙是否被禁用。
93
94**需要权限:** ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH
95
96**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
97
9899
100**参数:**
101
102| 参数名   | 类型                                  | 必填   | 说明      |
103| ----- | ----------------------------------- | ---- | ------- |
104| admin | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是    | 设备管理应用。 |
105
106**返回值:**
107
108| 类型                   | 说明                      |
109| :-------------------- | ------------------------- |
110| boolean | 返回蓝牙禁用状态,true表示蓝牙被禁用,false表示蓝牙未被禁用。 |
111
112**错误码:**
113
114以下的错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)
115
116| 错误码ID | 错误信息                                                                     |
117| ------- | ---------------------------------------------------------------------------- |
118| 9200001 | the application is not an administrator of the device.                        |
119| 9200002 | the administrator application does not have permission to manage the device. |
120
121**示例:**
122
123```ts
124import Want from '@ohos.app.ability.Want';
125import bluetoothManager from '@ohos.enterprise.bluetoothManager';
126let wantTemp: Want = {
127  bundleName: 'com.example.myapplication',
128  abilityName: 'EntryAbility',
129};
130
131try {
132  let isDisabled: boolean = bluetoothManager.isBluetoothDisabled(wantTemp);
133  console.info(`Succeeded in query the bluetooth is disabled or not, isDisabled : ${isDisabled}`);
134} catch(err) {
135  console.error(`Failed to query the bluetooth is disabled or not. Code: ${err.code}, message: ${err.message}`);
136};
137```
138
139## bluetoothManager.setBluetoothDisabled
140
141setBluetoothDisabled(admin: Want, disabled: boolean): void
142
143以同步方法设置禁用蓝牙策略。
144
145**需要权限:** ohos.permission.ENTERPRISE_MANAGE_BLUETOOTH
146
147**系统能力:** SystemCapability.Customization.EnterpriseDeviceManager
148
149150
151**参数:**
152
153| 参数名     | 类型                                | 必填 | 说明                                      |
154| ---------- | ----------------------------------- | ---- | ----------------------------------------- |
155| admin      | [Want](../apis-ability-kit/js-apis-app-ability-want.md) | 是   | 设备管理应用。                            |
156| disabled   | boolean                             | 是   | true表示禁用蓝牙,false表示解除蓝牙禁用。 |
157
158**错误码:**
159
160以下的错误码的详细介绍请参见[企业设备管理错误码](errorcode-enterpriseDeviceManager.md)
161
162| 错误码ID | 错误信息                                                     |
163| -------- | ------------------------------------------------------------ |
164| 9200001  | the application is not an administrator of the device.       |
165| 9200002  | the administrator application does not have permission to manage the device. |
166
167**示例:**
168
169```ts
170import Want from '@ohos.app.ability.Want';
171import bluetoothManager from '@ohos.enterprise.bluetoothManager';
172let wantTemp: Want = {
173  bundleName: 'com.example.myapplication',
174  abilityName: 'EntryAbility',
175};
176
177try {
178  bluetoothManager.setBluetoothDisabled(wantTemp, true);
179  console.info('Succeeded in set the bluetooth disabled');
180} catch(err) {
181  console.error(`Failed to set the bluetooth disabled. Code: ${err.code}, message: ${err.message}`);
182};
183```
184