# @ohos.bluetooth.baseProfile (蓝牙baseProfile模块)
baseProfile模块提供了基础的profile方法。
> **说明:**
>
> 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。
## 导入模块
```js
import baseProfile from '@ohos.bluetooth.baseProfile';
```
## ConnectionStrategy
枚举,表示Profile的连接策略。
**系统接口:** 此接口为系统接口。
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
| 名称 | 值 | 说明 |
| -------------------------------- | ------ | --------------- |
| CONNECTION_STRATEGY_UNSUPPORTED | 0 | 当设备未配对时的默认连接策略。
此接口为系统接口。 |
| CONNECTION_STRATEGY_ALLOWED | 1 | 设备允许接受或发起配对时的连接策略。
此接口为系统接口。 |
| CONNECTION_STRATEGY_FORBIDDEN | 2 | 设备不允许接受或发起配对时的连接策略。
此接口为系统接口。 |
## StateChangeParam
描述profile状态改变参数。
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
| 名称 | 类型 | 可读 | 可写 | 说明 |
| -------- | ----------------------------- | ---- | ---- | ------------------------------- |
| deviceId | string | 是 | 否 | 表示蓝牙设备地址。 |
| state | ProfileConnectionState | 是 | 否 | 表示蓝牙设备的profile连接状态。 |
## baseProfile.setConnectionStrategy
setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy, callback: AsyncCallback<void>): void
设置该设备Profile的连接策略。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------ | ---- | ----------------------------------- |
| deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
| strategy | [ConnectionStrategy](#connectionstrategy) | 是 |Profile的连接策略。 |
| callback | AsyncCallback<void> | 是 | 回调函数。当设置成功,err为undefined,否则为错误对象。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900004 | Profile is not supported. |
|2900099 | Operation failed. |
**示例:**
```js
import a2dp from '@ohos.bluetooth.a2dp';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
let setRet = a2dpSrc.setConnectionStrategy('XX:XX:XX:XX:XX:XX', 0, (err, data) => {
console.info('setConnectionStrategy, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data));
});
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## baseProfile.setConnectionStrategy
setConnectionStrategy(deviceId: string, strategy: ConnectionStrategy): Promise<void>
设置该设备Profile的连接策略。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------ | ---- | ----------------------------------- |
| deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
| strategy | [ConnectionStrategy](#connectionstrategy) | 是 |Profile的连接策略。 |
**返回值:**
| 类型 | 说明 |
| ------------------- | ------------- |
| Promise<void> | 返回promise对象。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900004 | Profile is not supported. |
|2900099 | Operation failed. |
**示例:**
```js
import a2dp from '@ohos.bluetooth.a2dp';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
let setRet = a2dpSrc.setConnectionStrategy('XX:XX:XX:XX:XX:XX', 1).then((data) => {
console.info('setConnectionStrategy');
}, (err) => {
console.error('setConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message);
});
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## baseProfile.getConnectionStrategy
getConnectionStrategy(deviceId: string, callback: AsyncCallback<ConnectionStrategy>): void
获取该Profile的连接策略。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------ | ---- | ----------------------------------- |
| deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
| callback | AsyncCallback<[ConnectionStrategy](#connectionstrategy)> | 是 | 回调函数。当获取策略成功,err为undefined,否则为错误对象。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900004 | Profile is not supported. |
|2900099 | Operation failed. |
**示例:**
```js
import a2dp from '@ohos.bluetooth.a2dp';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
let setRet = a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX', 0, (err, data) => {
console.info('getConnectionStrategy, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data));
});
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## baseProfile.getConnectionStrategy
getConnectionStrategy(deviceId: string): Promise<ConnectionStrategy>
获取该Profile的连接策略。
**系统接口**:此接口为系统接口。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ------ | ---- | ----------------------------------- |
| deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 |
**返回值:**
| 类型 | 说明 |
| ------------------- | ------------- |
| Promise<[ConnectionStrategy](#connectionstrategy)> | 返回promise对象。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900004 | Profile is not supported. |
|2900099 | Operation failed. |
**示例:**
```js
import a2dp from '@ohos.bluetooth.a2dp';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
let setRet = a2dpSrc.getConnectionStrategy('XX:XX:XX:XX:XX:XX', 1).then((data) => {
console.info('getConnectionStrategy');
}, (error) => {
console.error('getConnectionStrategy errCode: ' + err.code + ', errMessage: ' + err.message);
});
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## baseProfile.getConnectedDevices
getConnectedDevices(): Array<string>
获取已连接设备列表。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**返回值:**
| 类型 | 说明 |
| ------------------- | ------------------- |
| Array<string> | 返回当前已连接设备的地址。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900004 | Profile is not supported. |
|2900099 | Operation failed. |
**示例:**
```js
import a2dp from '@ohos.bluetooth.a2dp';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
let retArray = a2dpSrc.getConnectedDevices();
} catch (err) {
console.error("errCode:" + err.code + ",errMessage:" + err.message);
}
```
## baseProfile.getConnectionState
getConnectionState(deviceId: string): ProfileConnectionState
获取设备profile的连接状态。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| ------ | ------ | ---- | ------- |
| deviceId | string | 是 | 远端设备地址。 |
**返回值:**
| 类型 | 说明 |
| ------------------------------------------------- | ----------------------- |
| [ProfileConnectionState](js-apis-bluetooth-constant.md#profileconnectionstate) | 返回profile的连接状态。 |
**错误码**:
以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。
| 错误码ID | 错误信息 |
| -------- | ---------------------------- |
|2900001 | Service stopped. |
|2900003 | Bluetooth switch is off. |
|2900004 | Profile is not supported. |
|2900099 | Operation failed. |
**示例:**
```js
import a2dp from '@ohos.bluetooth.a2dp';
try {
let a2dpSrc = a2dp.createA2dpSrcProfile();
let ret = a2dpSrc.getConnectionState('XX:XX:XX:XX:XX:XX');
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## baseProfile.on('connectionStateChange')
on(type: 'connectionStateChange', callback: Callback<StateChangeParam>): void
订阅连接状态变化事件。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type | string | 是 | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
| callback | Callback<[StateChangeParam](#statechangeparam)> | 是 | 表示回调函数的入参。 |
**示例:**
```js
import a2dp from '@ohos.bluetooth.a2dp';
try {
function onReceiveEvent(data) {
console.info('a2dp state = '+ JSON.stringify(data));
}
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.on('connectionStateChange', onReceiveEvent);
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```
## baseProfile.off('connectionStateChange')
off(type: 'connectionStateChange', callback?: Callback<[StateChangeParam](#StateChangeParam)>): void
取消订阅a2dp连接状态变化事件。
**需要权限**:ohos.permission.ACCESS_BLUETOOTH
**系统能力**:SystemCapability.Communication.Bluetooth.Core。
**参数:**
| 参数名 | 类型 | 必填 | 说明 |
| -------- | ---------------------------------------- | ---- | ---------------------------------------- |
| type | string | 是 | 填写"connectionStateChange"字符串,表示连接状态变化事件。 |
| callback | Callback<[StateChangeParam](#StateChangeParam)> | 否 | 表示回调函数的入参。 |
**示例:**
```js
import a2dp from '@ohos.bluetooth.a2dp';
try {
function onReceiveEvent(data) {
console.info('a2dp state = '+ JSON.stringify(data));
}
let a2dpSrc = a2dp.createA2dpSrcProfile();
a2dpSrc.on('connectionStateChange', onReceiveEvent);
a2dpSrc.off('connectionStateChange', onReceiveEvent);
} catch (err) {
console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
}
```