@ohos.distributedsched.abilityConnectionManager (Cross-Device Connection Management)
The abilityConnectionManager module provides APIs for cross-device connection management. After successful networking between devices (login with the same account and enabling of Bluetooth on the devices), a system application and a third-party application can start a UIAbility of the same application across these devices to establish a Bluetooth connection. This way, data (specifically, text) can be transmitted across the devices over the connection.
NOTE
The initial APIs of this module are supported since API version 18. Newly added APIs will be marked with a superscript to indicate their earliest API version.
The APIs of this module can be used only in the stage model.
Modules to Import
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
abilityConnectionManager.createAbilityConnectionSession
createAbilityConnectionSession(serviceName: string, context: Context, peerInfo: PeerInfo , connectOptions: ConnectOptions): number
Creates a collaboration session between applications.
Required permissions: ohos.permission.INTERNET, ohos.permission.GET_NETWORK_INFO, ohos.permission.SET_NETWORK_INFO, and ohos.permission.DISTRIBUTED_DATASYNC
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| serviceName | string | Yes | Service name for the application. The service name must be the same on the local end and peer end. The value contains a maximum of 256 characters. |
| context | Context | Yes | Application context. |
| peerInfo | PeerInfo | Yes | Collaboration information of the peer end. |
| connectOptions | ConnectOptions | Yes | Connection options for the application. |
Return value
| Type | Description |
|---|---|
| number | ID of the collaboration session. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 201 | Permission denied. |
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
| 801 | Capability not supported. Failed to call the API due to limited device capabilities. |
Example
-
On device A, an application calls createAbilityConnectionSession() to create a collaboration session and return the session ID.
import { abilityConnectionManager, distributedDeviceManager } from '@kit.DistributedServiceKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; let dmClass: distributedDeviceManager.DeviceManager; function initDmClass(): void { try { dmClass = distributedDeviceManager.createDeviceManager('com.example.remotephotodemo'); } catch (err) { hilog.error(0x0000, 'testTag', 'createDeviceManager err: ' + JSON.stringify(err)); } } function getRemoteDeviceId(): string | undefined { initDmClass(); if (typeof dmClass === 'object' && dmClass !== null) { hilog.info(0x0000, 'testTag', 'getRemoteDeviceId begin'); let list = dmClass.getAvailableDeviceListSync(); if (typeof (list) === 'undefined' || typeof (list.length) === 'undefined') { hilog.info(0x0000, 'testTag', 'getRemoteDeviceId err: list is null'); return; } if (list.length === 0) { hilog.info(0x0000, 'testTag', 'getRemoteDeviceId err: list is empty'); return; } return list[0].networkId; } else { hilog.info(0x0000, 'testTag', 'getRemoteDeviceId err: dmClass is null'); return; } } @Entry @Component struct Index { createSession(): void { // Define peer device information. const peerInfo: abilityConnectionManager.PeerInfo = { deviceId: getRemoteDeviceId()!, bundleName: 'com.example.remotephotodemo', moduleName: 'entry', abilityName: 'EntryAbility', serviceName: 'collabTest' }; const myRecord: Record<string, string> = { "newKey1": "value1", }; // Define connection options. const connectOptions: abilityConnectionManager.ConnectOptions = { needSendData: true, startOptions: abilityConnectionManager.StartOptionParams.START_IN_FOREGROUND, parameters: myRecord }; let context = this.getUIContext().getHostContext(); try { let sessionId = abilityConnectionManager.createAbilityConnectionSession("collabTest", context, peerInfo, connectOptions); hilog.info(0x0000, 'testTag', 'createSession sessionId is', sessionId); } catch (error) { hilog.error(0x0000, 'testTag', error); } } build() { } } -
On device B, createAbilityConnectionSession can be called in onCollaborate, which is triggered when the application is started.
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit'; import { abilityConnectionManager } from '@kit.DistributedServiceKit'; import { hilog } from '@kit.PerformanceAnalysisKit'; export default class EntryAbility extends UIAbility { onCollaborate(wantParam: Record<string, Object>): AbilityConstant.CollaborateResult { hilog.info(0x0000, 'testTag', '%{public}s', 'on collaborate'); let param = wantParam["ohos.extra.param.key.supportCollaborateIndex"] as Record<string, Object> this.onCollab(param); return 0; } onCollab(collabParam: Record<string, Object>) { const sessionId = this.createSessionFromWant(collabParam); if (sessionId == -1) { hilog.info(0x0000, 'testTag', 'Invalid session ID.'); return; } } createSessionFromWant(collabParam: Record<string, Object>): number { let sessionId = -1; const peerInfo = collabParam["PeerInfo"] as abilityConnectionManager.PeerInfo; if (peerInfo == undefined) { return sessionId; } const options = collabParam["ConnectOption"] as abilityConnectionManager.ConnectOptions; try { sessionId = abilityConnectionManager.createAbilityConnectionSession("collabTest", this.context, peerInfo, options); AppStorage.setOrCreate('sessionId', sessionId); hilog.info(0x0000, 'testTag', 'createSession sessionId is' + sessionId); } catch (error) { hilog.error(0x0000, 'testTag', error); } return sessionId; } }
abilityConnectionManager.destroyAbilityConnectionSession
destroyAbilityConnectionSession(sessionId: number): void
Destroys a collaboration session between applications.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| sessionId | number | Yes | Collaboration session ID. The value is an integer greater than 100. |
Example
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
hilog.info(0x0000, 'testTag', 'destroyAbilityConnectionSession called');
let sessionId = 100;
abilityConnectionManager.destroyAbilityConnectionSession(sessionId);
abilityConnectionManager.getPeerInfoById
getPeerInfoById(sessionId: number): PeerInfo | undefined
Obtains information about the peer application in the specified session.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| sessionId | number | Yes | ID of the collaboration session. |
Return value
| Type | Description |
|---|---|
| PeerInfo | undefined | Information about the peer application if the corresponding PeerInfo exists; undefined if the session ID is not found. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
Example
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
hilog.info(0x0000, 'testTag', 'getPeerInfoById called');
let sessionId = 100;
const peerInfo = abilityConnectionManager.getPeerInfoById(sessionId);
abilityConnectionManager.connect
connect(sessionId: number): Promise<ConnectResult>
Sets up a UIAbility connection after a collaboration session is created and the session ID is obtained. This API uses a promise to return the result.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| sessionId | number | Yes | ID of the collaboration session. |
Return value
| Type | Description |
|---|---|
| Promise<ConnectResult> | Promise used to return the connection result. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
Example
After an application sets up a collaboration session and obtains the session ID on device A, it calls connect() to set up a UIAbility connection and start the application on device B.
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let sessionId = 100;
abilityConnectionManager.connect(sessionId).then((ConnectResult) => {
if (!ConnectResult.isConnected) {
hilog.info(0x0000, 'testTag', 'connect failed');
return;
}
}).catch(() => {
hilog.error(0x0000, 'testTag', "connect failed");
})
abilityConnectionManager.acceptConnect
acceptConnect(sessionId: number, token: string): Promise<void>
Accepts the UIAbility connection after a collaboration session is set up and the session ID is obtained. This API uses a promise to return the result.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| sessionId | number | Yes | ID of the collaboration session. |
| token | string | Yes | Token value passed by the application on device A. |
Return value
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
Example
After createAbilityConnectionSession is called on device A to create a collaboration session and the session ID is obtained, the application on device B can call acceptConnect to accept the connection.
import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
}
onCollaborate(wantParam: Record<string, Object>): AbilityConstant.CollaborateResult {
hilog.info(0x0000, 'testTag', '%{public}s', 'on collaborate');
let param = wantParam["ohos.extra.param.key.supportCollaborateIndex"] as Record<string, Object>
this.onCollab(param);
return 0;
}
onCollab(collabParam: Record<string, Object>) {
const sessionId = this.createSessionFromWant(collabParam);
if (sessionId == -1) {
hilog.info(0x0000, 'testTag', 'Invalid session ID.');
return;
}
const collabToken = collabParam["ohos.dms.collabToken"] as string;
abilityConnectionManager.acceptConnect(sessionId, collabToken).then(() => {
hilog.info(0x0000, 'testTag', 'acceptConnect success');
}).catch(() => {
hilog.error(0x0000, 'testTag', 'failed');
})
}
createSessionFromWant(collabParam: Record<string, Object>): number {
let sessionId = -1;
const peerInfo = collabParam["PeerInfo"] as abilityConnectionManager.PeerInfo;
if (peerInfo == undefined) {
return sessionId;
}
const options = collabParam["ConnectOption"] as abilityConnectionManager.ConnectOptions;
try {
sessionId = abilityConnectionManager.createAbilityConnectionSession("collabTest", this.context, peerInfo, options);
AppStorage.setOrCreate('sessionId', sessionId);
hilog.info(0x0000, 'testTag', 'createSession sessionId is' + sessionId);
} catch (error) {
hilog.error(0x0000, 'testTag', error);
}
return sessionId;
}
}
abilityConnectionManager.disconnect
disconnect(sessionId: number): void
Disconnects the UIAbility connection to end the collaboration session.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| sessionId | number | Yes | ID of the collaboration session. |
Example
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
hilog.info(0x0000, 'testTag', 'disconnectRemoteAbility begin');
let sessionId = 100;
abilityConnectionManager.disconnect(sessionId);
abilityConnectionManager.reject
reject(token: string, reason: string): void;
Rejects a connection request in a cross-device collaboration session. After a connection request sent from the peer application is rejected, a rejection reason is returned.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| token | string | Yes | Token used for application collaboration management. |
| reason | string | Yes | Reason why the connection is rejected. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
Example
import { AbilityConstant, UIAbility, Want} from '@kit.AbilityKit';
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
export default class EntryAbility extends UIAbility {
onCollaborate(wantParam: Record<string, Object>): AbilityConstant.CollaborateResult {
hilog.info(0x0000, 'testTag', '%{public}s', 'on collaborate');
let collabParam = wantParam["ohos.extra.param.key.supportCollaborateIndex"] as Record<string, Object>;
const collabToken = collabParam["ohos.dms.collabToken"] as string;
const reason = "test";
hilog.info(0x0000, 'testTag', 'reject begin');
abilityConnectionManager.reject(collabToken, reason);
return AbilityConstant.CollaborateResult.REJECT;
}
}
abilityConnectionManager.on('connect')
on(type: 'connect', sessionId: number, callback: Callback<EventCallbackInfo>): void
Enables listening for connect events. This API uses an asynchronous callback to return the result.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. This field has a fixed value of connect. This event is triggered when abilityConnectionManager.connect() is called. |
| sessionId | number | Yes | ID of the collaboration session. |
| callback | Callback<EventCallbackInfo> | Yes | Registered callback function. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
Example
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let sessionId = 100;
abilityConnectionManager.on("connect", sessionId,(callbackInfo) => {
hilog.info(0x0000, 'testTag', 'session connect, sessionId is', callbackInfo.sessionId);
});
abilityConnectionManager.off('connect')
off(type: 'connect', sessionId: number, callback?: Callback<EventCallbackInfo>): void
Disables listening for connect events.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. This field has a fixed value of connect. |
| sessionId | number | Yes | ID of the collaboration session. |
| callback | Callback<EventCallbackInfo> | No | Registered callback function. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
Example
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
let sessionId = 100;
abilityConnectionManager.off("connect", sessionId);
abilityConnectionManager.on('disconnect')
on(type: 'disconnect', sessionId: number, callback: Callback<EventCallbackInfo>): void
Enables listening for disconnect events.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. This field has a fixed value of disconnect. This event is triggered when abilityConnectionManager.disconnect() is called. |
| sessionId | number | Yes | ID of the collaboration session. |
| callback | Callback<EventCallbackInfo> | Yes | Registered callback function. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
Example
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let sessionId = 100;
abilityConnectionManager.on("disconnect", sessionId,(callbackInfo) => {
hilog.info(0x0000, 'testTag', 'session disconnect, sessionId is', callbackInfo.sessionId);
});
abilityConnectionManager.off('disconnect')
off(type: 'disconnect', sessionId: number, callback?: Callback<EventCallbackInfo>): void
Disables listening for disconnect events.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. This field has a fixed value of disconnect. |
| sessionId | number | Yes | ID of the collaboration session. |
| callback | Callback<EventCallbackInfo> | No | Registered callback function. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
Example
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let sessionId = 100;
abilityConnectionManager.off("disconnect", sessionId);
abilityConnectionManager.on('receiveMessage')
on(type: 'receiveMessage', sessionId: number, callback: Callback<EventCallbackInfo>): void
Enables listening for receiveMessage events.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. This field has a fixed value of receiveMessage. This event is triggered when abilityConnectionManager.sendMessage() is called. |
| sessionId | number | Yes | ID of the collaboration session. |
| callback | Callback<EventCallbackInfo> | Yes | Registered callback function. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
Example
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let sessionId = 100;
abilityConnectionManager.on("receiveMessage", sessionId,(callbackInfo) => {
hilog.info(0x0000, 'testTag', 'receiveMessage, sessionId is', callbackInfo.sessionId);
});
abilityConnectionManager.off('receiveMessage')
off(type: 'receiveMessage', sessionId: number, callback?: Callback<EventCallbackInfo>): void
Disables listening for receiveMessage events.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. This field has a fixed value of receiveMessage. |
| sessionId | number | Yes | ID of the collaboration session. |
| callback | Callback<EventCallbackInfo> | No | Registered callback function. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
Example
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let sessionId = 100;
abilityConnectionManager.off("receiveMessage", sessionId);
abilityConnectionManager.on('receiveData')
on(type: 'receiveData', sessionId: number, callback: Callback<EventCallbackInfo>): void
Enables listening for receiveData events.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. This field has a fixed value of receiveData. This event is triggered when abilityConnectionManager.sendData() is called. |
| sessionId | number | Yes | ID of the collaboration session. |
| callback | Callback<EventCallbackInfo> | Yes | Registered callback function. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
Example
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let sessionId = 100;
abilityConnectionManager.on("receiveData", sessionId,(callbackInfo) => {
hilog.info(0x0000, 'testTag', 'receiveData, sessionId is', callbackInfo.sessionId);
});
abilityConnectionManager.off('receiveData')
off(type: 'receiveData', sessionId: number, callback?: Callback<EventCallbackInfo>): void
Disables listening for receiveData events.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| type | string | Yes | Event type. This field has a fixed value of receiveData. |
| sessionId | number | Yes | ID of the collaboration session. |
| callback | Callback<EventCallbackInfo> | No | Registered callback function. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
Example
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let sessionId = 100;
abilityConnectionManager.off("receiveData", sessionId);
abilityConnectionManager.sendMessage
sendMessage(sessionId: number, msg: string): Promise<void>
Sends text messages after a collaboration session is set up.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| sessionId | number | Yes | ID of the collaboration session. |
| msg | string | Yes | Text content. The maximum size of the text content is 1 KB. |
Return value
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
Example
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
let sessionId = 100;
abilityConnectionManager.sendMessage(sessionId, "message send success").then(() => {
hilog.info(0x0000, 'testTag', "sendMessage success");
}).catch(() => {
hilog.error(0x0000, 'testTag', "connect failed");
})
abilityConnectionManager.sendData
sendData(sessionId: number, data: ArrayBuffer): Promise<void>
Sends ArrayBuffer byte streams from one device to another after a connection is successfully established.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
Parameters
| Name | Type | Mandatory | Description |
|---|---|---|---|
| sessionId | number | Yes | ID of the collaboration session. |
| data | ArrayBuffer | Yes | Byte stream information. |
Return value
| Type | Description |
|---|---|
| Promise<void> | Promise that returns no value. |
Error codes
For details about the error codes, see Universal Error Codes.
| ID | Error Message |
|---|---|
| 401 | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
Example
import { abilityConnectionManager } from '@kit.DistributedServiceKit';
import { hilog } from '@kit.PerformanceAnalysisKit';
import { util } from '@kit.ArkTS';
let textEncoder = util.TextEncoder.create("utf-8");
const arrayBuffer = textEncoder.encodeInto("data send success");
let sessionId = 100;
abilityConnectionManager.sendData(sessionId, arrayBuffer.buffer).then(() => {
hilog.info(0x0000, 'testTag', "sendMessage success");
}).catch(() => {
hilog.error(0x0000, 'testTag', "sendMessage failed");
})
PeerInfo
Defines the application collaboration information.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
| Name | Type | Read Only | Optional | Description |
|---|---|---|---|---|
| deviceId | string | No | No | Peer device ID. |
| bundleName | string | No | No | Bundle name of the application. |
| moduleName | string | No | No | Module name of the peer application. |
| abilityName | string | No | No | Ability name of the peer application. |
| serviceName | string | No | Yes | Service name for the application. |
ConnectOptions
Connection options for the application.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
| Name | Type | Read Only | Optional | Description |
|---|---|---|---|---|
| needSendData | boolean | No | Yes | Whether to send data. The value true indicates that data needs to be sent, and the value false indicates the opposite. |
| startOptions | StartOptionParams | No | Yes | Application startup options. |
| parameters | Record<string, string> | No | Yes | Additional configuration for the connection. |
ConnectResult
Defines the connection result.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
| Name | Type | Read Only | Optional | Description |
|---|---|---|---|---|
| isConnected | boolean | No | No | Whether the connection is successful. The value true indicates that the connection is successful, and the value false indicates the opposite. |
| errorCode | ConnectErrorCode | No | Yes | Connection error code. |
| reason | string | No | Yes | Connection rejection reason. |
EventCallbackInfo
Defines the event callback information.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
| Name | Type | Read Only | Optional | Description |
|---|---|---|---|---|
| sessionId | number | No | No | Collaboration session ID. |
| reason | DisconnectReason | No | Yes | Disconnection reason. |
| msg | string | No | Yes | Received message. |
| data | ArrayBuffer | No | Yes | Received byte stream. |
CollaborateEventInfo
Collaboration event information.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
| Name | Type | Read Only | Optional | Description |
|---|---|---|---|---|
| eventType | CollaborateEventType | No | No | Collaboration event type. |
| eventMsg | string | No | Yes | Content of a collaboration event. |
ConnectErrorCode
Enumerates connection error codes.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
| Name | Value | Description |
|---|---|---|
| CONNECTED_SESSION_EXISTS | 0 | A session already exists between applications. |
| PEER_APP_REJECTED | 1 | The peer application rejects the collaboration request. |
| LOCAL_WIFI_NOT_OPEN | 2 | Wi-Fi is disabled at the local end. |
| PEER_WIFI_NOT_OPEN | 3 | Wi-Fi is disabled at the peer end. |
| PEER_ABILITY_NO_ONCOLLABORATE | 4 | The onCollaborate callback is not implemented. |
| SYSTEM_INTERNAL_ERROR | 5 | An internal system error occurs. |
StartOptionParams
Enumerates application start options.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
| Name | Value | Description |
|---|---|---|
| START_IN_FOREGROUND | 0 | Start of the peer application in the foreground. |
CollaborateEventType
Enumerates collaboration event types.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
| Name | Value | Description |
|---|---|---|
| SEND_FAILURE | 0 | Task sending failure. |
| COLOR_SPACE_CONVERSION_FAILURE | 1 | Color space conversion failure. |
DisconnectReason
Enumerates the disconnection reasons.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
| Name | Value | Description |
|---|---|---|
| PEER_APP_CLOSE_COLLABORATION | 0 | The peer application proactively disables collaboration. |
| PEER_APP_EXIT | 1 | The peer application exits. |
| NETWORK_DISCONNECTED | 2 | The network is disconnected. |
CollaborationKeys
Enumerates application collaboration key values.
Model restriction: This API can be used only in the stage model.
System capability: SystemCapability.DistributedSched.AppCollaboration
| Name | Value | Description |
|---|---|---|
| PEER_INFO | ohos.collaboration.key.peerInfo | Key value of the peer device information. |
| CONNECT_OPTIONS | ohos.collaboration.key.connectOptions | Key value of the connection option. |
| COLLABORATE_TYPE | ohos.collaboration.key.abilityCollaborateType | Key value of the collaboration type. |
CollaborationValues
Model restriction: This API can be used only in the stage model.
Enumerates application collaboration values.
System capability: SystemCapability.DistributedSched.AppCollaboration
| Name | Value | Description |
|---|---|---|
| ABILITY_COLLABORATION_TYPE_DEFAULT | ohos.collaboration.value.abilityCollab | Default collaboration. |
| ABILITY_COLLABORATION_TYPE_CONNECT_PROXY | ohos.collaboration.value.connectProxy | Collaboration via connection proxy. |