文档管理中心
您当前正在浏览HarmonyOS最新文档,覆盖已发布的所有API版本,可在API参考中筛选您使用的API版本。详细的版本配套关系请参考版本说明
API参考系统网络NearLink Kit(星闪服务)ArkTS APIremoteDevice(对端设备的连接能力)

remoteDevice(对端设备的连接能力)

本模块提供了查询远端设备信息、发起配对等功能。

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 5.0.1(13)

导入模块

收起
自动换行
深色代码主题
复制
  1. import { remoteDevice } from '@kit.NearLinkKit';

PairingState

type PairingState = constant.PairingState

表示和远端设备的配对状态,为枚举值。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 5.0.1(13)

展开
类型 说明
constant.PairingState 和远端设备的配对状态。

ConnectionState

type ConnectionState = constant.ConnectionState

表示和远端设备的连接状态,为枚举值。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 5.0.1(13)

展开
类型 说明
constant.ConnectionState 和远端设备的连接状态。

DeviceClass

type DeviceClass = constant.DeviceClass

表示设备类型,为枚举值。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 5.0.1(13)

展开
类型 说明
constant.DeviceClass 设备类型。

AcbState

type AcbState = constant.AcbState

表示和远端设备的逻辑链路连接状态,为枚举值。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 5.1.0(18)

展开
类型 说明
constant.AcbState 和远端设备的逻辑链路连接状态。

createRemoteDevice

Phone5.0.1(13)+PC/2in15.0.1(13)+Tablet5.0.1(13)+TV5.1.1(19)+Wearable5.1.0(18)+

createRemoteDevice(address: string): RemoteDevice

创建远端设备实例。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 5.0.1(13)

参数:

展开
参数名 类型 必填 说明
address string 远端设备地址。地址格式参考:11:22:33:AA:BB:FF。

返回值:

展开
类型 说明
RemoteDevice 远端设备实例。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
801 Capability not supported.

示例:

收起
自动换行
深色代码主题
复制
  1. import { remoteDevice } from '@kit.NearLinkKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. let addr: string = '00:11:22:33:AA:FF'; // 扫描获取到的远端设备地址
  4. let device: remoteDevice.RemoteDevice;
  5. try {
  6. device = remoteDevice.createRemoteDevice(addr);
  7. console.info('device: ' + JSON.stringify(device));
  8. } catch (err) {
  9. console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  10. }

RemoteDevice

说明

提供远端设备的操作方法,使用前需要使用remoteDevice.createRemoteDevice方法创建一个远端设备RemoteDevice实例。一个设备只需要创建一次,无需多次创建。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 5.0.1(13)

startPairing

startPairing(): Promise<void>

发起与远端设备的配对。使用Promise异步回调。发起配对后,将依据本端与远端设备的输入输出能力标识弹出不同类型的弹窗,需使用者进一步确认。

模型约束: 此接口仅可在Stage模型下使用。

需要权限: ohos.permission.ACCESS_NEARLINK

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 5.1.0(18)

返回值:

展开
类型 说明
Promise<void> Promise对象,无返回结果。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

示例:

收起
自动换行
深色代码主题
复制
  1. import { remoteDevice } from '@kit.NearLinkKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. let addr: string = '00:11:22:33:AA:FF'; // 扫描获取到的远端设备地址
  4. let device: remoteDevice.RemoteDevice;
  5. try {
  6. device = remoteDevice.createRemoteDevice(addr);
  7. device.startPairing().then(()=>{
  8. console.info('start pairing success');
  9. }).catch ((err: BusinessError) => {
  10. console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
  11. });
  12. } catch (err) {
  13. console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  14. }

getPairingState

getPairingState(): PairingState

获取和远端设备的配对状态。

模型约束: 此接口仅可在Stage模型下使用。

需要权限: ohos.permission.ACCESS_NEARLINK

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 5.0.1(13)

返回值:

展开
类型 说明
PairingState 和远端设备的配对状态。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

示例:

收起
自动换行
深色代码主题
复制
  1. import { remoteDevice } from '@kit.NearLinkKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. let addr: string = '00:11:22:33:AA:FF'; // 扫描获取到的远端设备地址
  4. let device: remoteDevice.RemoteDevice;
  5. try {
  6. device = remoteDevice.createRemoteDevice(addr);
  7. let state: remoteDevice.PairingState = device.getPairingState();
  8. console.info('state:' + JSON.stringify(state));
  9. } catch (err) {
  10. console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  11. }

getDeviceName

getDeviceName(): string

获取远端设备名称。

模型约束: 此接口仅可在Stage模型下使用。

需要权限: ohos.permission.ACCESS_NEARLINK

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 5.0.1(13)

返回值:

展开
类型 说明
string 远端设备名称。最大长度为30。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

示例:

