• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.bluetooth.hfp (Bluetooth hfp Module)
2
3The **hfp** module provides APIs for using the Bluetooth Hands-Free Profile (HFP).
4
5> **NOTE**
6>
7> The initial APIs of this module are supported since API version 10. Newly added APIs will be marked with a superscript to indicate their earliest API version.
8
9
10
11## Modules to Import
12
13```js
14import hfp from '@ohos.bluetooth.hfp';
15```
16
17
18## hfp.createHfpAgProfile
19
20createHfpAgProfile(): HandsFreeAudioGatewayProfile
21
22Creates an **HfpAgProfile** instance.
23
24**System capability**: SystemCapability.Communication.Bluetooth.Core
25
26**Return value**
27
28| Type                           | Description        |
29| ----------------------------- | ---------- |
30| HandsFreeAudioGatewayProfile | **HfpAgProfile** instance created.|
31
32**Example**
33
34```js
35import { BusinessError } from '@ohos.base';
36try {
37    let hfpAgProfile = hfp.createHfpAgProfile();
38    console.info('hfpAg success');
39} catch (err) {
40    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
41}
42```
43
44
45## HandsFreeAudioGatewayProfile
46
47Before using any API of **HandsFreeAudioGatewayProfile**, you need to create an instance of this class by using **createHfpAgProfile()**.
48
49
50### connect
51
52connect(deviceId: string): void
53
54Connects to a hands-free device.
55
56**System API**: This is a system API.
57
58**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
59
60**System capability**: SystemCapability.Communication.Bluetooth.Core
61
62**Parameters**
63
64| Name   | Type    | Mandatory  | Description     |
65| ------ | ------ | ---- | ------- |
66| deviceId | string | Yes   | Address of the remote device.|
67
68**Error codes**
69
70For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
71
72| ID| Error Message|
73| -------- | ---------------------------- |
74|2900001 | Service stopped.                         |
75|2900003 | Bluetooth switch is off.                 |
76|2900004 | Profile is not supported.                |
77|2900099 | Operation failed.                        |
78
79**Example**
80
81```js
82import { BusinessError } from '@ohos.base';
83try {
84    let hfpAg = hfp.createHfpAgProfile();
85    hfpAg.connect('XX:XX:XX:XX:XX:XX');
86} catch (err) {
87    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
88}
89```
90
91
92### disconnect
93
94disconnect(deviceId: string): void
95
96Disconnects from a hands-free device.
97
98**System API**: This is a system API.
99
100**Required permissions**: ohos.permission.ACCESS_BLUETOOTH
101
102**System capability**: SystemCapability.Communication.Bluetooth.Core
103
104**Parameters**
105
106| Name   | Type    | Mandatory  | Description     |
107| ------ | ------ | ---- | ------- |
108| deviceId | string | Yes   | Address of the remote device.|
109
110**Error codes**
111
112For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md).
113
114| ID| Error Message|
115| -------- | ---------------------------- |
116|2900001 | Service stopped.                         |
117|2900003 | Bluetooth switch is off.                 |
118|2900004 | Profile is not supported.                |
119|2900099 | Operation failed.                        |
120
121**Example**
122
123```js
124import { BusinessError } from '@ohos.base';
125try {
126    let hfpAg = hfp.createHfpAgProfile();
127    hfpAg.disconnect('XX:XX:XX:XX:XX:XX');
128} catch (err) {
129    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
130}
131```
132