本模块提供了星闪数据传输功能,包括端口通道管理、连接管理、数据收发、连接状态查询与订阅等。
起始版本: 26.0.0
type ConnectionState = nearlinkConstant.ConnectionState
表示和远端设备的连接状态,为枚举值。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Communication.NearLink.Base
| 类型 | 说明 |
|---|---|
| nearlinkConstant.ConnectionState | 和远端设备的连接状态。 |
createPort(uuid: string): void
注册端口通道。端口通道注册后方可用于连接远端设备,不再使用时需通过dataTransfer.destroyPort销毁。
调用此接口前,您可以调用isNearLinkSupported查询设备是否支持星闪。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
需要权限: ohos.permission.ACCESS_NEARLINK
系统能力: SystemCapability.Communication.NearLink.Base
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| uuid | string | 是 | 星闪服务UUID,长度必须为36个字符,由32个十六进制数字和4个连字符(-)组成,例如: FFFFFFFF-1234-5678-ABCD-000000001234,表示一个128位标识符。 不允许使用星闪标准UUID。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 801 | Capability not supported. Possible causes: 1. The hardware does not support the capability; 2. The chip does not support the capability; 3. A dependent service feature is not supported. |
| 36100003 | NearLink disabled. |
| 36100020 | The UUID is already registered. |
| 36100021 | Port exceeds the upper limit. |
| 36100043 | Invalid UUID. |
| 36100044 | NearLink standard UUID not allowed. |
| 36100099 | Operation failed. |
示例:
import { dataTransfer } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let uuid: string = 'FFFFFFFF-FC70-11EA-B720-000078951234'; // 星闪服务UUID
dataTransfer.createPort(uuid);
console.info('create port success');
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
} destroyPort(uuid: string): void
销毁端口通道。
调用此接口前,您可以调用isNearLinkSupported查询设备是否支持星闪。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
需要权限: ohos.permission.ACCESS_NEARLINK
系统能力: SystemCapability.Communication.NearLink.Base
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| uuid | string | 是 | 星闪服务UUID,长度必须为36个字符,由32个十六进制数字和4个连字符(-)组成,例如: FFFFFFFF-1234-5678-ABCD-000000001234,表示一个128位标识符。 不允许使用星闪标准UUID。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 801 | Capability not supported. Possible causes: 1. The hardware does not support the capability; 2. The chip does not support the capability; 3. A dependent service feature is not supported. |
| 36100003 | NearLink disabled. |
| 36100022 | The UUID is not registered. |
| 36100043 | Invalid UUID. |
| 36100044 | NearLink standard UUID not allowed. |
| 36100099 | Operation failed. |
示例:
import { dataTransfer } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let uuid: string = 'FFFFFFFF-FC70-11EA-B720-000078951234'; // 星闪服务UUID
dataTransfer.destroyPort(uuid);
console.info('destroy port success');
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
} connect(params: ConnectionParams): Promise<void>
连接远端设备。使用Promise异步回调。
调用此接口前,您可以调用isNearLinkSupported查询设备是否支持星闪。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
需要权限: ohos.permission.ACCESS_NEARLINK
系统能力: SystemCapability.Communication.NearLink.Base
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| params | ConnectionParams | 是 | 指明端口的连接参数。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象,无返回结果。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 801 | Capability not supported. Possible causes: 1. The hardware does not support the capability; 2. The chip does not support the capability; 3. A dependent service feature is not supported. |
| 36100003 | NearLink disabled. |
| 36100041 | Invalid address. |
| 36100043 | Invalid UUID. |
| 36100044 | NearLink standard UUID not allowed. |
| 36100099 | Operation failed. |
示例:
import { dataTransfer } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
// 构造连接参数
let connectionParams:dataTransfer.ConnectionParams = {
address: '01:02:03:04:05:06', // 星闪远端设备地址,可通过扫描获取
uuid: 'FFFFFFFF-1234-5678-ABCD-000000001234', // 星闪服务UUID
};
dataTransfer.connect(connectionParams).then(() => {
console.info('connect 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);
} disconnect(params: ConnectionParams): Promise<void>
断连远端设备。需在通过dataTransfer.connect成功建立连接后调用,用于断开已建立的远端设备连接。使用Promise异步回调。
调用此接口前,您可以调用isNearLinkSupported查询设备是否支持星闪。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
需要权限: ohos.permission.ACCESS_NEARLINK
系统能力: SystemCapability.Communication.NearLink.Base
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| params | ConnectionParams | 是 | 指明端口的连接参数。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象,无返回结果。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 801 | Capability not supported. Possible causes: 1. The hardware does not support the capability; 2. The chip does not support the capability; 3. A dependent service feature is not supported. |
| 36100003 | NearLink disabled. |
| 36100041 | Invalid address. |
| 36100043 | Invalid UUID. |
| 36100044 | NearLink standard UUID not allowed. |
| 36100099 | Operation failed. |
示例:
import { dataTransfer } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
// 构造连接参数,用于指定要断开的连接
let connectionParams:dataTransfer.ConnectionParams = {
address: '01:02:03:04:05:06', // 星闪远端设备地址,可通过扫描获取
uuid: 'FFFFFFFF-1234-5678-ABCD-000000001234', // 星闪服务UUID
};
dataTransfer.disconnect(connectionParams).then(() => {
console.info('disconnect 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);
} onConnectionStateChanged(callback: Callback<ConnectionResult>): void
订阅端口通道连接状态变更事件。使用callback异步回调。
应用需具备ohos.permission.ACCESS_NEARLINK权限,方可接收此事件上报。
调用此接口前,您可以调用isNearLinkSupported查询设备是否支持星闪。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Communication.NearLink.Base
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | Callback<ConnectionResult> | 是 | 回调函数,返回与远端设备端口连接参数的协商结果。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 801 | Capability not supported. Possible causes: 1. The hardware does not support the capability; 2. The chip does not support the capability; 3. A dependent service feature is not supported. |
| 36100099 | Operation failed. |
示例:
import { dataTransfer } from '@kit.ConnectivityKit';
import { BusinessError, Callback } from '@kit.BasicServicesKit';
let callback: Callback<dataTransfer.ConnectionResult> = (data: dataTransfer.ConnectionResult) => {
console.info('data: ' + JSON.stringify(data));
};
try {
dataTransfer.onConnectionStateChanged(callback);
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
} offConnectionStateChanged(callback?: Callback<ConnectionResult>): void
取消订阅端口通道连接状态变更事件。使用callback异步回调。
调用此接口前,您可以调用isNearLinkSupported查询设备是否支持星闪。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Communication.NearLink.Base
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | Callback<ConnectionResult> | 否 | 回调函数,返回与远端设备端口连接参数的协商结果。 填写该参数则取消当前callback订阅。不填写该参数则取消该事件对应的所有回调。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 801 | Capability not supported. Possible causes: 1. The hardware does not support the capability; 2. The chip does not support the capability; 3. A dependent service feature is not supported. |
| 36100099 | Operation failed. |
示例:
import { dataTransfer } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
dataTransfer.offConnectionStateChanged();
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
} getConnectionState(params: ConnectionStateParams): ConnectionState
获取与远端设备之间的端口通道连接状态。
调用此接口前,您可以调用isNearLinkSupported查询设备是否支持星闪。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
需要权限: ohos.permission.ACCESS_NEARLINK
系统能力: SystemCapability.Communication.NearLink.Base
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| params | ConnectionStateParams | 是 | 指明端口的连接参数。 |
返回值:
| 类型 | 说明 |
|---|---|
| ConnectionState | 和远端设备的星闪端口通道连接状态。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 801 | Capability not supported. Possible causes: 1. The hardware does not support the capability; 2. The chip does not support the capability; 3. A dependent service feature is not supported. |
| 36100003 | NearLink disabled. |
| 36100041 | Invalid address. |
| 36100043 | Invalid UUID in connection parameters. |
| 36100044 | NearLink standard UUID not allowed. |
| 36100099 | Operation failed. |
示例:
import { dataTransfer } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
let connectionStateParams:dataTransfer.ConnectionStateParams = {
address: '01:02:03:04:05:06', // 扫描获取到的远端设备地址
uuid: 'FFFFFFFF-FC70-11EA-B720-000078951234' // 星闪服务UUID示例
};
let state:dataTransfer.ConnectionState = dataTransfer.getConnectionState(connectionStateParams);
console.info('state:' + JSON.stringify(state));
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
} writeData(params: DataParams): Promise<void>
通过设备地址和UUID向远端设备发送数据。使用Promise异步回调。
调用此接口前,您可以调用isNearLinkSupported查询设备是否支持星闪。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
需要权限: ohos.permission.ACCESS_NEARLINK
系统能力: SystemCapability.Communication.NearLink.Base
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| params | DataParams | 是 | 指明发送数据的参数,包含远端设备地址、服务UUID以及发送的数据包。 |
返回值:
| 类型 | 说明 |
|---|---|
| Promise<void> | Promise对象,无返回结果。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 201 | Permission verification failed. The application does not have the permission required to call the API. |
| 801 | Capability not supported. Possible causes: 1. The hardware does not support the capability; 2. The chip does not support the capability; 3. A dependent service feature is not supported. |
| 36100003 | NearLink disabled. |
| 36100023 | Data transmission congested. |
| 36100041 | Invalid address. |
| 36100043 | Invalid UUID. |
| 36100044 | NearLink standard UUID not allowed. |
| 36100099 | Operation failed. |
示例:
import { dataTransfer } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
// 构造发送数据参数
let transferValueBuffer: Uint8Array = new Uint8Array(4);
transferValueBuffer[0] = 1;
transferValueBuffer[1] = 2;
transferValueBuffer[2] = 3;
transferValueBuffer[3] = 4;
let dataParams: dataTransfer.DataParams = {
address: '01:02:03:04:05:06', // 星闪远端设备地址
uuid: 'FFFFFFFF-1234-5678-ABCD-000000001234', // 星闪服务UUID
data: transferValueBuffer.buffer // 星闪设备间传输的数据
};
dataTransfer.writeData(dataParams).then(() => {
console.info('writeData 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);
} onReadData(callback: Callback<DataParams>): void
订阅端口通道数据接收事件。使用callback异步回调。
应用需具备ohos.permission.ACCESS_NEARLINK权限,方可接收此事件上报。
调用此接口前,您可以调用isNearLinkSupported查询设备是否支持星闪。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Communication.NearLink.Base
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | Callback<DataParams> | 是 | 回调函数,返回端口通道接收到的数据参数。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 801 | Capability not supported. Possible causes: 1. The hardware does not support the capability; 2. The chip does not support the capability; 3. A dependent service feature is not supported. |
| 36100099 | Operation failed. |
示例:
import { dataTransfer } from '@kit.ConnectivityKit';
import { BusinessError, Callback } from '@kit.BasicServicesKit';
let callback: Callback<dataTransfer.DataParams> = (data: dataTransfer.DataParams) => {
console.info('data: ' + JSON.stringify(data));
};
try {
dataTransfer.onReadData(callback);
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
} offReadData(callback?: Callback<DataParams>): void
取消订阅端口通道数据接收事件。使用callback异步回调。
调用此接口前,您可以调用isNearLinkSupported查询设备是否支持星闪。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Communication.NearLink.Base
参数:
| 参数名 | 类型 | 必填 | 说明 |
|---|---|---|---|
| callback | Callback<DataParams> | 否 | 回调函数,返回端口通道接收到的数据参数。 填写该参数则取消当前callback订阅。不填写该参数则取消该事件对应的所有回调。 |
错误码:
| 错误码ID | 错误信息 |
|---|---|
| 801 | Capability not supported. Possible causes: 1. The hardware does not support the capability; 2. The chip does not support the capability; 3. A dependent service feature is not supported. |
| 36100099 | Operation failed. |
示例:
import { dataTransfer } from '@kit.ConnectivityKit';
import { BusinessError } from '@kit.BasicServicesKit';
try {
dataTransfer.offReadData();
} catch (err) {
console.error('errCode: ' + (err as BusinessError).code + ', errMessage: ' + (err as BusinessError).message);
} 发起端口连接的参数。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Communication.NearLink.Base
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| address | string | 否 | 否 | 远端设备的星闪地址。地址格式参考:11:22:33:AA:BB:FF。 |
| uuid | string | 否 | 否 | 星闪服务UUID,长度必须为36个字符,由32个十六进制数字和4个连字符(-)组成,例如: FFFFFFFF-1234-5678-ABCD-000000001234,表示一个128位标识符。 不允许使用星闪标准UUID。 |
| transferMode | TransferMode | 否 | 是 | 表示和远端设备的数据传输模式。默认值是BASIC。 |
端口数据发送和接收的参数。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Communication.NearLink.Base
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| address | string | 否 | 否 | 远端设备的星闪地址。地址格式参考:11:22:33:AA:BB:FF。 |
| uuid | string | 否 | 否 | 星闪服务UUID,长度必须为36个字符,由32个十六进制数字和4个连字符(-)组成,例如: FFFFFFFF-1234-5678-ABCD-000000001234,表示一个128位标识符。 不允许使用星闪标准UUID。 |
| data | ArrayBuffer | 否 | 否 | 数据包。通过dataTransfer.writeData发送时表示待发送的数据,通过dataTransfer.onReadData接收时表示接收到的数据。 |
与远端设备端口连接参数的协商结果
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Communication.NearLink.Base
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| address | string | 否 | 否 | 远端设备的星闪地址。地址格式参考:11:22:33:AA:BB:FF。 |
| uuid | string | 否 | 否 | 星闪服务UUID,长度必须为36个字符,由32个十六进制数字和4个连字符(-)组成,例如: FFFFFFFF-1234-5678-ABCD-000000001234,表示一个128位标识符。 不允许使用星闪标准UUID。 |
| mtu | number | 否 | 否 | 协商后的发送和接收数据的包长,单位为byte,范围[0, 65535]。 |
| state | ConnectionState | 否 | 否 | 与远端设备的连接状态。 |
获取端口通道连接状态所需参数。
起始版本: 26.0.0
模型约束: 此接口仅可在Stage模型下使用。
系统能力: SystemCapability.Communication.NearLink.Base
| 名称 | 类型 | 只读 | 可选 | 说明 |
|---|---|---|---|---|
| address | string | 否 | 否 | 远端设备的星闪地址。地址格式参考:11:22:33:AA:BB:FF。 |
| uuid | string | 否 | 否 | 星闪服务UUID,长度必须为36个字符,由32个十六进制数字和4个连字符(-)组成,例如: FFFFFFFF-1234-5678-ABCD-000000001234,表示一个128位标识符。 不允许使用星闪标准UUID。 |