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