目录

remoteDevice (Capability to Connect to a Remote Device)

This module provides functions such as querying remote device information and initiating pairing.

System capability: SystemCapability.Communication.NearLink.Core

Since: 5.0.1(13)

Modules to Import

import { remoteDevice } from '@kit.NearLinkKit';

PairingState

type PairingState = constant.PairingState

Enumerated values of pairing statuses with a remote device.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Communication.NearLink.Core

Since: 5.0.1(13)

Type Description
constant.PairingState Pairing status with a remote device.

ConnectionState

type ConnectionState = constant.ConnectionState

Enumerated values of connection statuses with a remote device.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Communication.NearLink.Core

Since: 5.0.1(13)

Type Description
constant.ConnectionState Connection status with a remote device.

DeviceClass

type DeviceClass = constant.DeviceClass

Enumerated values of device types.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Communication.NearLink.Core

Since: 5.0.1(13)

Type Description
constant.DeviceClass Device type.

AcbState

type AcbState = constant.AcbState

Enumerated values of logical link connection statuses with a remote device.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Communication.NearLink.Core

Since: 5.1.0(18)

Type Description
constant.AcbState Logical link connection status with a remote device.

createRemoteDevice

createRemoteDevice(address: string): RemoteDevice

Creates a remote device instance.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Communication.NearLink.Core

Since: 5.0.1(13)

Parameters:

Parameter Type Mandatory Description
address string Yes Address of a remote device. The address format is 11:22:33:AA:BB:FF.

Returns:

Type Description
RemoteDevice Remote device instance.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

ID Error Message
801 Capability not supported.

Example:

