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'; 15import { BusinessError } from '@ohos.base'; 16``` 17 18 19## hfp.createHfpAgProfile<a name="createHfpAgProfile"></a> 20 21createHfpAgProfile(): HandsFreeAudioGatewayProfile 22 23Creates an **HfpAgProfile** instance. 24 25**System capability**: SystemCapability.Communication.Bluetooth.Core 26 27**Return value** 28 29| Type | Description | 30| ----------------------------- | ---------- | 31| HandsFreeAudioGatewayProfile | **HfpAgProfile** instance created.| 32 33**Example** 34 35```js 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<a name="hfp-connect"></a> 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 82try { 83 let hfpAg = hfp.createHfpAgProfile(); 84 hfpAg.connect('XX:XX:XX:XX:XX:XX'); 85} catch (err) { 86 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 87} 88``` 89 90 91### disconnect<a name="hfp-disconnect"></a> 92 93disconnect(deviceId: string): void 94 95Disconnects from a hands-free device. 96 97**System API**: This is a system API. 98 99**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 100 101**System capability**: SystemCapability.Communication.Bluetooth.Core 102 103**Parameters** 104 105| Name | Type | Mandatory | Description | 106| ------ | ------ | ---- | ------- | 107| deviceId | string | Yes | Address of the remote device.| 108 109**Error codes** 110 111For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 112 113| ID| Error Message| 114| -------- | ---------------------------- | 115|2900001 | Service stopped. | 116|2900003 | Bluetooth switch is off. | 117|2900004 | Profile is not supported. | 118|2900099 | Operation failed. | 119 120**Example** 121 122```js 123try { 124 let hfpAg = hfp.createHfpAgProfile(); 125 hfpAg.disconnect('XX:XX:XX:XX:XX:XX'); 126} catch (err) { 127 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 128} 129``` 130