1# @ohos.bluetooth.baseProfile (蓝牙baseProfile模块) 2 3baseProfile模块提供了基础的profile方法。 4 5> **说明:** 6> 7> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 8 9 10 11## 导入模块 12 13```js 14import { baseProfile } from '@kit.ConnectivityKit'; 15``` 16 17 18## StateChangeParam 19 20描述profile状态改变参数。 21 22**系统能力**:SystemCapability.Communication.Bluetooth.Core。 23 24| 名称 | 类型 | 可读 | 可写 | 说明 | 25| -------- | ----------------------------- | ---- | ---- | ------------------------------- | 26| deviceId | string | 是 | 否 | 表示蓝牙设备地址。 | 27| state | [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | 是 | 否 | 表示蓝牙设备的profile连接状态。 | 28| cause<sup>12+</sup>| [DisconnectCause](#disconnectcause12) | 是 | 否 | 表示连接失败的原因。| 29 30 31## DisconnectCause<sup>12+</sup> 32 33枚举,连接失败原因。 34 35**系统能力**:SystemCapability.Communication.Bluetooth.Core。 36 37| 名称 | 值 | 说明 | 38| ------------------ | ---- | ------ | 39| USER_DISCONNECT | 0 | 用户主动断开连接。| 40| CONNECT_FROM_KEYBOARD | 1 | 应该从键盘侧发起连接。| 41| CONNECT_FROM_MOUSE | 2 | 应该从鼠标侧发起连接。| 42| CONNECT_FROM_CAR | 3 | 应该从车机侧发起连接。| 43| TOO_MANY_CONNECTED_DEVICES | 4 | 当前连接数超过上限。| 44| CONNECT_FAIL_INTERNAL | 5 | 内部错误。| 45 46 47## BaseProfile.getConnectedDevices 48 49getConnectedDevices(): Array<string> 50 51获取已连接设备列表。 52 53**需要权限**:ohos.permission.ACCESS_BLUETOOTH 54 55**系统能力**:SystemCapability.Communication.Bluetooth.Core。 56 57**返回值:** 58 59| 类型 | 说明 | 60| ------------------- | ------------------- | 61| Array<string> | 返回当前已连接设备的地址。基于信息安全考虑,此处获取的设备地址为随机MAC地址。配对成功后,该地址不会变更;已配对设备取消配对后重新扫描或蓝牙服务下电时,该随机地址会变更。 | 62 63**错误码**: 64 65以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 66 67| 错误码ID | 错误信息 | 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**示例:** 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 94获取设备profile的连接状态。 95 96**需要权限**:ohos.permission.ACCESS_BLUETOOTH 97 98**系统能力**:SystemCapability.Communication.Bluetooth.Core。 99 100**参数:** 101 102| 参数名 | 类型 | 必填 | 说明 | 103| ------ | ------ | ---- | ------- | 104| deviceId | string | 是 | 远端设备地址。 | 105 106**返回值:** 107 108| 类型 | 说明 | 109| ------------------------------------------------- | ----------------------- | 110| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | 返回profile的连接状态。 | 111 112**错误码**: 113 114以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 115 116| 错误码ID | 错误信息 | 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**示例:** 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 144订阅连接状态变化事件。使用Callback异步回调。 145 146**需要权限**:ohos.permission.ACCESS_BLUETOOTH 147 148**系统能力**:SystemCapability.Communication.Bluetooth.Core。 149 150**参数:** 151 152| 参数名 | 类型 | 必填 | 说明 | 153| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 154| type | string | 是 | 填写"connectionStateChange"字符串,表示连接状态变化事件。 | 155| callback | Callback<[StateChangeParam](#statechangeparam)> | 是 | 表示回调函数的入参。 | 156 157**错误码**: 158 159以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 160 161| 错误码ID | 错误信息 | 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**示例:** 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 188取消订阅连接状态变化事件。 189 190**需要权限**:ohos.permission.ACCESS_BLUETOOTH 191 192**系统能力**:SystemCapability.Communication.Bluetooth.Core。 193 194**参数:** 195 196| 参数名 | 类型 | 必填 | 说明 | 197| -------- | ---------------------------------------- | ---- | ---------------------------------------- | 198| type | string | 是 | 填写"connectionStateChange"字符串,表示连接状态变化事件。 | 199| callback | Callback<[StateChangeParam](#statechangeparam)> | 否 | 表示回调函数的入参。 | 200 201**错误码**: 202 203以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 204 205| 错误码ID | 错误信息 | 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**示例:** 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```