1# @ohos.bluetooth.access (蓝牙access模块)(系统接口)
2
3access模块提供了打开和关闭蓝牙、获取蓝牙状态的方法。
4
5> **说明:**
6>
7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
8> 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.bluetooth.access (蓝牙access模块)](js-apis-bluetooth-access.md)
9
10
11## 导入模块
12
13```js
14import { access } from '@kit.ConnectivityKit';
15```
16
17
18## access.factoryReset<sup>11+</sup>
19
20factoryReset(callback: AsyncCallback&lt;void&gt;): void
21
22恢复蓝牙出厂设置。使用Callback异步回调。
23
24**系统接口**:此接口为系统接口。
25
26**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
27
28**系统能力**:SystemCapability.Communication.Bluetooth.Core29
30**参数:**
31
32| 参数名   | 类型                                               | 必填  | 说明                                                       |
33| -------- | ------------------------------------------------- | ----- | ---------------------------------------------------------- |
34| callback     | AsyncCallback&lt;void&gt;             | 是    | 回调函数。当恢复蓝牙出厂设置时成功,err为undefined,否则为错误对象。       |
35
36**错误码**:
37
38以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
39
40|错误码ID   | 错误信息           |
41| -------- | ------------------ |
42|201 | Permission denied.                 |
43|202 | Non-system applications are not allowed to use system APIs. |
44|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types.           |
45|801 | Capability not supported.          |
46|2900001   | Service stopped.   |
47|2900099   | Operation failed.  |
48
49**示例:**
50
51```js
52import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
53try {
54    access.factoryReset((err: BusinessError) => {
55        if (err) {
56            console.error("factoryReset error");
57        }
58    });
59} catch (err) {
60    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
61}
62```
63
64
65## access.factoryReset<sup>11+</sup>
66
67factoryReset(): Promise&lt;void&gt;
68
69恢复蓝牙出厂设置。使用Promise异步回调。
70
71**系统接口**:此接口为系统接口。
72
73**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
74
75**系统能力**:SystemCapability.Communication.Bluetooth.Core76
77**返回值:**
78
79| 类型                              | 说明              |
80| --------------------------------- | ---------------- |
81| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
82
83**错误码**:
84
85以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
86
87|错误码ID   | 错误信息           |
88| -------- | ------------------ |
89|201 | Permission denied.                 |
90|202 | Non-system applications are not allowed to use system APIs. |
91|801 | Capability not supported.          |
92|2900001   | Service stopped.   |
93|2900099   | Operation failed.  |
94
95**示例:**
96
97```js
98import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
99try {
100    access.factoryReset().then(() => {
101        console.info("factoryReset");
102    });
103} catch (err) {
104    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
105}
106```
107
108
109## access.getLocalAddress<sup>11+</sup><a name="getLocalAddress"></a>
110
111getLocalAddress(): string;
112
113获取本端设备的蓝牙地址。
114
115**系统接口**:此接口为系统接口。
116
117**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.GET_BLUETOOTH_LOCAL_MAC
118
119**系统能力**:SystemCapability.Communication.Bluetooth.Core120
121**返回值:**
122
123| 类型      | 说明                |
124| --------- | ------------------ |
125| string    | 本端设备的蓝牙地址。 |
126
127**错误码**:
128
129以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
130
131|错误码ID   | 错误信息           |
132| -------- | ------------------ |
133|201 | Permission denied.                 |
134|202 | Non-system applications are not allowed to use system APIs. |
135|801 | Capability not supported.          |
136|2900001   | Service stopped.   |
137|2900099   | Operation failed.  |
138
139**示例:**
140
141```js
142try {
143    let localAddr = access.getLocalAddress();
144} catch (err) {
145    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
146}
147```
148
149
150## access.restrictBluetooth<sup>12+</sup><a name="restrictBluetooth"></a>
151
152restrictBluetooth(): Promise&lt;void&gt;
153
154约束当前蓝牙设备的BR/EDR能力。
155
156**系统接口**:此接口为系统接口。
157
158**需要权限**:ohos.permission.ACCESS_BLUETOOTHohos.permission.MANAGE_BLUETOOTH
159
160**系统能力**:SystemCapability.Communication.Bluetooth.Core161
162**返回值:**
163
164| 类型                              | 说明              |
165| --------------------------------- | ---------------- |
166| Promise&lt;void&gt; | Promise对象。无返回结果的Promise对象。 |
167
168**错误码**:
169
170以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。
171
172|错误码ID   | 错误信息           |
173| -------- | ------------------ |
174|201 | Permission denied.                 |
175|202 | Non-system applications are not allowed to use system APIs. |
176|801 | Capability not supported.          |
177|2900001   | Service stopped.   |
178|2900099   | Operation failed.  |
179
180**示例:**
181
182```js
183import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit';
184try {
185    access.restrictBluetooth().then(() => {
186        console.info("restrictBluetooth");
187    });
188} catch (err) {
189    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
190}
191```
192
193
194