1e41f4b71Sopenharmony_ci# @ohos.bluetooth.baseProfile (Bluetooth baseProfile Module) 2e41f4b71Sopenharmony_ci 3e41f4b71Sopenharmony_ciThe **baseProfile** module provides APIs for using basic Bluetooth profiles. 4e41f4b71Sopenharmony_ci 5e41f4b71Sopenharmony_ci> **NOTE** 6e41f4b71Sopenharmony_ci> 7e41f4b71Sopenharmony_ci> 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. 8e41f4b71Sopenharmony_ci 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci 11e41f4b71Sopenharmony_ci## Modules to Import 12e41f4b71Sopenharmony_ci 13e41f4b71Sopenharmony_ci```js 14e41f4b71Sopenharmony_ciimport { baseProfile } from '@kit.ConnectivityKit'; 15e41f4b71Sopenharmony_ci``` 16e41f4b71Sopenharmony_ci 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci## StateChangeParam 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ciRepresents the profile state change parameters. 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core 23e41f4b71Sopenharmony_ci 24e41f4b71Sopenharmony_ci| Name | Type | Readable| Writable| Description | 25e41f4b71Sopenharmony_ci| -------- | ----------------------------- | ---- | ---- | ------------------------------- | 26e41f4b71Sopenharmony_ci| deviceId | string | Yes | No | Address of the Bluetooth device. | 27e41f4b71Sopenharmony_ci| state | [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | Yes | No | Profile connection state of the device.| 28e41f4b71Sopenharmony_ci| cause<sup>12+</sup>| [DisconnectCause](#disconnectcause12) | Yes| No| Cause of the disconnection.| 29e41f4b71Sopenharmony_ci 30e41f4b71Sopenharmony_ci 31e41f4b71Sopenharmony_ci## DisconnectCause<sup>12+</sup> 32e41f4b71Sopenharmony_ci 33e41f4b71Sopenharmony_ciEnumerates the possible causes of a Bluetooth disconnection. 34e41f4b71Sopenharmony_ci 35e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core 36e41f4b71Sopenharmony_ci 37e41f4b71Sopenharmony_ci| Name | Value | Description | 38e41f4b71Sopenharmony_ci| ------------------ | ---- | ------ | 39e41f4b71Sopenharmony_ci| USER_DISCONNECT | 0 | The user proactively disconnects the connection.| 40e41f4b71Sopenharmony_ci| CONNECT_FROM_KEYBOARD | 1 | The connection should be initiated from a keyboard.| 41e41f4b71Sopenharmony_ci| CONNECT_FROM_MOUSE | 2 | The connection should be initiated from a mouse device.| 42e41f4b71Sopenharmony_ci| CONNECT_FROM_CAR | 3 | The connection should be initiated from a head unit side.| 43e41f4b71Sopenharmony_ci| TOO_MANY_CONNECTED_DEVICES | 4 | The number of connections exceeds the limit.| 44e41f4b71Sopenharmony_ci| CONNECT_FAIL_INTERNAL | 5 | Internal error.| 45e41f4b71Sopenharmony_ci 46e41f4b71Sopenharmony_ci 47e41f4b71Sopenharmony_ci## baseProfile.getConnectedDevices 48e41f4b71Sopenharmony_ci 49e41f4b71Sopenharmony_cigetConnectedDevices(): Array<string> 50e41f4b71Sopenharmony_ci 51e41f4b71Sopenharmony_ciObtains the connected devices. 52e41f4b71Sopenharmony_ci 53e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci**Return value** 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci| Type | Description | 60e41f4b71Sopenharmony_ci| ------------------- | ------------------- | 61e41f4b71Sopenharmony_ci| Array<string> | Addresses of the connected devices. For security purposes, the device addresses obtained are random MAC addresses. The random MAC address remains unchanged after a device is paired successfully. It changes when the paired device is unpaired and scanned again or the Bluetooth service is turned off.| 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci**Error codes** 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ciFor details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci| ID| Error Message| 68e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 69e41f4b71Sopenharmony_ci|201 | Permission denied. | 70e41f4b71Sopenharmony_ci|801 | Capability not supported. | 71e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 72e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 73e41f4b71Sopenharmony_ci|2900004 | Profile not supported. | 74e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 75e41f4b71Sopenharmony_ci 76e41f4b71Sopenharmony_ci**Example** 77e41f4b71Sopenharmony_ci 78e41f4b71Sopenharmony_ci```js 79e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 80e41f4b71Sopenharmony_ciimport { a2dp } from '@kit.ConnectivityKit'; 81e41f4b71Sopenharmony_citry { 82e41f4b71Sopenharmony_ci let a2dpSrc = a2dp.createA2dpSrcProfile(); 83e41f4b71Sopenharmony_ci let retArray = a2dpSrc.getConnectedDevices(); 84e41f4b71Sopenharmony_ci} catch (err) { 85e41f4b71Sopenharmony_ci console.error("errCode:" + (err as BusinessError).code + ",errMessage:" + (err as BusinessError).message); 86e41f4b71Sopenharmony_ci} 87e41f4b71Sopenharmony_ci``` 88e41f4b71Sopenharmony_ci 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci## baseProfile.getConnectionState 91e41f4b71Sopenharmony_ci 92e41f4b71Sopenharmony_cigetConnectionState(deviceId: string): ProfileConnectionState 93e41f4b71Sopenharmony_ci 94e41f4b71Sopenharmony_ciObtains the profile connection state of a device. 95e41f4b71Sopenharmony_ci 96e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 97e41f4b71Sopenharmony_ci 98e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci**Parameters** 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 103e41f4b71Sopenharmony_ci| ------ | ------ | ---- | ------- | 104e41f4b71Sopenharmony_ci| deviceId | string | Yes | Address of the remote device.| 105e41f4b71Sopenharmony_ci 106e41f4b71Sopenharmony_ci**Return value** 107e41f4b71Sopenharmony_ci 108e41f4b71Sopenharmony_ci| Type | Description | 109e41f4b71Sopenharmony_ci| ------------------------------------------------- | ----------------------- | 110e41f4b71Sopenharmony_ci| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | Profile connection state obtained.| 111e41f4b71Sopenharmony_ci 112e41f4b71Sopenharmony_ci**Error codes** 113e41f4b71Sopenharmony_ci 114e41f4b71Sopenharmony_ciFor details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 115e41f4b71Sopenharmony_ci 116e41f4b71Sopenharmony_ci| ID| Error Message| 117e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 118e41f4b71Sopenharmony_ci|201 | Permission denied. | 119e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 120e41f4b71Sopenharmony_ci|801 | Capability not supported. | 121e41f4b71Sopenharmony_ci|2900001 | Service stopped. | 122e41f4b71Sopenharmony_ci|2900003 | Bluetooth disabled. | 123e41f4b71Sopenharmony_ci|2900004 | Profile not supported. | 124e41f4b71Sopenharmony_ci|2900099 | Operation failed. | 125e41f4b71Sopenharmony_ci 126e41f4b71Sopenharmony_ci**Example** 127e41f4b71Sopenharmony_ci 128e41f4b71Sopenharmony_ci```js 129e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 130e41f4b71Sopenharmony_ciimport { a2dp } from '@kit.ConnectivityKit'; 131e41f4b71Sopenharmony_citry { 132e41f4b71Sopenharmony_ci let a2dpSrc = a2dp.createA2dpSrcProfile(); 133e41f4b71Sopenharmony_ci let ret = a2dpSrc.getConnectionState('XX:XX:XX:XX:XX:XX'); 134e41f4b71Sopenharmony_ci} catch (err) { 135e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 136e41f4b71Sopenharmony_ci} 137e41f4b71Sopenharmony_ci``` 138e41f4b71Sopenharmony_ci 139e41f4b71Sopenharmony_ci 140e41f4b71Sopenharmony_ci## baseProfile.on('connectionStateChange') 141e41f4b71Sopenharmony_ci 142e41f4b71Sopenharmony_cion(type: 'connectionStateChange', callback: Callback<StateChangeParam>): void 143e41f4b71Sopenharmony_ci 144e41f4b71Sopenharmony_ciSubscribes to profile connection state changes. This API uses an asynchronous callback to return the result. 145e41f4b71Sopenharmony_ci 146e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 147e41f4b71Sopenharmony_ci 148e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core 149e41f4b71Sopenharmony_ci 150e41f4b71Sopenharmony_ci**Parameters** 151e41f4b71Sopenharmony_ci 152e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 153e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 154e41f4b71Sopenharmony_ci| type | string | Yes | Event type. The value is **connectionStateChange**, which indicates a profile connection state change event.| 155e41f4b71Sopenharmony_ci| callback | Callback<[StateChangeParam](#statechangeparam)> | Yes | Callback used to return the profile connection state change. | 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci**Error codes** 158e41f4b71Sopenharmony_ci 159e41f4b71Sopenharmony_ciFor details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 160e41f4b71Sopenharmony_ci 161e41f4b71Sopenharmony_ci| ID| Error Message| 162e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 163e41f4b71Sopenharmony_ci|201 | Permission denied. | 164e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 165e41f4b71Sopenharmony_ci|801 | Capability not supported. | 166e41f4b71Sopenharmony_ci 167e41f4b71Sopenharmony_ci**Example** 168e41f4b71Sopenharmony_ci 169e41f4b71Sopenharmony_ci```js 170e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 171e41f4b71Sopenharmony_ciimport { a2dp } from '@kit.ConnectivityKit'; 172e41f4b71Sopenharmony_cifunction onReceiveEvent(data: baseProfile.StateChangeParam) { 173e41f4b71Sopenharmony_ci console.info('a2dp state = '+ JSON.stringify(data)); 174e41f4b71Sopenharmony_ci} 175e41f4b71Sopenharmony_citry { 176e41f4b71Sopenharmony_ci let a2dpSrc = a2dp.createA2dpSrcProfile(); 177e41f4b71Sopenharmony_ci a2dpSrc.on('connectionStateChange', onReceiveEvent); 178e41f4b71Sopenharmony_ci} catch (err) { 179e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 180e41f4b71Sopenharmony_ci} 181e41f4b71Sopenharmony_ci``` 182e41f4b71Sopenharmony_ci 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci## baseProfile.off('connectionStateChange') 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_cioff(type: 'connectionStateChange', callback?: Callback<[StateChangeParam](#statechangeparam)>): void 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ciUnsubscribes from profile connection state changes. 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci**Required permissions**: ohos.permission.ACCESS_BLUETOOTH 191e41f4b71Sopenharmony_ci 192e41f4b71Sopenharmony_ci**System capability**: SystemCapability.Communication.Bluetooth.Core 193e41f4b71Sopenharmony_ci 194e41f4b71Sopenharmony_ci**Parameters** 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci| Name | Type | Mandatory | Description | 197e41f4b71Sopenharmony_ci| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 198e41f4b71Sopenharmony_ci| type | string | Yes | Event type. The value is **connectionStateChange**, which indicates a profile connection state change event.| 199e41f4b71Sopenharmony_ci| callback | Callback<[StateChangeParam](#statechangeparam)> | No | Callback to unregister. | 200e41f4b71Sopenharmony_ci 201e41f4b71Sopenharmony_ci**Error codes** 202e41f4b71Sopenharmony_ci 203e41f4b71Sopenharmony_ciFor details about the error codes, see [Bluetooth Error Codes](errorcode-bluetoothManager.md). 204e41f4b71Sopenharmony_ci 205e41f4b71Sopenharmony_ci| ID| Error Message| 206e41f4b71Sopenharmony_ci| -------- | ---------------------------- | 207e41f4b71Sopenharmony_ci|201 | Permission denied. | 208e41f4b71Sopenharmony_ci|401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | 209e41f4b71Sopenharmony_ci|801 | Capability not supported. | 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci**Example** 212e41f4b71Sopenharmony_ci 213e41f4b71Sopenharmony_ci```js 214e41f4b71Sopenharmony_ciimport { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; 215e41f4b71Sopenharmony_ciimport { a2dp } from '@kit.ConnectivityKit'; 216e41f4b71Sopenharmony_cifunction onReceiveEvent(data: baseProfile.StateChangeParam) { 217e41f4b71Sopenharmony_ci console.info('a2dp state = '+ JSON.stringify(data)); 218e41f4b71Sopenharmony_ci} 219e41f4b71Sopenharmony_citry { 220e41f4b71Sopenharmony_ci let a2dpSrc = a2dp.createA2dpSrcProfile(); 221e41f4b71Sopenharmony_ci a2dpSrc.on('connectionStateChange', onReceiveEvent); 222e41f4b71Sopenharmony_ci a2dpSrc.off('connectionStateChange', onReceiveEvent); 223e41f4b71Sopenharmony_ci} catch (err) { 224e41f4b71Sopenharmony_ci console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); 225e41f4b71Sopenharmony_ci} 226e41f4b71Sopenharmony_ci``` 227