1# @ohos.bluetooth.baseProfile (Bluetooth baseProfile Module) (System API) 2 3The **baseProfile** module provides APIs for using basic Bluetooth profiles. 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> - This topic describes only the system APIs provided by the module. For details about its public APIs, see [@ohos.bluetooth.baseProfile (Bluetooth baseProfile Module)](js-apis-bluetooth-baseProfile.md). 9 10 11## Modules to Import 12 13```js 14import { baseProfile } from '@kit.ConnectivityKit'; 15``` 16 17 18## ConnectionStrategy 19 20Enumerates the profile connection strategies. 21 22**System API**: This is a system API. 23 24**System capability**: SystemCapability.Communication.Bluetooth.Core 25 26| Name | Value | Description | 27| -------------------------------- | ------ | --------------- | 28| CONNECTION_STRATEGY_UNSUPPORTED | 0 | Default connection strategy to use when the device is not paired.<br>This is a system API.| 29| CONNECTION_STRATEGY_ALLOWED | 1 | Connection strategy to use when the device is allowed to accept or initiate pairing.<br>This is a system API.| 30| CONNECTION_STRATEGY_FORBIDDEN | 2 | Connection strategy to use when the device is not allowed to accept or initiate pairing.<br>This is a system API. | 31 32 33 34## baseProfile.setConnectionStrategy 35 36setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy, callback: AsyncCallback<void>): void 37 38Sets the profile connection strategy for this device. This API uses an asynchronous callback to return the result. 39 40**System API**: This is a system API. 41 42**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 43 44**System capability**: SystemCapability.Communication.Bluetooth.Core 45 46**Parameters** 47 48| Name | Type | Mandatory | Description | 49| -------- | ------ | ---- | ----------------------------------- | 50| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX. | 51| strategy | [ConnectionStrategy](#connectionstrategy) | Yes |Profile connection strategy to set.| 52| callback | AsyncCallback<void> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 53 54**Error codes** 55 56For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 57 58| ID| Error Message| 59| -------- | ---------------------------- | 60|201 | Permission denied. | 61|202 | Non-system applications are not allowed to use system APIs. | 62|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 63|801 | Capability not supported. | 64|2900001 | Service stopped. | 65|2900003 | Bluetooth disabled. | 66|2900004 | Profile not supported. | 67|2900099 | Operation failed. | 68 69**Example** 70 71```js 72import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 73import { a2dp } from '@kit.ConnectivityKit'; 74try { 75 let a2dpSrc = a2dp.createA2dpSrcProfile(); 76 a2dpSrc.setConnectionStrategy('XX:XX:XX:XX:XX:XX', 0, (err: BusinessError) => { 77 console.info('setConnectionStrategy, err: ' + JSON.stringify(err)); 78 }); 79} catch (err) { 80 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 81} 82``` 83 84## baseProfile.setConnectionStrategy 85 86setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy): Promise<void> 87 88Sets the profile connection strategy for this device. This API uses a promise to return the result. This API uses a promise to return the result. 89 90**System API**: This is a system API. 91 92**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 93 94**System capability**: SystemCapability.Communication.Bluetooth.Core 95 96**Parameters** 97 98| Name | Type | Mandatory | Description | 99| -------- | ------ | ---- | ----------------------------------- | 100| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX. | 101| strategy | [ConnectionStrategy](#connectionstrategy) | Yes |Profile connection strategy to set.| 102 103**Return value** 104 105| Type | Description | 106| ------------------- | ------------- | 107| Promise<void> | Promise used to return the result.| 108 109**Error codes** 110 111For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 112 113| ID| Error Message| 114| -------- | ---------------------------- | 115|201 | Permission denied. | 116|202 | Non-system applications are not allowed to use system APIs. | 117|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 118|801 | Capability not supported. | 119|2900001 | Service stopped. | 120|2900003 | Bluetooth disabled. | 121|2900004 | Profile not supported. | 122|2900099 | Operation failed. | 123 124**Example** 125 126```js 127import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 128import { a2dp } from '@kit.ConnectivityKit'; 129try { 130 let a2dpSrc = a2dp.createA2dpSrcProfile(); 131 a2dpSrc.setConnectionStrategy('XX:XX:XX:XX:XX:XX', 1).then(() => { 132 console.info('setConnectionStrategy'); 133 }, (err: BusinessError) => { 134 console.error('setConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message); 135 }); 136} catch (err) { 137 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 138} 139``` 140 141## baseProfile.getConnectionStrategy 142 143getConnectionStrategy(deviceId: string, callback: AsyncCallback<ConnectionStrategy>): void 144 145Obtains the profile connection strategy. This API uses an asynchronous callback to return the result. This API uses an asynchronous callback to return the result. 146 147**System API**: This is a system API. 148 149**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 150 151**System capability**: SystemCapability.Communication.Bluetooth.Core 152 153**Parameters** 154 155| Name | Type | Mandatory | Description | 156| -------- | ------ | ---- | ----------------------------------- | 157| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.| 158| callback | AsyncCallback<[ConnectionStrategy](#connectionstrategy)> | Yes | Callback used to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 159 160**Error codes** 161 162For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 163 164| ID| Error Message| 165| -------- | ---------------------------- | 166|201 | Permission denied. | 167|202 | Non-system applications are not allowed to use system APIs. | 168|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 169|801 | Capability not supported. | 170|2900001 | Service stopped. | 171|2900003 | Bluetooth disabled. | 172|2900004 | Profile not supported. | 173|2900099 | Operation failed. | 174 175**Example** 176 177```js 178import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 179import { a2dp } from '@kit.ConnectivityKit'; 180try { 181 let a2dpSrc = a2dp.createA2dpSrcProfile(); 182 a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX', 0, (err: BusinessError, data: baseProfile.ConnectionStrategy) => { 183 console.info('getConnectionStrategy, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data)); 184 }); 185} catch (err) { 186 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 187} 188``` 189 190## baseProfile.getConnectionStrategy 191 192getConnectionStrategy(deviceId: string): Promise<ConnectionStrategy> 193 194Obtains the profile connection strategy. This API uses a promise to return the result. This API uses a promise to return the result. 195 196**System API**: This is a system API. 197 198**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 199 200**System capability**: SystemCapability.Communication.Bluetooth.Core 201 202**Parameters** 203 204| Name | Type | Mandatory | Description | 205| -------- | ------ | ---- | ----------------------------------- | 206| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX.| 207 208**Return value** 209 210| Type | Description | 211| ------------------- | ------------- | 212| Promise<[ConnectionStrategy](js-apis-bluetooth-baseProfile-sys.md#connectionstrategy)> | Promise used to return the profile connection strategy obtained.| 213 214**Error codes** 215 216For details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 217 218| ID| Error Message| 219| -------- | ---------------------------- | 220|2900001 | Service stopped. 221|201 | Permission denied. | 222|202 | Non-system applications are not allowed to use system APIs. | 223|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 224|801 | Capability not supported. | | 225|2900003 | Bluetooth disabled. | 226|2900004 | Profile not supported. | 227|2900099 | Operation failed. | 228 229**Example** 230 231```js 232import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 233import { a2dp } from '@kit.ConnectivityKit'; 234try { 235 let a2dpSrc = a2dp.createA2dpSrcProfile(); 236 a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX', 1).then((data: baseProfile.ConnectionStrategy) => { 237 console.info('getConnectionStrategy'); 238 }, (err: BusinessError) => { 239 console.error('getConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message); 240 }); 241} catch (err) { 242 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 243} 244``` 245