harmony 鸿蒙@ohos.bluetooth.connection (蓝牙connection模块)

  • 2023-10-30
  • 浏览 (425)

@ohos.bluetooth.connection (蓝牙connection模块)

connection模块提供了对蓝牙操作和管理的方法。

说明:

本模块首批接口从API version 10开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

导入模块

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,否则为错误对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    // 实际的地址可由扫描流程获取
    connection.pairDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).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对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    // 实际的地址可由扫描流程获取
    connection.pairDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).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 表示设备类型,例如传统蓝牙设备或低功耗蓝牙设备。
callback AsyncCallback<void> 回调函数。当发起配对成功,err为undefined,否则为错误对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
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等)获取到外设的地址,可以通过该接口发起配对。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH 和 ohos.permission.MANAGE_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
deviceId string 表示配对的远端设备地址,例如:”XX:XX:XX:XX:XX:XX”。
transport BluetoothTransport 表示设备类型,例如传统蓝牙设备或低功耗蓝牙设备。

返回值:

类型 说明
Promise<void> 返回promise对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
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

删除配对的远程设备。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
deviceId string 表示要删除的远程设备的地址,例如:”XX:XX:XX:XX:XX:XX”。
callback AsyncCallback<void> 回调函数。当删除远程配对设备成功,err为undefined,否则为错误对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    connection.cancelPairedDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).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对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    connection.cancelPairedDevice('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, 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,否则为错误对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
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>

删除正在配对中的远程设备。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
deviceId string 表示要删除的远程设备的地址,例如:”XX:XX:XX:XX:XX:XX”。

返回值:

类型 说明
Promise<void> 返回promise对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    connection.cancelPairingDevice('XX:XX:XX:XX:XX:XX');
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.getRemoteDeviceName

getRemoteDeviceName(deviceId: string): string

获取对端蓝牙设备的名称。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
deviceId string 表示远程设备的地址,例如:”XX:XX:XX:XX:XX:XX”。

返回值:

类型 说明
string 以字符串格式返回设备名称。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    let remoteDeviceName: string = connection.getRemoteDeviceName('XX:XX:XX:XX:XX:XX');
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.getRemoteDeviceClass

getRemoteDeviceClass(deviceId: string): DeviceClass

获取对端蓝牙设备的类别。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
deviceId string 表示远程设备的地址,例如:”XX:XX:XX:XX:XX:XX”。

返回值:

类型 说明
DeviceClass 远程设备的类别。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    let remoteDeviceClass: connection.DeviceClass = connection.getRemoteDeviceClass('XX:XX:XX:XX:XX:XX');
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.getLocalName

getLocalName(): string

获取蓝牙本地设备名称。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

类型 说明
string 蓝牙本地设备名称。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    let localName: string = connection.getLocalName();
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.getPairedDevices

getPairedDevices(): Array<string>

获取蓝牙配对列表。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

类型 说明
Array<string> 已配对蓝牙设备的地址列表。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    let devices: Array<string> = connection.getPairedDevices();
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.getProfileConnectionState

getProfileConnectionState(profileId?: ProfileId): ProfileConnectionState

获取蓝牙Profile的连接状态,其中ProfileId为可选参数。如果携带ProfileId,则返回的是当前Profile的连接状态。如果未携带ProfileId,任一Profile已连接则返回STATE_CONNECTED,否则返回STATE_DISCONNECTED

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
ProfileId profileId 表示profile的枚举值,例如:PROFILE_A2DP_SOURCE。

返回值:

类型 说明
ProfileConnectionState profile的连接状态。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900004 Profile is not supported.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
import constant from '@ohos.bluetooth.constant';
try {
    let result: connection.ProfileConnectionState = connection.getProfileConnectionState(constant.ProfileId.PROFILE_A2DP_SOURCE);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).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。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
// 订阅“pinRequired”配对请求事件,收到远端配对请求后设置配对确认
function onReceivePinRequiredEvent(data: connection.PinRequiredParam) { // data为配对请求的入参,配对请求参数
    console.info('pin required  = '+ JSON.stringify(data));
    connection.setDevicePairingConfirmation(data.deviceId, true);
}
try {
    connection.on('pinRequired', onReceivePinRequiredEvent);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.setDevicePinCode

setDevicePinCode(deviceId: string, code: string, callback: AsyncCallback&lt;void&gt;): void

当蓝牙配对类型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&lt;void&gt; 回调函数,当设置PinCode成功,err为undefined,否则为错误对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
//callback
try {
    connection.setDevicePinCode('11:22:33:44:55:66', '12345', (err: BusinessError) => {
        console.info('setDevicePinCode,device name err:' + JSON.stringify(err));
    });
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.setDevicePinCode

setDevicePinCode(deviceId: string, code: string): Promise&lt;void&gt;

当蓝牙配对类型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&lt;void&gt; 返回promise对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
//promise
try {
    connection.setDevicePinCode('11:22:33:44:55:66', '12345').then(() => {
        console.info('setDevicePinCode');
    }, (error: BusinessError) => {
        console.info('setDevicePinCode: errCode:' + error.code + ',errMessage' + error.message);
    })

} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.setLocalName

setLocalName(name: string): void

设置蓝牙本地设备名称。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
name string 要设置的蓝牙名称,最大长度为248字节数。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    connection.setLocalName('device_name');
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.setBluetoothScanMode

setBluetoothScanMode(mode: ScanMode, duration: number): void

设置蓝牙扫描模式,可以被远端设备发现。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
mode ScanMode 蓝牙扫描模式。
duration number 设备可被发现的持续时间,单位为毫秒;设置为0则持续可发现。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    // 设置为可连接可发现才可被远端设备扫描到,可以连接。
    connection.setBluetoothScanMode(connection.ScanMode.SCAN_MODE_CONNECTABLE_GENERAL_DISCOVERABLE, 100);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.getBluetoothScanMode

getBluetoothScanMode(): ScanMode

获取蓝牙扫描模式。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

类型 说明
ScanMode 蓝牙扫描模式。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    let scanMode: connection.ScanMode = connection.getBluetoothScanMode();
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.startBluetoothDiscovery

startBluetoothDiscovery(): void

开启蓝牙扫描,可以发现远端设备。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
function onReceiveEvent(data: Array<string>) {
    console.log('data length' + data.length);
}
try {
    connection.on('bluetoothDeviceFind', onReceiveEvent);
    connection.startBluetoothDiscovery();
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.stopBluetoothDiscovery

stopBluetoothDiscovery(): void

关闭蓝牙扫描。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    connection.stopBluetoothDiscovery();
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.getLocalProfileUuids

getLocalProfileUuids(callback: AsyncCallback&lt;Array&lt;ProfileUuids&gt;&gt;): void

获取本地设备的profile UUID。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
callback AsyncCallback&lt;Array&lt;ProfileUuids&gt;&gt; 回调函数。当获取UUID成功,err为undefined,否则为错误对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    connection.getLocalProfileUuids('XX:XX:XX:XX:XX:XX', (err: BusinessError, data: Array<connection.ProfileUuids>) => {
        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&lt;Array&lt;ProfileUuids&gt;&gt;

获取本地设备的profile UUID。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

返回值:

类型 说明
Promise&lt;Array&lt;ProfileUuids&gt;&gt; 返回promise对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    connection.getLocalProfileUuids('XX:XX:XX:XX:XX:XX').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.getRemoteProfileUuids

getRemoteProfileUuids(deviceId: string, callback: AsyncCallback&lt;Array&lt;ProfileUuids&gt;&gt;): void

获取对端蓝牙设备支持的Profile UUID。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
deviceId string 表示配对的远端设备地址,例如:”XX:XX:XX:XX:XX:XX”。
callback AsyncCallback&lt;Array&lt;ProfileUuids&gt;&gt; 回调函数。当获取UUID成功,err为undefined,否则为错误对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX', (err: BusinessError, data: Array<connection.ProfileUuids>) => {
        console.info('getRemoteProfileUuids, err: ' + JSON.stringify(err) + ', data: ' + JSON.stringify(data));
    });
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.getRemoteProfileUuids

getRemoteProfileUuids(deviceId: string): Promise&lt;Array&lt;ProfileUuids&gt;&gt;

获取对端蓝牙设备支持的Profile UUID。

系统接口:此接口为系统接口。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
deviceId string 表示配对的远端设备地址,例如:”XX:XX:XX:XX:XX:XX”。

返回值:

类型 说明
Promise&lt;Array&lt;ProfileUuids&gt;&gt; 返回promise对象。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900001 Service stopped.
2900003 Bluetooth switch is off.
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
try {
    connection.getRemoteProfileUuids('XX:XX:XX:XX:XX:XX').then(() => {
        console.info('getRemoteProfileUuids');
    }, (err: BusinessError) => {
        console.error('getRemoteProfileUuids: 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.on(‘bluetoothDeviceFind’)

on(type: ‘bluetoothDeviceFind’, callback: Callback&lt;Array&lt;string&gt;&gt;): void

订阅蓝牙设备发现上报事件。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
type string 填写”bluetoothDeviceFind”字符串,表示蓝牙设备发现事件。
callback Callback&lt;Array&lt;string&gt;&gt; 表示回调函数的入参,发现的设备集合。回调函数由用户创建通过该接口注册。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
function onReceiveEvent(data: Array<string>) { // data为蓝牙设备地址集合
    console.info('bluetooth device find = '+ JSON.stringify(data));
}
try {
    connection.on('bluetoothDeviceFind', onReceiveEvent);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.off(‘bluetoothDeviceFind’)

off(type: ‘bluetoothDeviceFind’, callback?: Callback&lt;Array&lt;string&gt;&gt;): void

取消订阅蓝牙设备发现上报事件。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
type string 填写”bluetoothDeviceFind”字符串,表示蓝牙设备发现事件。
callback Callback&lt;Array&lt;string&gt;&gt; 表示取消订阅蓝牙设备发现事件上报。不填该参数则取消订阅该type对应的所有回调。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
function onReceiveEvent(data: Array<string>) {
    console.info('bluetooth device find = '+ JSON.stringify(data));
}
try {
    connection.on('bluetoothDeviceFind', onReceiveEvent);
    connection.off('bluetoothDeviceFind', onReceiveEvent);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.on(‘bondStateChange’)

on(type: ‘bondStateChange’, callback: Callback&lt;BondStateParam&gt;): void

订阅蓝牙配对状态改变事件。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
type string 填写”bondStateChange”字符串,表示蓝牙配对状态改变事件。
callback Callback&lt;BondStateParam&gt; 表示回调函数的入参,配对的状态。回调函数由用户创建通过该接口注册。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
function onReceiveEvent(data: connection.BondStateParam) { // data为回调函数入参,表示配对的状态
    console.info('pair state = '+ JSON.stringify(data));
}
try {
    connection.on('bondStateChange', onReceiveEvent);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.off(‘bondStateChange’)

off(type: ‘bondStateChange’, callback?: Callback&lt;BondStateParam&gt;): void

取消订阅蓝牙配对状态改变事件。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
type string 填写”bondStateChange”字符串,表示蓝牙配对状态改变事件。
callback Callback&lt;BondStateParam&gt; 表示取消订阅蓝牙配对状态改变事件上报。不填该参数则取消订阅该type对应的所有回调。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
function onReceiveEvent(data: connection.BondStateParam) {
    console.info('bond state = '+ JSON.stringify(data));
}
try {
    connection.on('bondStateChange', onReceiveEvent);
    connection.off('bondStateChange', onReceiveEvent);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.on(‘pinRequired’)

on(type: ‘pinRequired’, callback: Callback&lt;PinRequiredParam&gt;): void

订阅远端蓝牙设备的配对请求事件。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
type string 填写”pinRequired”字符串,表示配对请求事件。
callback Callback&lt;PinRequiredParam&gt; 表示回调函数的入参,配对请求。回调函数由用户创建通过该接口注册。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
function onReceiveEvent(data: connection.PinRequiredParam) { // data为配对请求参数
    console.info('pin required = '+ JSON.stringify(data));
}
try {
    connection.on('pinRequired', onReceiveEvent);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

connection.off(‘pinRequired’)

off(type: ‘pinRequired’, callback?: Callback&lt;PinRequiredParam&gt;): void

取消订阅远端蓝牙设备的配对请求事件。

需要权限:ohos.permission.ACCESS_BLUETOOTH

系统能力:SystemCapability.Communication.Bluetooth.Core。

参数:

参数名 类型 必填 说明
type string 填写”pinRequired”字符串,表示配对请求事件。
callback Callback&lt;PinRequiredParam&gt; 表示取消订阅蓝牙配对请求事件上报,入参为配对请求参数。不填该参数则取消订阅该type对应的所有回调。

错误码

以下错误码的详细介绍请参见蓝牙服务子系统错误码

错误码ID 错误信息
2900099 Operation failed.

示例:

import { BusinessError } from '@ohos.base';
function onReceiveEvent(data: connection.PinRequiredParam) {
    console.info('pin required = '+ JSON.stringify(data));
}
try {
    connection.on('pinRequired', onReceiveEvent);
    connection.off('pinRequired', onReceiveEvent);
} catch (err) {
    console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

BondStateParam

描述配对状态参数。

系统能力:SystemCapability.Communication.Bluetooth.Core。

名称 类型 可读 可写 说明
deviceId string 表示要配对的设备ID。
state BondState 表示配对设备的状态。

PinRequiredParam

描述配对请求参数。

系统能力:SystemCapability.Communication.Bluetooth.Core。

名称 类型 可读 可写 说明
deviceId string 表示要配对的设备ID。
pinCode string 表示要配对的密钥。
pinType PinType 表示要配对的设备类型。
此接口为系统接口。

DeviceClass

描述蓝牙设备的类别。

系统能力:SystemCapability.Communication.Bluetooth.Core。

名称 类型 可读 可写 说明
majorClass MajorClass 表示蓝牙设备主要类别的枚举。
majorMinorClass 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码。
此接口为系统接口。

你可能感兴趣的鸿蒙文章

harmony 鸿蒙接口

harmony 鸿蒙系统公共事件定义(待停用)

harmony 鸿蒙系统公共事件定义

harmony 鸿蒙开发说明

harmony 鸿蒙企业设备管理概述(仅对系统应用开放)

harmony 鸿蒙BundleStatusCallback

harmony 鸿蒙@ohos.bundle.innerBundleManager (innerBundleManager模块)

harmony 鸿蒙@ohos.distributedBundle (分布式包管理)

harmony 鸿蒙@ohos.bundle (Bundle模块)

harmony 鸿蒙@ohos.enterprise.EnterpriseAdminExtensionAbility (企业设备管理扩展能力)

0  赞