# @ohos.bluetooth.connection (蓝牙connection模块)(系统接口) connection模块提供了对蓝牙操作和管理的方法。 > **说明:** > > 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 > 当前页面仅包含本模块的系统接口,其他公开接口参见[@ohos.bluetooth.connection (蓝牙connection模块)](js-apis-bluetooth-connection.md) ## 导入模块 ```js import { connection } from '@kit.ConnectivityKit'; ``` ## connection.pairCredibleDevice pairCredibleDevice(deviceId: string, transport: BluetoothTransport, callback: AsyncCallback<void>): void 向可信的远端设备发起蓝牙配对。通过非蓝牙扫描的方式(例如NFC等)获取到外设的地址,可以通过该接口发起配对。使用Callback异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | | transport | [BluetoothTransport](js-apis-bluetooth-connection.md#bluetoothtransport) | 是 | 表示设备类型,例如传统蓝牙设备或低功耗蓝牙设备。 | | callback | AsyncCallback<void> | 是 | 回调函数。当发起配对成功,err为undefined,否则为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |202 | Non-system applications are not allowed to use system APIs. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |801 | Capability not supported. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; try { connection.pairCredibleDevice('68:13:24:79:4C:8C', connection.BluetoothTransport .TRANSPORT_BR_EDR, (err: BusinessError) => { if (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); return; } console.info('pairCredibleDevice, err: ' + JSON.stringify(err)); }); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.pairCredibleDevice pairCredibleDevice(deviceId: string, transport: BluetoothTransport): Promise<void> 向可信的远端设备发起蓝牙配对。通过非蓝牙扫描的方式(例如NFC等)获取到外设的地址,可以通过该接口发起配对。使用Promise异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | | transport | [BluetoothTransport](js-apis-bluetooth-connection.md#bluetoothtransport) | 是 | 表示设备类型,例如传统蓝牙设备或低功耗蓝牙设备。 | **返回值:** | 类型 | 说明 | | ------------------------------------------------- | ------------------- | | Promise<void> | 返回promise对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |202 | Non-system applications are not allowed to use system APIs. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |801 | Capability not supported. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; try { connection.pairCredibleDevice('68:13:24:79:4C:8C', 0).then(() => { console.info('PairCredibleDevice'); }, (err: BusinessError) => { console.error('PairCredibleDevice:errCode' + err.code + ', errMessage: ' + err.message); }); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.cancelPairedDevice cancelPairedDevice(deviceId: string, callback: AsyncCallback<void>): void 删除配对的远程设备。使用Callback异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ------------------------------------- | | deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | | callback | AsyncCallback<void> | 是 | 回调函数。当删除远程配对设备成功,err为undefined,否则为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |202 | Non-system applications are not allowed to use system APIs. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |801 | Capability not supported. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; //callback try { connection.cancelPairedDevice('11:22:33:44:55:66', (err: BusinessError) => { console.info('cancelPairedDevice, device name err:' + JSON.stringify(err)); }); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.cancelPairedDevice cancelPairedDevice(deviceId: string): Promise<void> 删除配对的远程设备。使用Promise异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ------------------------------------- | | deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | **返回值:** | 类型 | 说明 | | ------------------- | ------------- | | Promise<void> | 返回promise对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |202 | Non-system applications are not allowed to use system APIs. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |801 | Capability not supported. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; //promise try { connection.cancelPairedDevice('11:22:33:44:55:66').then(() => { console.info('cancelPairedDevice'); }, (error: BusinessError) => { console.info('cancelPairedDevice: errCode:' + error.code + ',errMessage' + error.message); }) } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.cancelPairingDevice cancelPairingDevice(deviceId: string, callback: AsyncCallback<void>): void 删除正在配对中的远程设备。使用Callback异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ------------------------------------- | | deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | | callback | AsyncCallback<void> | 是 | 回调函数。当删除远程配对设备成功,err为undefined,否则为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |202 | Non-system applications are not allowed to use system APIs. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |801 | Capability not supported. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; try { connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX'); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.cancelPairingDevice cancelPairingDevice(deviceId: string): Promise<void> 删除正在配对中的远程设备。使用Promise异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ------------------------------------- | | deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | **返回值:** | 类型 | 说明 | | ------------------- | ------------- | | Promise<void> | 返回promise对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |202 | Non-system applications are not allowed to use system APIs. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |801 | Capability not supported. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; try { connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX'); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.getLocalProfileUuids getLocalProfileUuids(callback: AsyncCallback<Array<ProfileUuids>>): void 获取本地设备的profile UUID。使用Callback异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | callback | AsyncCallback<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | 是 | 回调函数。当获取UUID成功,err为undefined,否则为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |202 | Non-system applications are not allowed to use system APIs. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. | |801 | Capability not supported. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; try { connection.getLocalProfileUuids((err: BusinessError, data: Array) => { console.info('getLocalProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data)); }); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.getLocalProfileUuids getLocalProfileUuids(): Promise<Array<ProfileUuids>> 获取本地设备的profile UUID。使用Promise异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **返回值:** | 类型 | 说明 | | ------------------- | ------------- | | Promise<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | 返回promise对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |202 | Non-system applications are not allowed to use system APIs. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |801 | Capability not supported. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; try { connection.getLocalProfileUuids().then(() => { console.info('getLocalProfileUuids'); }, (err: BusinessError) => { console.error('getLocalProfileUuids: errCode' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); }); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.connectAllowedProfiles11+ connectAllowedProfiles(deviceId: string, callback: AsyncCallback<void>): void 连接远端设备所有允许连接的profiles。使用Callback异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | deviceId | string | 是 | 表示连接的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | | callback | AsyncCallback<void> | 是 | 以callback形式异步返回结果。当发起连接成功,err为undefined,否则为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |202 | Non-system applications are not allowed to use system APIs. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |801 | Capability not supported. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; try { connection.connectAllowedProfiles('68:13:24:79:4C:8C', (err: BusinessError) => { if (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); return; } console.info('connectAllowedProfiles, err: ' + JSON.stringify(err)); }); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.connectAllowedProfiles11+ connectAllowedProfiles(deviceId: string): Promise<void> 连接远端设备所有允许连接的profiles。使用Promise异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | deviceId | string | 是 | 表示连接的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | **返回值:** | 类型 | 说明 | | ------------------------------------------------- | ------------------- | | Promise<void> | 以Promise形式返回连接profiles的结果,返回true为成功,false为失败。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |202 | Non-system applications are not allowed to use system APIs. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |801 | Capability not supported. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; try { connection.connectAllowedProfiles('68:13:24:79:4C:8C').then(() => { console.info('connectAllowedProfiles'); }, (err: BusinessError) => { console.error('connectAllowedProfiles:errCode' + err.code + ', errMessage: ' + err.message); }); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.disconnectAllowedProfiles11+ disconnectAllowedProfiles(deviceId: string, callback: AsyncCallback<void>): void 断开远端设备所有连接的profiles。使用Callback异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | deviceId | string | 是 | 表示断开的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | | callback | AsyncCallback<void> | 是 | 以callback形式异步返回结果。当发起断开成功,err为undefined,否则为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |202 | Non-system applications are not allowed to use system APIs. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |801 | Capability not supported. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; try { connection.disconnectAllowedProfiles('68:13:24:79:4C:8C', (err: BusinessError) => { if (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); return; } console.info('disconnectAllowedProfiles, err: ' + JSON.stringify(err)); }); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.disconnectAllowedProfiles11+ disconnectAllowedProfiles(deviceId: string): Promise<void> 断开远端设备所有连接的profiles。使用Promise异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | deviceId | string | 是 | 表示断开的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | **返回值:** | 类型 | 说明 | | ------------------------------------------------- | ------------------- | | Promise<void> | 以Promise形式返回断开profiles的结果,返回true为成功,false为失败。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |202 | Non-system applications are not allowed to use system APIs. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |801 | Capability not supported. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; try { connection.disconnectAllowedProfiles('68:13:24:79:4C:8C').then(() => { console.info('disconnectAllowedProfiles'); }, (err: BusinessError) => { console.error('disconnectAllowedProfiles:errCode' + err.code + ', errMessage: ' + err.message); }); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.getRemoteProductId11+ getRemoteProductId(deviceId: string): string 获取对端蓝牙设备的Product ID。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | --------------------------------- | | deviceId | string | 是 | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | **返回值:** | 类型 | 说明 | | ------ | ------------- | | string | 以字符串格式返回设备Product ID。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |202 | Non-system applications are not allowed to use system APIs. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |801 | Capability not supported. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | |2900099 | Operation failed. | **示例:** ```js try { let remoteDeviceProductId = connection.getRemoteProductId('XX:XX:XX:XX:XX:XX'); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.on('discoveryResult')12+ on(type: 'discoveryResult', callback: Callback<Array<DiscoveryResult>>): void 订阅蓝牙设备发现上报事件。使用Callback异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.GET_BLUETOOTH_PEERS_MAC **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ----------------------------------- | ---- | -------------------------------------- | | type | string | 是 | 填写"discoveryResult"字符串,表示蓝牙设备发现事件。 | | callback | Callback<Array<[DiscoveryResult](#discoveryresult12)>> | 是 | 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |801 | Capability not supported. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; let onReceiveEvent: (data: Array) => void = (data: Array) => { // data为蓝牙设备扫描结果集合 console.info('bluetooth device find = '+ JSON.stringify(data)); } try { connection.on('discoveryResult', onReceiveEvent); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.off('discoveryResult')12+ off(type: 'discoveryResult', callback?: Callback<Array<DiscoveryResult>>): void 取消订阅蓝牙设备发现上报事件。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.GET_BLUETOOTH_PEERS_MAC **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ----------------------------------- | ---- | ---------------------------------------- | | type | string | 是 | 填写"discoveryResult"字符串,表示蓝牙设备发现事件。 | | callback | Callback<Array<[DiscoveryResult](#discoveryresult12)>> | 否 | 表示取消订阅蓝牙设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |801 | Capability not supported. | |2900099 | Operation failed. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; let onReceiveEvent: (data: Array) => void = (data: Array) => { // data为蓝牙设备扫描结果集合 console.info('bluetooth device find = '+ JSON.stringify(data)); } try { connection.on('discoveryResult', onReceiveEvent); connection.off('discoveryResult', onReceiveEvent); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.setRemoteDeviceType12+ setRemoteDeviceType(deviceId: string, type: DeviceType): Promise<void> 设置蓝牙远端设备自定义类型。使用Promise异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------ | ------- | ---- | -------------------------------- | | deviceId | string | 是 | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 | | type | [DeviceType](#devicetype12) | 是 | 表示设备类型。 | **返回值:** | 类型 | 说明 | | ------------------- | ------------- | | Promise<void> | 以Promise形式返回设置蓝牙远端设备类型的结果,设置失败时返回错误码信息。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; //promise try { connection.setRemoteDeviceType('11:22:33:44:55:66', connection.DeviceType.DEVICE_TYPE_HEADSET).then(() => { console.info('setRemoteDeviceType success'); }); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## connection.getRemoteDeviceType12+ getRemoteDeviceType(deviceId: string): Promise<DeviceType> 获取蓝牙远端设备自定义类型。使用Promise异步回调。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------ | ------- | ---- | -------------------------------- | | deviceId | string | 是 | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 | **返回值:** | 类型 | 说明 | | ------------------- | ------------- | | Promise<[DeviceType](#devicetype12)> | 以Promise形式返回设置蓝牙远端设备类型的结果,返回值为设备类型。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |201 | Permission denied. | |401 | Invalid parameter. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. 3. Parameter verification failed. | |2900001 | Service stopped. | |2900003 | Bluetooth disabled. | **示例:** ```js import { AsyncCallback, BusinessError } from '@kit.BasicServicesKit'; //promise try { connection.getRemoteDeviceType('11:22:33:44:55:66').then((data: connection.DeviceType) => { console.info('getRemoteDeviceType success, DeviceType:' + JSON.stringify(data)); }); } catch (err) { console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message); } ``` ## PinRequiredParam 描述配对请求参数。 **系统能力**:SystemCapability.Communication.Bluetooth.Core。 | 名称 | 类型 | 可读 | 可写 | 说明 | | -------- | ------ | ---- | ---- | ----------- | | pinType | [PinType](#pintype) | 是 | 否 | 表示要配对的设备类型。
此接口为系统接口。 | ## PinType 枚举,蓝牙配对类型。 **系统接口:** 此接口为系统接口。 **系统能力**:SystemCapability.Communication.Bluetooth.Core。 | 名称 | 值 | 说明 | | -------------------------------- | ------ | --------------- | | PIN_TYPE_ENTER_PIN_CODE | 0 | 用户需要输入对端设备上显示的PIN码。
此接口为系统接口。 | | PIN_TYPE_ENTER_PASSKEY | 1 | 用户需要输入对端设备上显示的PASSKEY。
此接口为系统接口。 | | PIN_TYPE_CONFIRM_PASSKEY | 2 | 用户需要确认本地设备上显示的PASSKEY。
此接口为系统接口。 | | PIN_TYPE_NO_PASSKEY_CONSENT | 3 | 无PASSKEY,用户需要接受或拒绝配对请求。
此接口为系统接口。 | | PIN_TYPE_NOTIFY_PASSKEY | 4 | 本地设备显示PASSKEY,用户需要在对端设备上输入该PASSKEY。
此接口为系统接口。 | | PIN_TYPE_DISPLAY_PIN_CODE | 5 | bluetooth 2.0设备,用户需要输入对端设备上显示的PIN码。
此接口为系统接口。 | | PIN_TYPE_OOB_CONSENT | 6 | 用户需要接受或拒绝OOB配对请求。
此接口为系统接口。 | | PIN_TYPE_PIN_16_DIGITS | 7 | 用户需要输入对端设备上显示的16位PIN码。
此接口为系统接口。 | ## DiscoveryResult12+ 描述扫描设备状态参数。 **系统接口:** 此接口为系统接口。 **系统能力**:SystemCapability.Communication.Bluetooth.Core。 | 名称 | 类型 | 可读 | 可写 | 说明 | | -------- | ------ | ---- | ---- | ----------- | | deviceId12+ | string | 是 | 否 | 表示扫描到的设备ID。
此接口为系统接口。 | | rssi12+ | number | 是 | 否 | 表示扫描到的设备的信号强度。
此接口为系统接口。 | | deviceName12+ | string | 是 | 否 | 表示扫描到的设备的设备名称。
此接口为系统接口。 | | deviceClass12+ | DeviceClass | 是 | 否 | 表示扫描到的设备的设备类别。
此接口为系统接口。 | ## DeviceType12+ 枚举,蓝牙远程设备的自定义类型。 **系统接口:** 此接口为系统接口。 **系统能力**:SystemCapability.Communication.Bluetooth.Core。 | 名称 | 值 | 说明 | | -------------------------------- | ------ | --------------- | | DEVICE_TYPE_DEFAULT12+ | 0 | 默认设备类型,与原类型一致。
此接口为系统接口。 | | DEVICE_TYPE_CAR12+ | 1 | 汽车。
此接口为系统接口。 | | DEVICE_TYPE_HEADSET12+ | 2 | 耳机。
此接口为系统接口。 | | DEVICE_TYPE_HEARING12+ | 3 | 助听器
此接口为系统接口。 | | DEVICE_TYPE_GLASSES12+ | 4 | 眼镜。
此接口为系统接口。 | | DEVICE_TYPE_WATCH12+ | 5 | 手表。
此接口为系统接口。 | | DEVICE_TYPE_SPEAKER12+ | 6 | 音响。
此接口为系统接口。 | | DEVICE_TYPE_OTHERS12+ | 7 | 其他设备。
此接口为系统接口。 | ## BatteryInfo12+ 描述电量信息的内容。 **系统能力**:SystemCapability.Communication.Bluetooth.Core。 | 名称 | 类型 | 可读 | 可写 | 说明 | | -------- | ------ | ---- | ---- | ----------- | | deviceId | string | 是 | 否 | 表示远端设备的MAC地址。
此接口为系统接口。 |