• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1# @ohos.abilityConnectionManager (Cross-Device Connection Management)
2
3The **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](../apis-ability-kit/js-apis-app-ability-uiAbility.md) 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.
4
5> **NOTE**
6>
7> 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.
8
9## Modules to Import
10
11```js
12import { abilityConnectionManager } from '@kit.DistributedServiceKit';
13```
14
15## abilityConnectionManager.createAbilityConnectionSession
16
17createAbilityConnectionSession(serviceName: string, context: Context, peerInfo: PeerInfo , connectOptions: ConnectOptions): number
18
19Creates a collaboration session between applications.
20
21**System capability**: SystemCapability.DistributedSched.AppCollaboration
22
23**Parameters**
24
25| Name      | Type                                     | Mandatory  | Description       |
26| --------- | --------------------------------------- | ---- | --------- |
27| 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.|
28| context | [Context](../apis-ability-kit/js-apis-inner-application-context.md) | Yes| Application context.|
29| peerInfo  | [PeerInfo](#peerinfo)               | Yes   | Collaboration information of the peer end.|
30| connectOptions  | [ConnectOptions](#connectoptions)               | Yes   | Connection options for the application.|
31
32**Return value**
33
34| Type                 | Description              |
35| ------------------- | ---------------- |
36| number | ID of the collaboration session.|
37
38**Error codes**
39
40For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
41
42| ID| Error Message|
43| ------- | -------------------------------- |
44| 201      | Permission denied.|
45| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
46
47**Example**
48
491. On device A, an application calls **createAbilityConnectionSession()** to create a collaboration session and return the session ID.
50
51   ```ts
52   import { abilityConnectionManager, distributedDeviceManager } from '@kit.DistributedServiceKit';
53   import { common } from '@kit.AbilityKit';
54   import { hilog } from '@kit.PerformanceAnalysisKit';
55
56   let dmClass: distributedDeviceManager.DeviceManager;
57
58   function initDmClass(): void {
59     try {
60       dmClass = distributedDeviceManager.createDeviceManager('com.example.remotephotodemo');
61     } catch (err) {
62       hilog.error(0x0000, 'testTag', 'createDeviceManager err: ' + JSON.stringify(err));
63     }
64   }
65
66   function getRemoteDeviceId(): string | undefined {
67     initDmClass();
68     if (typeof dmClass === 'object' && dmClass !== null) {
69       hilog.info(0x0000, 'testTag', 'getRemoteDeviceId begin');
70       let list = dmClass.getAvailableDeviceListSync();
71       if (typeof (list) === 'undefined' || typeof (list.length) === 'undefined') {
72         hilog.info(0x0000, 'testTag', 'getRemoteDeviceId err: list is null');
73         return;
74       }
75       if (list.length === 0) {
76         hilog.info(0x0000, 'testTag', 'getRemoteDeviceId err: list is empty');
77         return;
78       }
79       return list[0].networkId;
80     } else {
81       hilog.info(0x0000, 'testTag', 'getRemoteDeviceId err: dmClass is null');
82       return;
83     }
84   }
85
86   const peerInfo: abilityConnectionManager.PeerInfo = {
87     deviceId: "sinkDeviceId",
88     bundleName: 'com.example.remotephotodemo',
89     moduleName: 'entry',
90     abilityName: 'EntryAbility',
91     serviceName: 'collabTest'
92   };
93   const myRecord: Record<string, string> = {
94     "newKey1": "value1",
95   };
96
97   const options: Record<string, string> = {
98     'ohos.collabrate.key.start.option': 'ohos.collabrate.value.foreground',
99   };
100
101   const connectOptions: abilityConnectionManager.ConnectOptions = {
102     needSendBigData: true,
103     needSendStream: false,
104     needReceiveStream: true,
105     options: options,
106     parameters: myRecord
107   };
108   let context = getContext(this) as common.UIAbilityContext;
109   try {
110     this.sessionId = abilityConnectionManager.createAbilityConnectionSession("collabTest", context, peerInfo, connectOptions);
111     hilog.info(0x0000, 'testTag', 'createSession sessionId is', this.sessionId);
112   } catch (error) {
113     hilog.error(0x0000, 'testTag', error);
114   }
115   ```
116
1172. On device B, **createAbilityConnectionSession** can be called in **onCollaborate**, which is triggered when the application is started.
118
119   ```ts
120   import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
121   import { abilityConnectionManager } from '@kit.DistributedServiceKit';
122   import { hilog } from '@kit.PerformanceAnalysisKit';
123
124   export default class EntryAbility extends UIAbility {
125     onCollaborate(wantParam: Record<string, Object>): AbilityConstant.OnCollaborateResult {
126       hilog.info(0x0000, 'testTag', '%{public}s', 'on collaborate');
127       let param = wantParam["ohos.extra.param.key.supportCollaborateIndex"] as Record<string, Object>
128       this.onCollab(param);
129       return 0;
130     }
131
132     onCollab(collabParam: Record<string, Object>) {
133       const sessionId = this.createSessionFromWant(collabParam);
134       if (sessionId == -1) {
135         hilog.info(0x0000, 'testTag', 'Invalid session ID.');
136         return;
137       }
138     }
139
140     createSessionFromWant(collabParam: Record<string, Object>): number {
141       let sessionId = -1;
142       const peerInfo = collabParam["PeerInfo"] as abilityConnectionManager.PeerInfo;
143       if (peerInfo == undefined) {
144         return sessionId;
145       }
146
147       const options = collabParam["ConnectOptions"] as abilityConnectionManager.ConnectOptions;
148       options.needSendBigData = true;
149       options.needSendStream = true;
150       options.needReceiveStream = false;
151       try {
152         sessionId = abilityConnectionManager.createAbilityConnectionSession("collabTest", this.context, peerInfo, options);
153         AppStorage.setOrCreate('sessionId', sessionId);
154         hilog.info(0x0000, 'testTag', 'createSession sessionId is' + sessionId);
155       } catch (error) {
156         hilog.error(0x0000, 'testTag', error);
157       }
158       return sessionId;
159     }
160   }
161   ```
162
163## abilityConnectionManager.destroyAbilityConnectionSession
164
165destroyAbilityConnectionSession(sessionId:&nbsp;number):&nbsp;void
166
167Destroys a collaboration session between applications.
168
169**System capability**: SystemCapability.DistributedSched.AppCollaboration
170
171**Parameters**
172
173| Name      | Type                                      | Mandatory  | Description      |
174| --------- | ---------------------------------------- | ---- | -------- |
175| sessionId | number  | Yes   | Collaboration session ID.  |
176
177**Example**
178
179  ```ts
180  import { abilityConnectionManager } from '@kit.DistributedServiceKit';
181  import { hilog } from '@kit.PerformanceAnalysisKit';
182
183  hilog.info(0x0000, 'testTag', 'destroyAbilityConnectionSession called');
184  abilityConnectionManager.destroyAbilityConnectionSession(this.sessionId);
185  ```
186
187## abilityConnectionManager.getPeerInfoById
188
189getPeerInfoById(sessionId:&nbsp;number):&nbsp;PeerInfo&nbsp;|&nbsp;undefined
190
191Obtains information about the peer application in the specified session.
192
193**System capability**: SystemCapability.DistributedSched.AppCollaboration
194
195**Parameters**
196
197| Name      | Type                                      | Mandatory  | Description      |
198| --------- | ---------------------------------------- | ---- | -------- |
199| sessionId | string  | Yes   | ID of the collaboration session.  |
200
201**Return value**
202
203| Type                 | Description              |
204| ------------------- | ---------------- |
205| PeerInfo | Information about the peer application.|
206| undefined | Unknown situation.|
207
208**Error codes**
209
210For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
211
212| ID| Error Message|
213| ------- | -------------------------------- |
214| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified. 2. Incorrect parameter types. |
215
216**Example**
217
218  ```ts
219  import { abilityConnectionManager } from '@kit.DistributedServiceKit';
220  import { hilog } from '@kit.PerformanceAnalysisKit';
221
222  hilog.info(0x0000, 'testTag', 'getPeerInfoById called');
223  const peerInfo = abilityConnectionManager.getPeerInfoById(this.sessionId);
224  ```
225
226## abilityConnectionManager.connect
227
228connect(sessionId:&nbsp;number):&nbsp;Promise&lt;ConnectResult&gt;
229
230Sets up a UIAbility connection after a collaboration session is created and the session ID is obtained.
231
232**System capability**: SystemCapability.DistributedSched.AppCollaboration
233
234**Parameters**
235
236| Name      | Type                                     | Mandatory  | Description       |
237| --------- | --------------------------------------- | ---- | --------- |
238| sessionId | number | Yes   | ID of the collaboration session.   |
239
240**Return value**
241
242| Type                 | Description              |
243| ------------------- | ---------------- |
244| Promise&lt;ConnectResult&gt; | Promise used to return the [connection result](#connectresult).|
245
246**Error codes**
247
248For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
249
250| ID| Error Message|
251| ------- | -------------------------------- |
252| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
253
254**Example**
255
256After 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.
257
258  ```ts
259  import { abilityConnectionManager } from '@kit.DistributedServiceKit';
260  import { hilog } from '@kit.PerformanceAnalysisKit';
261
262  abilityConnectionManager.connect(this.sessionId).then((ConnectResult) => {
263    if (!ConnectResult.isConnected) {
264      hilog.info(0x0000, 'testTag', 'connect failed');
265      return;
266    }
267  }).catch(() => {
268    hilog.error(0x0000, 'testTag', "connect failed");
269  })
270  ```
271
272## abilityConnectionManager.acceptConnect
273
274acceptConnect(sessionId:&nbsp;number,&nbsp;token:&nbsp;string):&nbsp;Promise&lt;void&gt;
275
276Accepts the UIAbility connection after a collaboration session is set up and the session ID is obtained.
277
278**System capability**: SystemCapability.DistributedSched.AppCollaboration
279
280**Parameters**
281
282| Name      | Type                                     | Mandatory  | Description   |
283| --------- | --------------------------------------- | ---- | ----- |
284| sessionId | number | Yes   | ID of the collaboration session.   |
285| token | string | Yes   | Token value passed by the application on device A.   |
286
287**Return value**
288
289| Type                 | Description              |
290| ------------------- | ---------------- |
291| Promise&lt;void&gt; |Promise that returns no value.|
292
293**Error codes**
294
295For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
296
297| ID| Error Message|
298| ------- | -------------------------------- |
299| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
300
301**Example**
302
303After **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.
304
305  ```ts
306  import { AbilityConstant, UIAbility, Want } from '@kit.AbilityKit';
307  import { abilityConnectionManager } from '@kit.DistributedServiceKit';
308  import { hilog } from '@kit.PerformanceAnalysisKit';
309
310  export default class EntryAbility extends UIAbility {
311    onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
312      hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
313    }
314
315    onCollaborate(wantParam: Record<string, Object>): AbilityConstant.OnCollaborateResult {
316      hilog.info(0x0000, 'testTag', '%{public}s', 'on collaborate');
317      let param = wantParam["ohos.extra.param.key.supportCollaborateIndex"] as Record<string, Object>
318      this.onCollab(param);
319      return 0;
320    }
321
322    onCollab(collabParam: Record<string, Object>) {
323      const sessionId = this.createSessionFromWant(collabParam);
324      if (sessionId == -1) {
325        hilog.info(0x0000, 'testTag', 'Invalid session ID.');
326        return;
327      }
328      const collabToken = collabParam["ohos.dms.collabToken"] as string;
329      abilityConnectionManager.acceptConnect(sessionId, collabToken).then(() => {
330        hilog.info(0x0000, 'testTag', 'acceptConnect success');
331      }).catch(() => {
332        hilog.error("failed");
333      })
334    }
335
336    createSessionFromWant(collabParam: Record<string, Object>): number {
337      let sessionId = -1;
338      const peerInfo = collabParam["PeerInfo"] as abilityConnectionManager.PeerInfo;
339      if (peerInfo == undefined) {
340        return sessionId;
341      }
342
343      const options = collabParam["ConnectOptions"] as abilityConnectionManager.ConnectOptions;
344      options.needSendBigData = true;
345      options.needSendStream = true;
346      options.needReceiveStream = false;
347      try {
348        sessionId = abilityConnectionManager.createAbilityConnectionSession("collabTest", this.context, peerInfo, options);
349        AppStorage.setOrCreate('sessionId', sessionId);
350        hilog.info(0x0000, 'testTag', 'createSession sessionId is' + sessionId);
351      } catch (error) {
352        hilog.error(0x0000, 'testTag', error);
353      }
354      return sessionId;
355    }
356  }
357  ```
358
359## abilityConnectionManager.disconnect
360
361disconnect(sessionId:&nbsp;number):&nbsp;void
362
363Disconnects the UIAbility connection to end the collaboration session.
364
365**System capability**: SystemCapability.DistributedSched.AppCollaboration
366
367**Parameters**
368
369| Name      | Type                                   | Mandatory  | Description       |
370| --------- | ------------------------------------- | ---- | --------- |
371| sessionId | number | Yes   | ID of the collaboration session.    |
372
373**Example**
374
375  ```ts
376  import { abilityConnectionManager } from '@kit.DistributedServiceKit';
377  import { hilog } from '@kit.PerformanceAnalysisKit';
378
379  hilog.info(0x0000, 'testTag', 'disconnectRemoteAbility begin');
380  if (this.sessionId == -1) {
381    hilog.info(0x0000, 'testTag', 'Invalid session ID.');
382  return;
383  }
384  abilityConnectionManager.disconnect(this.sessionId);
385  ```
386
387## abilityConnectionManager.reject
388
389reject(token:&nbsp;string,&nbsp;reason:&nbsp;string):&nbsp;void;
390
391Rejects a connection requet in multi-device application collaboration. After a connection request sent from the peer application is rejected, a rejection reason is returned.
392
393**System capability**: SystemCapability.DistributedSched.AppCollaboration
394
395**Parameters**
396
397| Name      | Type                                     | Mandatory  | Description   |
398| --------- | --------------------------------------- | ---- | ----- |
399| token | string | Yes   | Token used for application collaboration management.   |
400| reason | string | Yes   | Reason why the connection is rejected.   |
401
402**Error codes**
403
404For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
405
406| ID| Error Message|
407| ------- | -------------------------------- |
408| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
409
410**Example**
411
412  ```ts
413  import { abilityConnectionManager } from '@kit.DistributedServiceKit';
414  import { hilog } from '@kit.PerformanceAnalysisKit';
415
416  hilog.info(0x0000, 'testTag', 'reject begin');
417  const collabToken = collabParam["ohos.dms.collabToken"] as string;
418  const reason = "test";
419  abilityConnectionManager.reject(collabToken, reason);
420  ```
421
422## abilityConnectionManager.on
423
424on(type:&nbsp;'connect'&nbsp;|&nbsp;'disconnect'&nbsp;|&nbsp;'receiveMessage'|&nbsp;'receiveData',&nbsp;sessionId:&nbsp;number,&nbsp;callback:&nbsp;Callback&lt;EventCallbackInfo&gt;):&nbsp;void
425
426Registers the callback listener for the **connect**, **disconnect**, **receiveMessage**, and **receiveData** events.
427
428**System capability**: SystemCapability.DistributedSched.AppCollaboration
429
430**Parameters**
431
432| Name      | Type                                   | Mandatory  | Description   |
433| --------- | ------------------------------------- | ---- | ----- |
434| type | string  | Yes   |   Event type, which can be:<br>\- `connect`: event triggered when `connect()` is called.<br>\- `disconnect`: event is triggered when `disconnect()` is called.<br>\- `receiveMessage`: event triggered when `sendMessage()` is called.<br>\- `receiveData`: event triggered when `sendData()` is called.  |
435| sessionId | number  | Yes   | ID of the collaboration session.   |
436| callback | Callback&lt;[EventCallbackInfo](#eventcallbackinfo)&gt; | Yes   | Registered callback function.   |
437
438**Error codes**
439
440For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
441
442| ID| Error Message|
443| ------- | -------------------------------- |
444| 201      | Permission verification failed. The application does not have the permission required to call the API.|
445| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
446
447**Example**
448
449  ```ts
450  import { abilityConnectionManager } from '@kit.DistributedServiceKit';
451  import { hilog } from '@kit.PerformanceAnalysisKit';
452
453  abilityConnectionManager.on("connect", this.sessionId,(callbackInfo) => {
454    hilog.info(0x0000, 'testTag', 'session connect, sessionId is', callbackInfo.sessionId);
455  });
456
457  abilityConnectionManager.on("disconnect", this.sessionId,(callbackInfo) => {
458    hilog.info(0x0000, 'testTag', 'session disconnect, sessionId is', callbackInfo.sessionId);
459  });
460
461  abilityConnectionManager.on("receiveMessage", this.sessionId,(callbackInfo) => {
462    hilog.info(0x0000, 'testTag', 'session receiveMessage, sessionId is', callbackInfo.sessionId);
463  });
464
465  abilityConnectionManager.on("receiveData", this.sessionId,(callbackInfo) => {
466    hilog.info(0x0000, 'testTag', 'session receiveData, sessionId is', callbackInfo.sessionId);
467  });
468
469  ```
470
471## abilityConnectionManager.off
472
473off(type:&nbsp;'connect'&nbsp;|&nbsp;'disconnect'&nbsp;|&nbsp;'receiveMessage'|&nbsp;'receiveData',&nbsp;sessionId:&nbsp;number,&nbsp;callback?:&nbsp;Callback&lt;EventCallbackInfo&gt;):&nbsp;void
474
475Unregisters the callback listener for the **connect**, **disconnect**, **receiveMessage**, and **receiveData** events.
476
477**System capability**: SystemCapability.DistributedSched.AppCollaboration
478
479**Parameters**
480
481| Name      | Type                                   | Mandatory  | Description   |
482| --------- | ------------------------------------- | ---- | ----- |
483| type | string  | Yes   |   Event type, which can be:<br>\- `connect`: event triggered when `connect()` is called.<br>\- `disconnect`: event is triggered when `disconnect()` is called.<br>\- `receiveMessage`: event triggered when `sendMessage()` is called.<br>\- `receiveData`: event triggered when `sendData()` is called.   |
484| sessionId | number  | Yes   | ID of the collaboration session.   |
485| callback | Callback&lt;[EventCallbackInfo](#eventcallbackinfo)&gt; | No   | Registered callback function.   |
486
487**Error codes**
488
489For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
490
491| ID| Error Message|
492| ------- | -------------------------------- |
493| 201      | Permission verification failed. The application does not have the permission required to call the API.|
494| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
495
496**Example**
497
498  ```ts
499  import { abilityConnectionManager } from '@kit.DistributedServiceKit';
500  import { hilog } from '@kit.PerformanceAnalysisKit';
501
502  abilityConnectionManager.off("connect", this.sessionId);
503  abilityConnectionManager.off("disconnect", this.sessionId);
504  abilityConnectionManager.off("receiveMessage", this.sessionId);
505  abilityConnectionManager.off("receiveData", this.sessionId);
506
507  ```
508
509## abilityConnectionManager.sendMessage
510
511sendMessage(sessionId:&nbsp;number,&nbsp;msg:&nbsp;string):&nbsp;Promise&lt;void&gt;
512
513Sends text messages after a collaboration session is set up.
514
515**System capability**: SystemCapability.DistributedSched.AppCollaboration
516
517**Parameters**
518
519| Name      | Type                                     | Mandatory  | Description   |
520| --------- | --------------------------------------- | ---- | ----- |
521| sessionId | number | Yes   | ID of the collaboration session.|
522| msg | string | Yes   | Text content. The maximum size of the text content is 1 KB.|
523
524**Return value**
525
526| Type                 | Description              |
527| ------------------- | ---------------- |
528| Promise&lt;void&gt; | Promise that returns no value.|
529
530**Error codes**
531
532For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
533
534| ID| Error Message|
535| ------- | -------------------------------- |
536| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types. |
537
538**Example**
539
540  ```ts
541  import { abilityConnectionManager } from '@kit.DistributedServiceKit';
542  import { hilog } from '@kit.PerformanceAnalysisKit';
543
544  abilityConnectionManager.sendMessage(this.sessionId, "message send success").then(() => {
545    hilog.info(0x0000, 'testTag', "sendMessage success");
546  }).catch(() => {
547    hilog.error(0x0000, 'testTag', "connect failed");
548  })
549  ```
550
551## abilityConnectionManager.sendData
552
553sendData(sessionId:&nbsp;number,&nbsp;data:&nbsp;ArrayBuffer):&nbsp;Promise&lt;void&gt;
554
555Sends [ArrayBuffer](../../arkts-utils/arraybuffer-object.md) byte streams from one device to another after a connection is successfully established.
556
557**System capability**: SystemCapability.DistributedSched.AppCollaboration
558
559**Parameters**
560
561| Name      | Type                                     | Mandatory  | Description   |
562| --------- | --------------------------------------- | ---- | ----- |
563| sessionId | number | Yes   | ID of the collaboration session.|
564| data | [ArrayBuffer](../../arkts-utils/arraybuffer-object.md) | Yes   | Byte stream information.|
565
566**Return value**
567
568| Type                 | Description              |
569| ------------------- | ---------------- |
570| Promise&lt;void&gt; | Promise that returns no value.|
571
572**Error codes**
573
574For details about the error codes, see [Universal Error Codes](../errorcode-universal.md).
575
576| ID| Error Message|
577| ------- | -------------------------------- |
578| 201      | Permission verification failed. The application does not have the permission required to call the API.|
579| 202      | Permission verification failed. A non-system application calls a system API.|
580| 401      | Parameter error. Possible causes: 1. Mandatory parameters are left unspecified; 2. Incorrect parameter types; 3. Parameter verification failed. |
581
582**Example**
583
584  ```ts
585  import { abilityConnectionManager } from '@kit.DistributedServiceKit';
586  import { hilog } from '@kit.PerformanceAnalysisKit';
587
588  let textEncoder = util.TextEncoder.create("utf-8");
589  const arrayBuffer  = textEncoder.encodeInto("data send success");
590
591  abilityConnectionManager.sendData(this.sessionId, arrayBuffer.buffer).then(() => {
592    hilog.info(0x0000, 'testTag', "sendMessage success");
593  }).catch(() => {
594    hilog.info(0x0000, 'testTag', "sendMessage failed");
595  })
596  ```
597
598## PeerInfo
599
600Defines the application collaboration information.
601
602**System capability**: SystemCapability.DistributedSched.AppCollaboration
603
604| Name                   | Type      | Readable  | Writable  | Mandatory  | Description                |
605| --------------------- | -------- | ---- | ---- | ---- | ------------------ |
606| deviceId | string | Yes   | No   | Yes   | Peer device ID.    |
607| bundleName        | string | Yes   | No  |  Yes   | Bundle name of the application.|
608| moduleName        | string | Yes   | No  |  Yes   | Module name of the peer application.|
609| abilityName        | string | Yes   | No |  Yes    | Ability name of the peer application.|
610| serviceName        | string | Yes   | No |  No    | Service name for the application.|
611
612## ConnectOptions
613
614Connection options for the application.
615
616**System capability**: SystemCapability.DistributedSched.AppCollaboration
617
618| Name         | Type   | Readable  | Writable  | Mandatory  | Description         |
619| ----------- | ------- | ---- | ---- | ---- | ----------- |
620| needSendData    | boolean  | Yes   | Yes   | No   | Whether to send data. The value **true** indicates that data needs to be sent, and the value **false** indicates the opposite.    |
621| needSendStream    | boolean  | Yes   | Yes   | No   | Whether to send streams. The value **true** indicates that streams need to be sent, and the value **false** indicates the opposite.   |
622| needReceiveStream    | boolean  | Yes   | Yes   | No   | Whether to receive streams. The value **true** indicates that streams need to be received, and the value **false** indicates the opposite.    |
623| startOptions | [StartOptionParams](#startoptionparams) | Yes   | Yes   | No   | Application startup options.|
624| parameters | Record&lt;string, string&gt;  | Yes   | Yes   | No   | Additional configuration for the connection.   |
625
626## ConnectResult
627
628Defines the connection result.
629
630**System capability**: SystemCapability.DistributedSched.AppCollaboration
631
632| Name      | Type  | Readable  | Writable  | Mandatory  | Description     |
633| -------- | ------ | ---- | ---- | ---- | ------- |
634| isConnected | boolean | Yes   | No   | Yes   | Whether the connection is successful. The value **true** indicates that the connection is successful, and the value **false** indicates the opposite.|
635| errorCode | [ConnectErrorCode](#connecterrorcode) | Yes   | No   | No   | Connection error code.|
636| reason | string | Yes   | No   | No   | Connection rejection reason.|
637
638## EventCallbackInfo
639
640Defines the event callback information.
641
642**System capability**: SystemCapability.DistributedSched.AppCollaboration
643
644| Name      | Type   | Readable  | Writable  | Description         |
645| -------- | ------ | ---- | ---- | ----------- |
646| sessionId | number   | Yes   | Yes   |   Collaborative session ID.|
647| reason | [DisconnectReason](#disconnectreason)     | Yes   | No   |   Disconnection reason.|
648| msg | string   | Yes   | No   |   Received message.|
649| data  | ArrayBuffer | Yes   | No   |   Received byte stream.|
650| image  | image.PixelMap | Yes   | No   |   Received image.|
651
652## CollaborateEventInfo
653
654Collaboration event information.
655
656**System capability**: SystemCapability.DistributedSched.AppCollaboration
657
658| Name      | Type  | Readable  | Writable  | Mandatory  | Description     |
659| -------- | ------ | ---- | ---- | ---- | ------- |
660| eventType | [CollaborateEventType](#collaborateeventtype) | Yes   | No   | Yes   | Collaboration event type.|
661| eventMsg | string | Yes   | No   | No   | Collaboration event message.|
662
663## ConnectErrorCode
664
665Enumerates connection error codes.
666
667**System capability**: SystemCapability.DistributedSched.AppCollaboration
668
669| Value| Description|
670| -------- | -------- |
671| CONNECTED_SESSION_EXISTS | A session already exists between applications.|
672| PEER_APP_REJECTED | The peer application rejects the collaboration request.|
673| LOCAL_WIFI_NOT_OPEN  | Wi-Fi is disabled at the local end.|
674| PEER_WIFI_NOT_OPEN  | Wi-Fi is disabled at the peer end.|
675| PEER_ABILITY_NO_ONCOLLABORATE | The **onCollaborate** callback is not implemented.|
676
677## StartOptionParams
678
679Enumerates application start options.
680
681**System capability**: SystemCapability.DistributedSched.AppCollaboration
682
683| Value| Description|
684| -------- | -------- |
685| START_IN_FOREGROUND | Start of the peer application in the foreground.|
686| START_IN_BACKGROUND | Start of the peer application in the background.|
687
688## CollaborateEventType
689
690Enumerates collaboration event types.
691
692**System capability**: SystemCapability.DistributedSched.AppCollaboration
693
694| Value| Description|
695| -------- | -------- |
696| SEND_FAILURE | Task sending failure.|
697| COLOR_SPACE_CONVERSION_FAILURE | Color space conversion failure.|
698
699## DisconnectReason
700
701Enumerates the disconnection reasons.
702
703**System capability**: SystemCapability.DistributedSched.AppCollaboration
704
705| Name|  Value| Description|
706|-------|-------|-------|
707| PEER_APP_CLOSE_COLLABORATION | 0 |The peer application proactively disables collaboration.|
708| PEER_APP_EXIT | 1 |The peer application exits.|
709| NETWORK_DISCONNECTED | 2 |The network is disconnected.|
710
711## CollaborationKeys
712
713Enumerates application collaboration key values.
714
715**System capability**: SystemCapability.DistributedSched.AppCollaboration
716
717| Name               |                  Value            | Description                  |
718| -------------------| ------------------------------- | ---------------------- |
719| PEER_INFO           | ohos.collaboration.key.peerInfo | Key value of the peer device information.|
720| CONNECT_OPTIONS     | ohos.collaboration.key.connectOptions | Key value of the connection option.  |
721| COLLABORATE_TYPE    | ohos.collaboration.key.abilityCollaborateType | Key value of the collaboration type.  |
722
723## CollaborationValues
724
725Enumerates application collaboration values.
726
727**System capability**: SystemCapability.DistributedSched.AppCollaboration
728
729| Name                                     | Value      | Description                  |
730| ----------------------------------------- | -------- | ---------------------- |
731| ABILITY_COLLABORATION_TYPE_DEFAULT | ohos.collaboration.value.abilityCollab | Default collaboration.|
732| ABILITY_COLLABORATION_TYPE_CONNECT_PROXY  | ohos.collaboration.value.connectProxy | Collaboration via connection proxy.  |
733