收起
自动换行
深色代码主题
复制
  1. import { remoteDevice } from '@kit.NearLinkKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. let addr: string = '00:11:22:33:AA:FF'; // 扫描获取到的远端设备地址
  4. let device: remoteDevice.RemoteDevice;
  5. try {
  6. device = remoteDevice.createRemoteDevice(addr);
  7. let name: string = device.getDeviceName();
  8. console.info('name:' + JSON.stringify(name));
  9. } catch (err) {
  10. console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  11. }

getDeviceClass

Phone5.0.1(13)+PC/2in15.0.1(13)+Tablet5.0.1(13)+TV5.1.1(19)+Wearable5.1.0(18)+

getDeviceClass(): DeviceClass

获取远端设备类型。

模型约束: 此接口仅可在Stage模型下使用。

需要权限: ohos.permission.ACCESS_NEARLINK

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 5.0.1(13)

返回值:

展开
类型 说明
DeviceClass 远端设备类型。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

示例:

收起
自动换行
深色代码主题
复制
  1. import { remoteDevice } from '@kit.NearLinkKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. let addr: string = '00:11:22:33:AA:FF'; // 扫描获取到的远端设备地址
  4. let device: remoteDevice.RemoteDevice;
  5. try {
  6. device = remoteDevice.createRemoteDevice(addr);
  7. let type: remoteDevice.DeviceClass = device.getDeviceClass();
  8. console.info('type:' + JSON.stringify(type));
  9. } catch (err) {
  10. console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  11. }

getConnectionState

getConnectionState(): ConnectionState

获取本端设备和远端设备的连接状态。

模型约束: 此接口仅可在Stage模型下使用。

需要权限: ohos.permission.ACCESS_NEARLINK

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 5.0.1(13)

返回值:

展开
类型 说明
ConnectionState 本端设备和远端设备的连接状态。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

示例:

收起
自动换行
深色代码主题
复制
  1. import { remoteDevice } from '@kit.NearLinkKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. let addr: string = '00:11:22:33:AA:FF'; // 扫描获取到的远端设备地址
  4. let device: remoteDevice.RemoteDevice;
  5. try {
  6. device = remoteDevice.createRemoteDevice(addr);
  7. let state: remoteDevice.ConnectionState = device.getConnectionState();
  8. console.info('state:' + JSON.stringify(state));
  9. } catch (err) {
  10. console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  11. }

getAcbState

getAcbState(): AcbState

获取和远端设备的逻辑链路连接状态。

模型约束: 此接口仅可在Stage模型下使用。

需要权限: ohos.permission.ACCESS_NEARLINK

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 5.1.0(18)

返回值:

展开
类型 说明
AcbState 和远端设备的逻辑链路连接状态。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

示例:

收起
自动换行
深色代码主题
复制
  1. import { remoteDevice } from '@kit.NearLinkKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. let addr: string = '00:11:22:33:AA:FF'; // 扫描获取到的远端设备地址
  4. let device: remoteDevice.RemoteDevice;
  5. try {
  6. device = remoteDevice.createRemoteDevice(addr);
  7. let state: remoteDevice.AcbState = device.getAcbState();
  8. console.info('state:' + JSON.stringify(state));
  9. } catch (err) {
  10. console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  11. }

getDeviceInformation

getDeviceInformation(): DeviceInformation

获取远端设备的设备信息。

模型约束: 此接口仅可在Stage模型下使用。

需要权限: ohos.permission.ACCESS_NEARLINK

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 6.1.1(24)

返回值:

展开
类型 说明
DeviceInformation 远端设备的设备信息。

错误码:

以下错误码的详细介绍请参见ArkTS API错误码

展开
错误码ID 错误信息
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

示例:

收起
自动换行
深色代码主题
复制
  1. import { remoteDevice } from '@kit.NearLinkKit';
  2. import { BusinessError } from '@kit.BasicServicesKit';
  3. import { buffer } from '@kit.ArkTS';
  4. let addr: string = '00:11:22:33:AA:FF'; // 扫描获取到的远端设备地址
  5. let device: remoteDevice.RemoteDevice;
  6. try {
  7. device = remoteDevice.createRemoteDevice(addr);
  8. let deviceInfo: remoteDevice.DeviceInformation = device.getDeviceInformation();
  9. console.info('deviceInfo.manufacturerData:' + buffer.from(deviceInfo.manufacturerData, 'binary').toString('hex'));
  10. console.info('deviceInfo.modelData:' + buffer.from(deviceInfo.modelData, 'binary').toString('hex'));
  11. } catch (err) {
  12. console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
  13. }

DeviceInformation

Phone6.1.1(24)+PC/2in16.1.1(24)+Tablet6.1.1(24)+TV6.1.1(24)+Wearable6.1.1(24)+

表示远端设备信息。

模型约束: 此接口仅可在Stage模型下使用。

系统能力: SystemCapability.Communication.NearLink.Core

起始版本: 6.1.1(24)

展开
名称 类型 只读 可选 说明
manufacturerData string 厂商信息。
modelData string 设备型号信息。
在 API参考 中进行搜索
请输入您想要搜索的关键词