import { remoteDevice } from '@kit.NearLinkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let addr: string = '00:11:22:33:AA:FF'; // Remote device address obtained through scanning.
let device: remoteDevice.RemoteDevice;
try {
  device = remoteDevice.createRemoteDevice(addr);
  console.info('device: ' + JSON.stringify(device));
} catch (err) {
  console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

RemoteDevice

Provides the method for operating on a remote device. Before using this method, you need to use the remoteDevice.createRemoteDevice method to create a RemoteDevice instance. You need to create only one instance for a device.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Communication.NearLink.Core

Since: 5.0.1(13)

[h2]startPairing

startPairing(): Promise<void>

Initiates pairing with a remote device. This API uses a promise to return the result. After the pairing is initiated, different types of dialog boxes will be displayed based on the input and output capability flags of both the local and remote devices. The user will need to confirm the pairing.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ACCESS_NEARLINK

System capability: SystemCapability.Communication.NearLink.Core

Since: 5.1.0(18)

Returns:

Type Description
Promise<void> Promise that returns no value.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

ID Error Message
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

Example:

import { remoteDevice } from '@kit.NearLinkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let addr: string = '00:11:22:33:AA:FF'; // Remote device address obtained through scanning.
let device: remoteDevice.RemoteDevice;
try {
  device = remoteDevice.createRemoteDevice(addr);
  device.startPairing().then(()=>{
    console.info('start pairing success');
  }).catch ((err: BusinessError) => {
    console.error('errCode: ' + err.code + ', errMessage: ' + err.message);
  });
} catch (err) {
  console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

[h2]getPairingState

getPairingState(): PairingState

Obtains the pairing status with a remote device.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ACCESS_NEARLINK

System capability: SystemCapability.Communication.NearLink.Core

Since: 5.0.1(13)

Returns:

Type Description
PairingState Pairing status with a remote device.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

ID Error Message
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

Example:

import { remoteDevice } from '@kit.NearLinkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let addr: string = '00:11:22:33:AA:FF'; // Remote device address obtained through scanning.
let device: remoteDevice.RemoteDevice;
try {
  device = remoteDevice.createRemoteDevice(addr);
  let state: remoteDevice.PairingState = device.getPairingState();
  console.info('state:' + JSON.stringify(state));
} catch (err) {
  console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

[h2]getDeviceName

getDeviceName(): string

Obtains the name of a remote device.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ACCESS_NEARLINK

System capability: SystemCapability.Communication.NearLink.Core

Since: 5.0.1(13)

Returns:

Type Description
string Remote device name. The value contains a maximum of 30 characters.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

ID Error Message
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

Example:

import { remoteDevice } from '@kit.NearLinkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let addr: string = '00:11:22:33:AA:FF'; // Remote device address obtained through scanning.
let device: remoteDevice.RemoteDevice;
try {
  device = remoteDevice.createRemoteDevice(addr);
  let name: string = device.getDeviceName();
  console.info('name:' + JSON.stringify(name));
} catch (err) {
  console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

[h2]getDeviceClass

getDeviceClass(): DeviceClass

Obtains the type of a remote device.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ACCESS_NEARLINK

System capability: SystemCapability.Communication.NearLink.Core

Since: 5.0.1(13)

Returns:

Type Description
DeviceClass Remote device type.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

ID Error Message
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

Example:

import { remoteDevice } from '@kit.NearLinkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let addr: string = '00:11:22:33:AA:FF'; // Remote device address obtained through scanning.
let device: remoteDevice.RemoteDevice;
try {
  device = remoteDevice.createRemoteDevice(addr);
  let type: remoteDevice.DeviceClass = device.getDeviceClass();
  console.info('type:' + JSON.stringify(type));
} catch (err) {
  console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

[h2]getConnectionState

getConnectionState(): ConnectionState

Obtains the connection status between the local and remote devices.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ACCESS_NEARLINK

System capability: SystemCapability.Communication.NearLink.Core

Since: 5.0.1(13)

Returns:

Type Description
ConnectionState Connection status between the local and remote devices.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

ID Error Message
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

Example:

import { remoteDevice } from '@kit.NearLinkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let addr: string = '00:11:22:33:AA:FF'; // Remote device address obtained through scanning.
let device: remoteDevice.RemoteDevice;
try {
  device = remoteDevice.createRemoteDevice(addr);
  let state: remoteDevice.ConnectionState = device.getConnectionState();
  console.info('state:' + JSON.stringify(state));
} catch (err) {
  console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

[h2]getAcbState

getAcbState(): AcbState

Obtains the logical link connection status with a remote device.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ACCESS_NEARLINK

System capability: SystemCapability.Communication.NearLink.Core

Since: 5.1.0(18)

Returns:

Type Description
AcbState Logical link connection status with a remote device.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

ID Error Message
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

Example:

import { remoteDevice } from '@kit.NearLinkKit';
import { BusinessError } from '@kit.BasicServicesKit';

let addr: string = '00:11:22:33:AA:FF'; // Remote device address obtained through scanning.
let device: remoteDevice.RemoteDevice;
try {
  device = remoteDevice.createRemoteDevice(addr);
  let state: remoteDevice.AcbState = device.getAcbState();
  console.info('state:' + JSON.stringify(state));
} catch (err) {
  console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

[h2]getDeviceInformation

getDeviceInformation(): DeviceInformation

Obtains the device information of a remote device.

Model restriction: This API can be used only in the stage model.

Required permissions: ohos.permission.ACCESS_NEARLINK

System capability: SystemCapability.Communication.NearLink.Core

Since: 6.1.1(24)

Returns:

Type Description
DeviceInformation Device information of a remote device.

Error codes:

For details about the error codes, please refer to ArkTS API Error Codes.

ID Error Message
201 Permission denied.
801 Capability not supported.
1009700003 NearLink is off.
1009700099 Operation failed.

Example:

import { remoteDevice } from '@kit.NearLinkKit';
import { BusinessError } from '@kit.BasicServicesKit';
import { buffer } from '@kit.ArkTS';

let addr: string = '00:11:22:33:AA:FF'; // Remote device address obtained through scanning.
let device: remoteDevice.RemoteDevice;
try {
  device = remoteDevice.createRemoteDevice(addr);
  let deviceInfo: remoteDevice.DeviceInformation = device.getDeviceInformation();
  console.info('deviceInfo.manufacturerData:' + buffer.from(deviceInfo.manufacturerData, 'binary').toString('hex'));
  console.info('deviceInfo.modelData:' + buffer.from(deviceInfo.modelData, 'binary').toString('hex'));
} catch (err) {
  console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
}

DeviceInformation

Remote device information.

Model restriction: This API can be used only in the stage model.

System capability: SystemCapability.Communication.NearLink.Core

Since: 6.1.1(24)

Name Type Read-Only Optional Description
manufacturerData string No No Manufacturer information.
modelData string No No Device model.