1# @ohos.bluetooth.baseProfile (Bluetooth baseProfile Module) 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 9 10 11## Modules to Import 12 13```js 14import baseProfile from '@ohos.bluetooth.baseProfile'; 15import { BusinessError } from '@ohos.base'; 16``` 17 18 19## ConnectionStrategy<a name="ConnectionStrategy"></a> 20 21Enumerates the profile connection strategies. 22 23**System API**: This is a system API. 24 25**System capability**: SystemCapability.Communication.Bluetooth.Core 26 27| Name | Value | Description | 28| -------------------------------- | ------ | --------------- | 29| CONNECTION_STRATEGY_UNSUPPORTED | 0 | Default connection strategy to use when the device is not paired.<br>This is a system API.| 30| CONNECTION_STRATEGY_ALLOWED | 1 | Connection strategy to use when the device is allowed to accept or initiate pairing.<br>This is a system API.| 31| 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. | 32 33 34## StateChangeParam<a name="StateChangeParam"></a> 35 36Represents the profile state change parameters. 37 38**System capability**: SystemCapability.Communication.Bluetooth.Core 39 40| Name | Type | Readable| Writable| Description | 41| -------- | ----------------------------- | ---- | ---- | ------------------------------- | 42| deviceId | string | Yes | No | Address of the Bluetooth device. | 43| state | ProfileConnectionState | Yes | No | Profile connection state of the device.| 44 45 46## baseProfile.setConnectionStrategy<a name="setConnectionStrategy"></a> 47 48setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy, callback: AsyncCallback<void>): void 49 50Sets the profile connection strategy for this device. This API uses an asynchronous callback to return the result. 51 52**System API**: This is a system API. 53 54**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 55 56**System capability**: SystemCapability.Communication.Bluetooth.Core 57 58**Parameters** 59 60| Name | Type | Mandatory | Description | 61| -------- | ------ | ---- | ----------------------------------- | 62| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX. | 63| strategy | [ConnectionStrategy](#connectionstrategy) | Yes |Profile connection strategy to set.| 64| callback | AsyncCallback<void> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 65 66**Error codes** 67 68For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 69 70| ID| Error Message| 71| -------- | ---------------------------- | 72|2900001 | Service stopped. | 73|2900003 | Bluetooth switch is off. | 74|2900004 | Profile is not supported. | 75|2900099 | Operation failed. | 76 77**Example** 78 79```js 80import a2dp from '@ohos.bluetooth.a2dp'; 81try { 82 let a2dpSrc = a2dp.createA2dpSrcProfile(); 83 a2dpSrc.setConnectionStrategy('XX:XX:XX:XX:XX:XX', 0, (err: BusinessError) => { 84 console.info('setConnectionStrategy, err: ' + JSON.stringify(err)); 85 }); 86} catch (err) { 87 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 88} 89``` 90 91## baseProfile.setConnectionStrategy<a name="setConnectionStrategy"></a> 92 93setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy): Promise<void> 94 95Sets the profile connection strategy for this device. This API uses a promise to return the result. 96 97**System API**: This is a system API. 98 99**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_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 device to pair, for example, XX:XX:XX:XX:XX:XX. | 108| strategy | [ConnectionStrategy](#connectionstrategy) | Yes |Profile connection strategy to set.| 109 110**Return value** 111 112| Type | Description | 113| ------------------- | ------------- | 114| Promise<void> | Promise used to return the result.| 115 116**Error codes** 117 118For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 119 120| ID| Error Message| 121| -------- | ---------------------------- | 122|2900001 | Service stopped. | 123|2900003 | Bluetooth switch is off. | 124|2900004 | Profile is not supported. | 125|2900099 | Operation failed. | 126 127**Example** 128 129```js 130import a2dp from '@ohos.bluetooth.a2dp'; 131try { 132 let a2dpSrc = a2dp.createA2dpSrcProfile(); 133 a2dpSrc.setConnectionStrategy('XX:XX:XX:XX:XX:XX', 1).then(() => { 134 console.info('setConnectionStrategy'); 135 }, (err: BusinessError) => { 136 console.error('setConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message); 137 }); 138} catch (err) { 139 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 140} 141``` 142 143## baseProfile.getConnectionStrategy<a name="getConnectionStrategy"></a> 144 145getConnectionStrategy(deviceId: string, callback: AsyncCallback<ConnectionStrategy>): void 146 147Obtains the profile connection strategy. This API uses an asynchronous callback to return the result. 148 149**System API**: This is a system API. 150 151**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 152 153**System capability**: SystemCapability.Communication.Bluetooth.Core 154 155**Parameters** 156 157| Name | Type | Mandatory | Description | 158| -------- | ------ | ---- | ----------------------------------- | 159| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX. | 160| callback | AsyncCallback<[ConnectionStrategy](#connectionstrategy)> | Yes | Callback invoked to return the result. If the operation is successful, **err** is **undefined**. Otherwise, **err** is an error object.| 161 162**Error codes** 163 164For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 165 166| ID| Error Message| 167| -------- | ---------------------------- | 168|2900001 | Service stopped. | 169|2900003 | Bluetooth switch is off. | 170|2900004 | Profile is not supported. | 171|2900099 | Operation failed. | 172 173**Example** 174 175```js 176import a2dp from '@ohos.bluetooth.a2dp'; 177try { 178 let a2dpSrc = a2dp.createA2dpSrcProfile(); 179 a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX', 0, (err: BusinessError, data: baseProfile.ConnectionStrategy) => { 180 console.info('getConnectionStrategy, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data)); 181 }); 182} catch (err) { 183 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 184} 185``` 186 187## baseProfile.getConnectionStrategy<a name="getConnectionStrategy"></a> 188 189getConnectionStrategy(deviceId: string): Promise<ConnectionStrategy> 190 191Obtains the profile connection strategy. This API uses a promise to return the result. 192 193**System API**: This is a system API. 194 195**Required permissions**: ohos.permission.ACCESS_BLUETOOTH and ohos.permission.MANAGE_BLUETOOTH 196 197**System capability**: SystemCapability.Communication.Bluetooth.Core 198 199**Parameters** 200 201| Name | Type | Mandatory | Description | 202| -------- | ------ | ---- | ----------------------------------- | 203| deviceId | string | Yes | Address of the device to pair, for example, XX:XX:XX:XX:XX:XX. | 204 205**Return value** 206 207| Type | Description | 208| ------------------- | ------------- | 209| Promise<[ConnectionStrategy](#connectionstrategy)> | Promise used to return the profile connection strategy obtained.| 210 211**Error codes** 212 213For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 214 215| ID| Error Message| 216| -------- | ---------------------------- | 217|2900001 | Service stopped. | 218|2900003 | Bluetooth switch is off. | 219|2900004 | Profile is not supported. | 220|2900099 | Operation failed. | 221 222**Example** 223 224```js 225import a2dp from '@ohos.bluetooth.a2dp'; 226try { 227 let a2dpSrc = a2dp.createA2dpSrcProfile(); 228 a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX', 1).then((data: baseProfile.ConnectionStrategy) => { 229 console.info('getConnectionStrategy'); 230 }, (err: BusinessError) => { 231 console.error('getConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message); 232 }); 233} catch (err) { 234 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 235} 236``` 237 238 239## baseProfile.getConnectedDevices<a name="getConnectedDevices"></a> 240 241getConnectedDevices(): Array<string> 242 243Obtains the connected devices. 244 245**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 246 247**System capability**: SystemCapability.Communication.Bluetooth.Core 248 249**Return value** 250 251| Type | Description | 252| ------------------- | ------------------- | 253| Array<string> | Addresses of the connected devices.| 254 255**Error codes** 256 257For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 258 259| ID| Error Message| 260| -------- | ---------------------------- | 261|2900001 | Service stopped. | 262|2900003 | Bluetooth switch is off. | 263|2900004 | Profile is not supported. | 264|2900099 | Operation failed. | 265 266**Example** 267 268```js 269import a2dp from '@ohos.bluetooth.a2dp'; 270try { 271 let a2dpSrc = a2dp.createA2dpSrcProfile(); 272 let retArray = a2dpSrc.getConnectedDevices(); 273} catch (err) { 274 console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 275} 276``` 277 278 279## baseProfile.getConnectionState<a name="getConnectionState"></a> 280 281getConnectionState(deviceId: string): ProfileConnectionState 282 283Obtains the profile connection state of a device. 284 285**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 286 287**System capability**: SystemCapability.Communication.Bluetooth.Core 288 289**Parameters** 290 291| Name | Type | Mandatory | Description | 292| ------ | ------ | ---- | ------- | 293| deviceId | string | Yes | Address of the remote device.| 294 295**Return value** 296 297| Type | Description | 298| ------------------------------------------------- | ----------------------- | 299| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | Profile connection state obtained.| 300 301**Error codes** 302 303For details about the error codes, see [Bluetooth Error Codes](../errorcodes/errorcode-bluetoothManager.md). 304 305| ID| Error Message| 306| -------- | ---------------------------- | 307|2900001 | Service stopped. | 308|2900003 | Bluetooth switch is off. | 309|2900004 | Profile is not supported. | 310|2900099 | Operation failed. | 311 312**Example** 313 314```js 315import a2dp from '@ohos.bluetooth.a2dp'; 316try { 317 let a2dpSrc = a2dp.createA2dpSrcProfile(); 318 let ret = a2dpSrc.getConnectionState('XX:XX:XX:XX:XX:XX'); 319} catch (err) { 320 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 321} 322``` 323 324 325## baseProfile.on('connectionStateChange') 326 327on(type: 'connectionStateChange', callback: Callback<StateChangeParam>): void 328 329Subscribes to profile connection state changes. 330 331**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 332 333**System capability**: SystemCapability.Communication.Bluetooth.Core 334 335**Parameters** 336 337| Name | Type | Mandatory | Description | 338| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 339| type | string | Yes | Event type. The value is **connectionStateChange**, which indicates a profile connection state change event.| 340| callback | Callback<[StateChangeParam](#statechangeparam)> | Yes | Callback invoked to return the profile connection state change. | 341 342**Example** 343 344```js 345import a2dp from '@ohos.bluetooth.a2dp'; 346function onReceiveEvent(data: baseProfile.StateChangeParam) { 347 console.info('a2dp state = '+ JSON.stringify(data)); 348} 349try { 350 let a2dpSrc = a2dp.createA2dpSrcProfile(); 351 a2dpSrc.on('connectionStateChange', onReceiveEvent); 352} catch (err) { 353 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 354} 355``` 356 357 358## baseProfile.off('connectionStateChange') 359 360off(type: 'connectionStateChange', callback?: Callback<[StateChangeParam](#StateChangeParam)>): void 361 362Unsubscribes from profile connection state changes. 363 364**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 365 366**System capability**: SystemCapability.Communication.Bluetooth.Core 367 368**Parameters** 369 370| Name | Type | Mandatory | Description | 371| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 372| type | string | Yes | Event type. The value is **connectionStateChange**, which indicates a profile connection state change event.| 373| callback | Callback<[StateChangeParam](#StateChangeParam)> | No | Callback for the profile connection state change. | 374 375**Example** 376 377```js 378import a2dp from '@ohos.bluetooth.a2dp'; 379function onReceiveEvent(data: baseProfile.StateChangeParam) { 380 console.info('a2dp state = '+ JSON.stringify(data)); 381} 382try { 383 let a2dpSrc = a2dp.createA2dpSrcProfile(); 384 a2dpSrc.on('connectionStateChange', onReceiveEvent); 385 a2dpSrc.off('connectionStateChange', onReceiveEvent); 386} catch (err) { 387 console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 388} 389``` 390