# @ohos.bluetooth.connection (蓝牙connection模块) connection模块提供了对蓝牙操作和管理的方法。 > **说明:** > > 本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。 ## 导入模块 ```js import connection from '@ohos.bluetooth.connection'; ``` ## connection.pairDevice pairDevice(deviceId: string, callback: AsyncCallback<void>): void 发起蓝牙配对。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | | callback | AsyncCallback<void> | 是 | 回调函数。当配对成功,err为undefined,否则为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { // 实际的地址可由扫描流程获取 connection.pairDevice('XX:XX:XX:XX:XX:XX'); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.pairDevice pairDevice(deviceId: string): Promise<void> 发起蓝牙配对。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | **返回值:** | 类型 | 说明 | | ------------------- | ------------- | | Promise<void> | 返回promise对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { // 实际的地址可由扫描流程获取 connection.pairDevice('XX:XX:XX:XX:XX:XX'); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.pairCredibleDevice pairCredibleDevice(deviceId: string, transport: BluetoothTransport, callback: AsyncCallback<void>): void 向可信的远端设备发起蓝牙配对。通过非蓝牙扫描的方式(例如NFC等)获取到外设的地址,可以通过该接口发起配对。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | | transport | [BluetoothTransport](#bluetoothtransport) | 是 | 表示设备类型,例如传统蓝牙设备或低功耗蓝牙设备。 | | callback | AsyncCallback<void> | 是 | 回调函数。当发起配对成功,err为undefined,否则为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { connection.pairCredibleDevice('68:13:24:79:4C:8C', 1, err => { if (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); return; } console.info('pairCredibleDevice, err: ' + JSON.stringify(err)); }); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.pairCredibleDevice pairCredibleDevice(deviceId: string, transport: BluetoothTransport): Promise<void> 向可信的远端设备发起蓝牙配对。通过非蓝牙扫描的方式(例如NFC等)获取到外设的地址,可以通过该接口发起配对。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | | transport | [BluetoothTransport](#bluetoothtransport) | 是 | 表示设备类型,例如传统蓝牙设备或低功耗蓝牙设备。 | **返回值:** | 类型 | 说明 | | ------------------------------------------------- | ------------------- | | Promise<void> | 返回promise对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { connection.pairCredibleDevice('68:13:24:79:4C:8C', 0).then(() => { console.info('PairCredibleDevice'); }, err => { console.error('PairCredibleDevice:errCode' + err.code + ', errMessage: ' + err.message); }); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.cancelPairedDevice cancelPairedDevice(deviceId: string, callback: AsyncCallback<void>): void 删除配对的远程设备。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ------------------------------------- | | deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | | callback | AsyncCallback<void> | 是 | 回调函数。当删除远程配对设备成功,err为undefined,否则为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { connection.cancelPairedDevice('XX:XX:XX:XX:XX:XX'); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.cancelPairedDevice cancelPairedDevice(deviceId: string): Promise<void> 删除配对的远程设备。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ------------------------------------- | | deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | **返回值:** | 类型 | 说明 | | ------------------- | ------------- | | Promise<void> | 返回promise对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { connection.cancelPairedDevice('XX:XX:XX:XX:XX:XX'); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.cancelPairingDevice cancelPairingDevice(deviceId: string, callback: AsyncCallback<void>): void 删除正在配对中的远程设备。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ------------------------------------- | | deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | | callback | AsyncCallback<void> | 是 | 回调函数。当删除远程配对设备成功,err为undefined,否则为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX'); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.cancelPairingDevice cancelPairingDevice(deviceId: string): Promise<void> 删除正在配对中的远程设备。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ------------------------------------- | | deviceId | string | 是 | 表示要删除的远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | **返回值:** | 类型 | 说明 | | ------------------- | ------------- | | Promise<void> | 返回promise对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX'); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.getRemoteDeviceName getRemoteDeviceName(deviceId: string): string 获取对端蓝牙设备的名称。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | --------------------------------- | | deviceId | string | 是 | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | **返回值:** | 类型 | 说明 | | ------ | ------------- | | string | 以字符串格式返回设备名称。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { let remoteDeviceName = connection.getRemoteDeviceName('XX:XX:XX:XX:XX:XX'); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.getRemoteDeviceClass getRemoteDeviceClass(deviceId: string): DeviceClass 获取对端蓝牙设备的类别。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | --------------------------------- | | deviceId | string | 是 | 表示远程设备的地址,例如:"XX:XX:XX:XX:XX:XX"。 | **返回值:** | 类型 | 说明 | | --------------------------- | -------- | | [DeviceClass](#deviceclass) | 远程设备的类别。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { let remoteDeviceClass = connection.getRemoteDeviceClass('XX:XX:XX:XX:XX:XX'); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.getLocalName getLocalName(): string 获取蓝牙本地设备名称。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **返回值:** | 类型 | 说明 | | ------ | --------- | | string | 蓝牙本地设备名称。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900099 | Operation failed. | **示例:** ```js try { let localName = connection.getLocalName(); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.getPairedDevices getPairedDevices(): 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. | |2900099 | Operation failed. | **示例:** ```js try { let devices = connection.getPairedDevices(); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.getProfileConnectionState getProfileConnectionState(profileId?: ProfileId): ProfileConnectionState 依据ProfileId获取指定profile的连接状态。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | --------- | --------- | ---- | ------------------------------------- | | ProfileId | [profileId](js-apis-bluetooth-constant.md#profileid) | 否 | 表示profile的枚举值,例如:PROFILE_A2DP_SOURCE。 | **返回值:** | 类型 | 说明 | | ------------------------------------------------- | ------------------- | | [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 constant from '@ohos.bluetooth.constant'; try { let result = connection.getProfileConnectionState(constant.ProfileId.PROFILE_A2DP_SOURCE); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.setDevicePairingConfirmation setDevicePairingConfirmation(deviceId: string, accept: boolean): void 设置设备配对请求确认。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------ | ------- | ---- | -------------------------------- | | deviceId | string | 是 | 表示远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | | accept | boolean | 是 | 接受配对请求设置为true,否则设置为false。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { // 订阅“pinRequired”配对请求事件,收到远端配对请求后设置配对确认 function onReceivePinRequiredEvent(data) { // data为配对请求的入参,配对请求参数 console.info('pin required = '+ JSON.stringify(data)); connection.setDevicePairingConfirmation(data.deviceId, true); } connection.on('pinRequired', onReceivePinRequiredEvent); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.setDevicePinCode setDevicePinCode(deviceId: string, code: string, callback: AsyncCallback<void>): void 当蓝牙配对类型[PinType](#pintype)为PIN_TYPE_ENTER_PIN_CODE或PIN_TYPE_PIN_16_DIGITS时调用此接口,请求用户输入PIN码。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------ | ------- | ---- | -------------------------------- | | deviceId | string | 是 | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 | | code | string | 是 | 用户输入的PIN码。 | | callback | AsyncCallback<void> | 是 | 回调函数,当设置PinCode成功,err为undefined,否则为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js //callback try { connection.setDevicePinCode('11:22:33:44:55:66', '12345', (err, data) => { console.info('setDevicePinCode,device name err:' + JSON.stringify(err) + ',device name:' + JSON.stringify(data)); }); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.setDevicePinCode setDevicePinCode(deviceId: string, code: string): Promise<void> 当蓝牙配对类型[PinType](#pintype)为PIN_TYPE_ENTER_PIN_CODE或PIN_TYPE_PIN_16_DIGITS时调用此接口,请求用户输入PIN码。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ------ | ------- | ---- | -------------------------------- | | deviceId | string | 是 | 表示远端设备MAC地址,例如:"XX:XX:XX:XX:XX:XX"。 | | code | string | 是 | 用户输入的PIN码。 | **返回值:** | 类型 | 说明 | | ------------------- | ------------- | | Promise<void> | 返回promise对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js //promise try { connection.setDevicePinCode('11:22:33:44:55:66', '12345').then(() => { console.info('setDevicePinCode'); }, error => { console.info('setDevicePinCode: errCode:' + error.code + ',errMessage' + error.message); }) } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.setLocalName setLocalName(name: string): void 设置蓝牙本地设备名称。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | ---- | ------ | ---- | --------------------- | | name | string | 是 | 要设置的蓝牙名称,最大长度为248字节数。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { connection.setLocalName('device_name'); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.setBluetoothScanMode setBluetoothScanMode(mode: ScanMode, duration: number): void 设置蓝牙扫描模式,可以被远端设备发现。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | --------------------- | ---- | ---------------------------- | | mode | [ScanMode](#scanmode) | 是 | 蓝牙扫描模式。 | | duration | number | 是 | 设备可被发现的持续时间,单位为毫秒;设置为0则持续可发现。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { // 设置为可连接可发现才可被远端设备扫描到,可以连接。 connection.setBluetoothScanMode(connection.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.getBluetoothScanMode getBluetoothScanMode(): ScanMode 获取蓝牙扫描模式。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **返回值:** | 类型 | 说明 | | --------------------- | ------- | | [ScanMode](#scanmode) | 蓝牙扫描模式。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { let scanMode = connection.getBluetoothScanMode(); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.startBluetoothDiscovery startBluetoothDiscovery(): void 开启蓝牙扫描,可以发现远端设备。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js let deviceId; function onReceiveEvent(data) { deviceId = data; } try { connection.on('bluetoothDeviceFind', onReceiveEvent); connection.startBluetoothDiscovery(); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.stopBluetoothDiscovery stopBluetoothDiscovery(): void 关闭蓝牙扫描。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { connection.stopBluetoothDiscovery(); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.getLocalProfileUuids getLocalProfileUuids(callback: AsyncCallback<Array<ProfileUuids>>): void 获取本地设备的profile UUID。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | callback | AsyncCallback<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | 是 | 回调函数。当获取UUID成功,err为undefined,否则为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { connection.getLocalProfileUuids('XX:XX:XX:XX:XX:XX', (err, data) => { console.info('getLocalProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data)); }); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.getLocalProfileUuids getLocalProfileUuids(): Promise<Array<ProfileUuids>> 获取本地设备的profile UUID。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **返回值:** | 类型 | 说明 | | ------------------- | ------------- | | Promise<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | 返回promise对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { connection.getLocalProfileUuids('XX:XX:XX:XX:XX:XX').then(() => { console.info('getLocalProfileUuids'); }, err => { console.error('getLocalProfileUuids: errCode' + err.code + ', errMessage: ' + err.message); }); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.getRemoteProfileUuids getRemoteProfileUuids(deviceId: string, callback: AsyncCallback<Array<ProfileUuids>>): void 获取对端蓝牙设备支持的Profile UUID。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | | callback | AsyncCallback<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | 是 | 回调函数。当获取UUID成功,err为undefined,否则为错误对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX', (err, data) => { console.info('getRemoteProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data)); }); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.getRemoteProfileUuids getRemoteProfileUuids(deviceId: string): Promise<Array<ProfileUuids>> 获取对端蓝牙设备支持的Profile UUID。 **系统接口**:此接口为系统接口。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ------ | ---- | ----------------------------------- | | deviceId | string | 是 | 表示配对的远端设备地址,例如:"XX:XX:XX:XX:XX:XX"。 | **返回值:** | 类型 | 说明 | | ------------------- | ------------- | | Promise<Array<[ProfileUuids](js-apis-bluetooth-constant.md#profileuuids)>> | 返回promise对象。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900001 | Service stopped. | |2900003 | Bluetooth switch is off. | |2900099 | Operation failed. | **示例:** ```js try { connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX').then(() => { console.info('getRemoteProfileUuids'); }, err => { console.error('getRemoteProfileUuids: errCode' + err.code + ', errMessage: ' + err.message); }); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.on('bluetoothDeviceFind') on(type: 'bluetoothDeviceFind', callback: Callback<Array<string>>): void 订阅蓝牙设备发现上报事件。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ----------------------------------- | ---- | -------------------------------------- | | type | string | 是 | 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。 | | callback | Callback<Array<string>> | 是 | 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900099 | Operation failed. | **示例:** ```js function onReceiveEvent(data) { // data为蓝牙设备地址集合 console.info('bluetooth device find = '+ JSON.stringify(data)); } try { connection.on('bluetoothDeviceFind', onReceiveEvent); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.off('bluetoothDeviceFind') off(type: 'bluetoothDeviceFind', callback?: Callback<Array<string>>): void 取消订阅蓝牙设备发现上报事件。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ----------------------------------- | ---- | ---------------------------------------- | | type | string | 是 | 填写"bluetoothDeviceFind"字符串,表示蓝牙设备发现事件。 | | callback | Callback<Array<string>> | 否 | 表示取消订阅蓝牙设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900099 | Operation failed. | **示例:** ```js function onReceiveEvent(data) { console.info('bluetooth device find = '+ JSON.stringify(data)); } try { connection.on('bluetoothDeviceFind', onReceiveEvent); connection.off('bluetoothDeviceFind', onReceiveEvent); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.on('bondStateChange') on(type: 'bondStateChange', callback: Callback<BondStateParam>): void 订阅蓝牙配对状态改变事件。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ------------------------------------ | | type | string | 是 | 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。 | | callback | Callback<[BondStateParam](#BondStateParam)> | 是 | 表示回调函数的入参,配对的状态。回调函数由用户创建通过该接口注册。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900099 | Operation failed. | **示例:** ```js function onReceiveEvent(data) { // data为回调函数入参,表示配对的状态 console.info('pair state = '+ JSON.stringify(data)); } try { connection.on('bondStateChange', onReceiveEvent); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.off('bondStateChange') off(type: 'bondStateChange', callback?: Callback<BondStateParam>): void 取消订阅蓝牙配对状态改变事件。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ---------------------------------------- | | type | string | 是 | 填写"bondStateChange"字符串,表示蓝牙配对状态改变事件。 | | callback | Callback<[BondStateParam](#BondStateParam)> | 否 | 表示取消订阅蓝牙配对状态改变事件上报。不填该参数则取消订阅该type对应的所有回调。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900099 | Operation failed. | **示例:** ```js function onReceiveEvent(data) { console.info('bond state = '+ JSON.stringify(data)); } try { connection.on('bondStateChange', onReceiveEvent); connection.off('bondStateChange', onReceiveEvent); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.on('pinRequired') on(type: 'pinRequired', callback: Callback<PinRequiredParam>): void 订阅远端蓝牙设备的配对请求事件。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | -------------------------------- | | type | string | 是 | 填写"pinRequired"字符串,表示配对请求事件。 | | callback | Callback<[PinRequiredParam](#pinrequiredparam)> | 是 | 表示回调函数的入参,配对请求。回调函数由用户创建通过该接口注册。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900099 | Operation failed. | **示例:** ```js function onReceiveEvent(data) { // data为配对请求参数 console.info('pin required = '+ JSON.stringify(data)); } try { connection.on('pinRequired', onReceiveEvent); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## connection.off('pinRequired') off(type: 'pinRequired', callback?: Callback<PinRequiredParam>): void 取消订阅远端蓝牙设备的配对请求事件。 **需要权限**:ohos.permission.ACCESS_BLUETOOTH **系统能力**:SystemCapability.Communication.Bluetooth.Core。 **参数:** | 参数名 | 类型 | 必填 | 说明 | | -------- | ---------------------------------------- | ---- | ---------------------------------------- | | type | string | 是 | 填写"pinRequired"字符串,表示配对请求事件。 | | callback | Callback<[PinRequiredParam](#pinrequiredparam)> | 否 | 表示取消订阅蓝牙配对请求事件上报,入参为配对请求参数。不填该参数则取消订阅该type对应的所有回调。 | **错误码**: 以下错误码的详细介绍请参见[蓝牙服务子系统错误码](../errorcodes/errorcode-bluetoothManager.md)。 | 错误码ID | 错误信息 | | -------- | ---------------------------- | |2900099 | Operation failed. | **示例:** ```js function onReceiveEvent(data) { console.info('pin required = '+ JSON.stringify(data)); } try { connection.on('pinRequired', onReceiveEvent); connection.off('pinRequired', onReceiveEvent); } catch (err) { console.error('errCode: ' + err.code + ', errMessage: ' + err.message); } ``` ## BondStateParam 描述配对状态参数。 **系统能力**:SystemCapability.Communication.Bluetooth.Core。 | 名称 | 类型 | 可读 | 可写 | 说明 | | -------- | ------ | ---- | ---- | ----------- | | deviceId | string | 是 | 否 | 表示要配对的设备ID。 | | state | BondState | 是 | 否 | 表示配对设备的状态。 | ## PinRequiredParam 描述配对请求参数。 **系统能力**:SystemCapability.Communication.Bluetooth.Core。 | 名称 | 类型 | 可读 | 可写 | 说明 | | -------- | ------ | ---- | ---- | ----------- | | deviceId | string | 是 | 否 | 表示要配对的设备ID。 | | pinCode | string | 是 | 否 | 表示要配对的密钥。 | | pinType | [PinType](#pintype) | 是 | 否 | 表示要配对的设备类型。
此接口为系统接口。 | ## DeviceClass 描述蓝牙设备的类别。 **系统能力**:SystemCapability.Communication.Bluetooth.Core。 | 名称 | 类型 | 可读 | 可写 | 说明 | | --------------- | ----------------------------------- | ---- | ---- | ---------------- | | majorClass | [MajorClass](js-apis-bluetooth-constant.md#majorclass) | 是 | 否 | 表示蓝牙设备主要类别的枚举。 | | majorMinorClass | [MajorMinorClass](js-apis-bluetooth-constant.md#majorminorclass) | 是 | 否 | 表示主要次要蓝牙设备类别的枚举。 | | classOfDevice | number | 是 | 否 | 表示设备类别。 | ## BluetoothTransport 枚举,表示设备类型。例如传统蓝牙设备或低功耗蓝牙设备,支持双模默认使用TRANSPORT_BR_EDR。 **系统能力**:SystemCapability.Communication.Bluetooth.Core。 | 名称 | 值 | 说明 | | -------------------------------- | ------ | --------------- | | TRANSPORT_BR_EDR | 0 | 表示传统蓝牙(BR/EDR)设备。 | | TRANSPORT_LE | 1 | 表示低功耗蓝牙(BLE)设备。 | ## ScanMode 枚举,扫描模式。 **系统能力**:SystemCapability.Communication.Bluetooth.Core。 | 名称 | 值 | 说明 | | ---------------------------------------- | ---- | --------------- | | SCAN_MODE_NONE | 0 | 没有扫描模式。 | | SCAN_MODE_CONNECTABLE | 1 | 可连接扫描模式。 | | SCAN_MODE_GENERAL_DISCOVERABLE | 2 | general发现模式。 | | SCAN_MODE_LIMITED_DISCOVERABLE | 3 | limited发现模式。 | | SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE | 4 | 可连接general发现模式。 | | SCAN_MODE_CONNECTABLE_LIMITED_DISCOVERABLE | 5 | 可连接limited发现模式。 | ## BondState 枚举,配对状态。 **系统能力**:SystemCapability.Communication.Bluetooth.Core。 | 名称 | 值 | 说明 | | ------------------ | ---- | ------ | | BOND_STATE_INVALID | 0 | 无效的配对。 | | BOND_STATE_BONDING | 1 | 正在配对。 | | BOND_STATE_BONDED | 2 | 已配对。 | ## 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码。
此接口为系统接口。 |