1# @ohos.distributedsched.linkEnhance (Enhanced Connection) 2<!--Kit: Distributed Service Kit--> 3<!--Subsystem: DistributedSched--> 4<!--Owner: @wangJE--> 5<!--Designer: @lee_jet520--> 6<!--Tester: @Ytt-test--> 7<!--Adviser: @w_Machine_cc--> 8The **linkEnhance** module delivers highly efficient Bluetooth connectivity and data transmission capabilities, significantly enhancing the cross-device connection stability. By employing a multi-channel merging algorithm, it not only increases the number of available cross-device connections but also strengthens cross-device data transmission capabilities, thereby improving the overall user experience. 9 10> **NOTE** 11> 12> The initial APIs of this module are supported since API version 20. Newly added APIs will be marked with a superscript to indicate their earliest API version. 13 14## Modules to Import 15 16```js 17import { linkEnhance } from '@kit.DistributedServiceKit'; 18``` 19 20## linkEnhance.createServer 21 22createServer(name: string): Server 23 24Creates a **Server** object. After **start()** is called, the device can be connected to other devices as a server. 25 26**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 27 28**System capability**: SystemCapability.DistributedSched.AppCollaboration 29 30**Parameters** 31 32| Name | Type | Mandatory | Description | 33| --------- | ---------------------------------------- | ---- | -------- | 34| name | string | Yes | **Server** object name. The value is a string of up to 255 bytes. It cannot be empty. | 35 36**Returns** 37 38| Type | Description | 39| ------------------- | ---------------- | 40| [Server](#server) | **Server** object created.| 41 42**Error codes** 43 44For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 45 46| ID| Error Message| 47| ------- | -------------------------------- | 48| 201 | Permission denied.| 49| 32390203 | Duplicate server name.| 50| 32390206 | Invalid parameter. | 51 52**Example** 53 54```ts 55import { linkEnhance } from '@kit.DistributedServiceKit'; 56import { BusinessError } from '@kit.BasicServicesKit'; 57import { hilog } from '@kit.PerformanceAnalysisKit'; 58const TAG = "testDemo"; 59 60try { 61 let name:string = "demo"; 62 hilog.info(0x0000, TAG, 'start sever name = ' + name); 63 // Construct a Server object using the specified name. 64 let server: linkEnhance.Server = linkEnhance.createServer(name); 65} catch (err) { 66 hilog.info(0x0000, TAG, 'start sever errCode: ' + (err as BusinessError).code + ', errMessage: ' + 67 (err as BusinessError).message); 68} 69``` 70 71## linkEnhance.createConnection 72 73createConnection(deviceId: string, name: string): Connection 74 75Creates a **Connection** object on the device that functions as the client. The device can then initiate connection requests to the device that functions as the server. 76 77**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 78 79**System capability**: SystemCapability.DistributedSched.AppCollaboration 80 81**Parameters** 82 83| Name | Type | Mandatory | Description | 84| --------- | --------------------------------------- | ---- | --------- | 85| deviceId | string | Yes | Device ID of the peer device, that is, the BLE MAC address of the peer device. For details about how to obtain the BLE MAC address, see [BLE Advertising and Scanning](../../connectivity/bluetooth/ble-development-guide.md).| 86| name | string | Yes | Server name of the device to be connected. The value is a string of up to 255 bytes. It cannot be empty.| 87 88**Returns** 89 90| Type | Description | 91| ------------------- | ---------------- | 92| [Connection](#connection) | **Connection** object created.| 93 94**Error codes** 95 96For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 97 98| ID| Error Message| 99| ------- | -------------------------------- | 100| 201 | Permission denied.| 101| 32390206 | Invalid parameter. | 102 103**Example** 104 105 On the device that functions as the client, call the **createConnection()** to create a **Connection** object. 106 107```ts 108import { linkEnhance } from '@kit.DistributedServiceKit'; 109import { BusinessError } from '@kit.BasicServicesKit'; 110import { hilog } from '@kit.PerformanceAnalysisKit'; 111const TAG = "testDemo"; 112 113try { 114 let peerDeviceId: string = "00:11:22:33:44:55"; 115 hilog.info(0x0000, TAG, 'connection sever deviceId = ' + peerDeviceId); 116 let connection: linkEnhance.Connection = linkEnhance.createConnection(peerDeviceId, "demo"); 117} catch (err) { 118 hilog.info(0x0000, TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + 119 (err as BusinessError).message); 120} 121``` 122## Server 123 124Represents a **Server** object, which provides methods for starting, stopping, and closing the server, and registering or unregistering event callbacks. 125 126The following APIs are used on the server. 127 128### start() 129 130start(): void 131 132Starts a server so that it can be connected by the client. A maximum of 10 servers are supported. 133 134**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 135 136**System capability**: SystemCapability.DistributedSched.AppCollaboration 137 138**Error codes** 139 140For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 141 142| ID| Error Message| 143| ------- | -------------------------------- | 144| 201 | Permission denied.| 145| 32390202 | The number of servers exceeds the limit. | 146| 32390300 | Internal error. | 147 148**Example** 149 150```ts 151import { linkEnhance } from '@kit.DistributedServiceKit'; 152import { BusinessError } from '@kit.BasicServicesKit'; 153import { hilog } from '@kit.PerformanceAnalysisKit'; 154const TAG = "testDemo"; 155 156try { 157 let name: string = "demo"; 158 hilog.info(0x0000, TAG, 'start sever name = ' + name); 159 let server: linkEnhance.Server = linkEnhance.createServer(name); 160 server.start(); 161} catch (err) { 162 hilog.error(0x0000, TAG, 'start sever errCode: ' + (err as BusinessError).code + ', errMessage: ' + 163 (err as BusinessError).message); 164} 165``` 166### stop() 167 168stop(): void 169 170Stops the server. After the server is stopped, you can call `start` to start it again. 171 172**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 173 174**System capability**: SystemCapability.DistributedSched.AppCollaboration 175 176**Error codes** 177 178For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 179 180| ID| Error Message| 181| ------- | -------------------------------- | 182| 201 | Permission denied.| 183 184**Example** 185 186```ts 187import { linkEnhance } from '@kit.DistributedServiceKit'; 188import { BusinessError } from '@kit.BasicServicesKit'; 189import { hilog } from '@kit.PerformanceAnalysisKit'; 190const TAG = "testDemo"; 191 192try { 193 let name: string = "demo"; 194 hilog.info(0x0000, TAG, 'start sever name = ' + name); 195 let server: linkEnhance.Server = linkEnhance.createServer(name); 196 server.start(); 197 server.stop(); 198} catch (err) { 199 hilog.error(0x0000, TAG, 'start sever errCode: ' + (err as BusinessError).code + ', errMessage: ' + 200 (err as BusinessError).message); 201} 202``` 203 204### close() 205 206close(): void 207 208Destroys the **Server** object to release related resources. To interact with the peer device again, create a new **Server** object. 209 210**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 211 212**System capability**: SystemCapability.DistributedSched.AppCollaboration 213 214**Error codes** 215 216For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 217 218| ID| Error Message| 219| ------- | -------------------------------- | 220| 201 | Permission denied.| 221 222**Example** 223 224```ts 225import { linkEnhance } from '@kit.DistributedServiceKit'; 226import { BusinessError } from '@kit.BasicServicesKit'; 227import { hilog } from '@kit.PerformanceAnalysisKit'; 228const TAG = "testDemo"; 229 230try { 231 let name: string = "demo"; 232 hilog.info(0x0000, TAG, 'start sever name = ' + name); 233 let server: linkEnhance.Server = linkEnhance.createServer(name); 234 server.start(); 235 server.close(); 236} catch (err) { 237 hilog.error(0x0000, TAG, 'start sever errCode: ' + (err as BusinessError).code + ', errMessage: ' + 238 (err as BusinessError).message); 239} 240``` 241### on('connectionAccepted') 242 243on(type: 'connectionAccepted', callback: Callback<Connection>): void 244 245Registers a callback listener for **connectionAccepted** events. This API uses an asynchronous callback to return the result. 246 247**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 248 249**System capability**: SystemCapability.DistributedSched.AppCollaboration 250 251| Name | Type | Mandatory | Description | 252| --------- | ------------------------------------- | ---- | ----- | 253| type | string | Yes | Event type, which is **connectionAccepted**. This event is triggered when a connection from the peer end is received. | 254| callback | Callback<[Connection](#connection)> | Yes | Registered callback, which is used to return the [Connection](#connection) object.| 255 256**Error codes** 257 258For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 259 260| ID| Error Message| 261| ------- | -------------------------------- | 262| 201 | Permission denied.| 263| 32390206 | Parameter invalid. | 264 265**Example** 266 267```ts 268import { linkEnhance } from '@kit.DistributedServiceKit'; 269import { BusinessError } from '@kit.BasicServicesKit'; 270import { hilog } from '@kit.PerformanceAnalysisKit'; 271const TAG = "testDemo"; 272 273try { 274 let name: string = "demo"; 275 hilog.info(0x0000, TAG, 'start sever name = ' + name); 276 // Construct a Server object using the specified name. 277 let server: linkEnhance.Server = linkEnhance.createServer(name); 278 279 // Subscribe to connectionAccepted events. 280 server.on('connectionAccepted', (connection: linkEnhance.Connection): void => { 281 hilog.info(0x0000, TAG, 'serverOnCallback = ' + JSON.stringify(connection)); 282 }); 283 // Start the server. 284 server.start(); 285} catch (err) { 286 hilog.info(0x0000, TAG, 'start sever errCode: ' + (err as BusinessError).code + ', errMessage: ' + 287 (err as BusinessError).message); 288} 289``` 290### off('connectionAccepted') 291 292off(type: 'connectionAccepted', callback?: Callback<Connection>): void 293 294Unregisters the callback listener for **connectionAccepted** events. This API uses an asynchronous callback to return the result. 295 296**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 297 298**System capability**: SystemCapability.DistributedSched.AppCollaboration 299 300| Name | Type | Mandatory | Description | 301| --------- | ------------------------------------- | ---- | ----- | 302| type | string | Yes | Event type, which is **connectionAccepted**. This event is triggered when a connection from the peer end is received. | 303| callback | Callback<[Connection](#connection)> | No | Registered callback, which is used to return the [Connection](#connection) object.| 304 305**Error codes** 306 307For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 308 309| ID| Error Message| 310| ------- | -------------------------------- | 311| 201 | Permission denied.| 312| 32390206 | Parameter invalid. | 313 314**Example** 315 316```ts 317import { linkEnhance } from '@kit.DistributedServiceKit'; 318import { BusinessError } from '@kit.BasicServicesKit'; 319import { hilog } from '@kit.PerformanceAnalysisKit'; 320const TAG = "testDemo"; 321 322try { 323 let name: string = "demo"; 324 hilog.info(0x0000, TAG, 'start sever name = ' + name); 325 // Construct a Server object using the specified name. 326 let server: linkEnhance.Server = linkEnhance.createServer(name); 327 server.on('connectionAccepted', (connection: linkEnhance.Connection): void => { 328 hilog.info(0x0000, TAG, 'accpet new connection'); 329 }); 330 // Unsubscribe from connectionAccepted events. 331 server.off('connectionAccepted', (connection: linkEnhance.Connection): void => { 332 hilog.info(0x0000, TAG, 'accpet new connection'); 333 }); 334} catch (err) { 335 hilog.error(0x0000, TAG, 'start sever errCode: ' + (err as BusinessError).code + ', errMessage: ' + 336 (err as BusinessError).message); 337} 338``` 339 340### on('serverStopped') 341 342on(type: 'serverStopped', callback: Callback<number>): void 343 344Registers a callback listener for **serverStopped** events. This API uses an asynchronous callback to return the result. 345 346**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 347 348**System capability**: SystemCapability.DistributedSched.AppCollaboration 349 350| Name | Type | Mandatory | Description | 351| --------- | ------------------------------------- | ---- | ----- | 352| type | string | Yes | Event type, which is **serverStopped**. This event is triggered when the server is stopped abnormally. | 353| callback | Callback<number> | Yes | Registered callback, where **number** indicates the returned error code.| 354 355**Error codes** 356 357For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 358 359| ID| Error Message| 360| ------- | -------------------------------- | 361| 201 | Permission denied.| 362| 32390206 | Parameter invalid. | 363 364**Example** 365 366```ts 367import { linkEnhance } from '@kit.DistributedServiceKit'; 368import { BusinessError } from '@kit.BasicServicesKit'; 369import { hilog } from '@kit.PerformanceAnalysisKit'; 370const TAG = "testDemo"; 371 372try { 373 let name: string = "demo"; 374 hilog.info(0x0000, TAG, 'start sever name = ' + name); 375 // Construct a Server object using the specified name. 376 let server: linkEnhance.Server = linkEnhance.createServer(name); 377 378 // Unsubscribe from serverStopped events. 379 server.on('serverStopped', (reason: number): void => { 380 hilog.info(0x0000, TAG, 'serverStopped, reason= ' + reason); 381 }); 382 // Start the server. 383 server.start(); 384} catch (err) { 385 hilog.error(0x0000, TAG, 'start sever errCode: ' + (err as BusinessError).code + ', errMessage: ' + 386 (err as BusinessError).message); 387} 388``` 389 390### off('serverStopped') 391 392off(type: 'serverStopped', callback?: Callback<number>): void 393 394Unregisters the callback listener for **serverStopped** events. This API uses an asynchronous callback to return the result. 395 396**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 397 398**System capability**: SystemCapability.DistributedSched.AppCollaboration 399 400| Name | Type | Mandatory | Description | 401| --------- | ------------------------------------- | ---- | ----- | 402| type | string | Yes | Event type, which is **serverStopped**. This event is triggered when the server is stopped abnormally. | 403| callback | Callback<number> | No | Registered callback, where **number** indicates the returned error code.| 404 405**Error codes** 406 407For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 408 409| ID| Error Message| 410| ------- | -------------------------------- | 411| 201 | Permission denied.| 412| 32390206 | Parameter invalid. | 413 414**Example** 415 416```ts 417import { linkEnhance } from '@kit.DistributedServiceKit'; 418import { BusinessError } from '@kit.BasicServicesKit'; 419import { hilog } from '@kit.PerformanceAnalysisKit'; 420const TAG = "testDemo"; 421 422try { 423 let name: string = "demo"; 424 hilog.info(0x0000, TAG, 'start sever name = ' + name); 425 // Construct a Server object using the specified name. 426 let server: linkEnhance.Server = linkEnhance.createServer(name); 427 server.on('serverStopped', (reason: number): void => { 428 hilog.info(0x0000, TAG, 'serverStopped, reason= ' + reason); 429 }); 430 // Unsubscribe from serverStopped events. 431 server.off('serverStopped', (reason: number): void => { 432 hilog.info(0x0000, TAG, 'serverStopped, reason= ' + reason); 433 }); 434} catch (err) { 435 hilog.info(0x0000, TAG, 'start sever errCode: ' + (err as BusinessError).code + ', errMessage: ' + 436 (err as BusinessError).message); 437} 438``` 439## ConnectResult 440 441Represents the connection result, which is returned after the client calls **connect()**. 442 443**System capability**: SystemCapability.DistributedSched.AppCollaboration 444 445| **Name** | Type |Read-Only | Optional | Description | 446| ----------------- | ------ | ---- | ---- | ------------------ | 447| deviceId | string | No |No | ID of the peer device. If the connection is successful, the device ID of the peer device is returned. If the connection fails, an empty string is returned. | 448| success | boolean | No |No | Connection result. The value **true** indicates that the connection is successful, and the value **false** indicates the opposite.| 449| reason | number | No |No | Number indicating the result code. If the connection is successful, **0** is returned. If the connection fails, an error code is returned:<br>- 32390200: The client connection times out.<br>- 32390201: The server service is not started.<br>- 32390300: Internal error.<br>For details about the error codes, see [Link Enhancement Error Codes](errorcode_linkEnhance.md).| 450 451## Connection 452 453Represents a **Connection** object, which provides methods for connecting to and disconnecting from a peer device, obtaining the device's ID, sending data, and registering or unregistering event callbacks. 454 455### connect() 456 457connect(): void 458 459Connects to the server on the client. A maximum number of 10 connections are supported. 460 461**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 462 463**System capability**: SystemCapability.DistributedSched.AppCollaboration 464 465 466**Error codes** 467 468For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 469 470| ID| Error Message| 471| ------- | -------------------------------- | 472| 201 | Permission denied.| 473| 32390204 | The number of connection exceeds the limit. | 474| 32390300 | Internal error. | 475 476**Example** 477 478After creating a **Connection** object, the application on the client device calls **connect()** to connect to the target device (that is, the server). 479 480```ts 481import { linkEnhance } from '@kit.DistributedServiceKit'; 482import { BusinessError } from '@kit.BasicServicesKit'; 483import { hilog } from '@kit.PerformanceAnalysisKit'; 484const TAG = "testDemo"; 485 486try { 487 let peerDeviceId: string = "00:11:22:33:44:55"; 488 hilog.info(0x0000, TAG, 'connection sever deviceId = ' + peerDeviceId); 489 let connection: linkEnhance.Connection = linkEnhance.createConnection(peerDeviceId, "demo"); 490 // Subscribe to connectResult events. 491 connection.on('connectResult', (result: linkEnhance.ConnectResult): void => { 492 hilog.info(0x0000, TAG, 'clientConnectResultCallback result = ' + result.success); 493 }); 494 // Initiate a connection. 495 connection.connect(); 496} catch (err) { 497 hilog.error(0x0000, TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + 498 (err as BusinessError).message); 499} 500``` 501 502### disconnect() 503 504disconnect(): void 505 506Disconnects from the peer device. The created **Connection** object remains valid after this API is called. You can call **connect()** to reconnect to the peer device if necessary. 507 508**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 509 510**System capability**: SystemCapability.DistributedSched.AppCollaboration 511 512 513**Error codes** 514 515For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 516 517| ID| Error Message| 518| ------- | -------------------------------- | 519| 201 | Permission denied.| 520 521**Example** 522 523```ts 524import { linkEnhance } from '@kit.DistributedServiceKit'; 525import { BusinessError } from '@kit.BasicServicesKit'; 526import { hilog } from '@kit.PerformanceAnalysisKit'; 527const TAG = "testDemo"; 528 529try { 530 let peerDeviceId: string = "00:11:22:33:44:55"; 531 hilog.info(0x0000, TAG, 'connection sever deviceId = ' + peerDeviceId); 532 let connection: linkEnhance.Connection = linkEnhance.createConnection(peerDeviceId, "demo"); 533 connection.on('connectResult', (result: linkEnhance.ConnectResult): void => { 534 hilog.info(0x0000, TAG, 'clientConnectResultCallback result = ' + result.success); 535 if (result.success) { 536 connection.disconnect(); 537 } 538 }); 539 connection.connect(); 540} catch (err) { 541 hilog.error(0x0000, TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + 542 (err as BusinessError).message); 543} 544``` 545 546### close() 547 548close(): void 549 550Destroys the **Connection** object to release resources. If the device needs to interact with the peer device again, create a **Connection** object again and call `connect()` to initiate a connection. 551 552**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 553 554**System capability**: SystemCapability.DistributedSched.AppCollaboration 555 556**Error codes** 557 558For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 559 560| ID| Error Message| 561| ------- | -------------------------------- | 562| 201 | Permission denied.| 563 564 565**Example** 566 567```ts 568import { linkEnhance } from '@kit.DistributedServiceKit'; 569import { BusinessError } from '@kit.BasicServicesKit'; 570import { hilog } from '@kit.PerformanceAnalysisKit'; 571const TAG = "testDemo"; 572 573try { 574 let peerDeviceId: string = "00:11:22:33:44:55"; 575 hilog.info(0x0000, TAG, 'connection sever deviceId = ' + peerDeviceId); 576 let connection: linkEnhance.Connection = linkEnhance.createConnection(peerDeviceId, "demo"); 577 connection.on('connectResult', (result: linkEnhance.ConnectResult): void => { 578 hilog.info(0x0000, TAG, 'clientConnectResultCallback result = ' + result.success); 579 if (result.success) { 580 connection.close(); 581 } 582 }); 583 connection.connect(); 584} catch (err) { 585 hilog.error(0x0000, TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + 586 (err as BusinessError).message); 587} 588``` 589### getPeerDeviceId() 590 591getPeerDeviceId(): string 592 593Obtains the device ID of the peer device. This API is called when the connection is established successfully either by initiating a connection or accepting an incoming connection. 594 595**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 596 597**System capability**: SystemCapability.DistributedSched.AppCollaboration 598 599 600**Returns** 601 602| Type | Description | 603| ------------------- | ---------------- | 604| string | Device ID of the peer device, that is, the BLE MAC address of the peer device. An empty string is returned if no device ID is obtained.| 605 606 607**Error codes** 608 609For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 610 611| ID| Error Message| 612| ------- | -------------------------------- | 613| 201 | Permission denied.| 614 615**Example** 616 617```ts 618import { linkEnhance } from '@kit.DistributedServiceKit'; 619import { BusinessError } from '@kit.BasicServicesKit'; 620import { hilog } from '@kit.PerformanceAnalysisKit'; 621const TAG = "testDemo"; 622 623try { 624 let peerDeviceId: string = "00:11:22:33:44:55"; 625 hilog.info(0x0000, TAG, 'connection sever deviceId = ' + peerDeviceId); 626 let connection: linkEnhance.Connection = linkEnhance.createConnection(peerDeviceId, "demo"); 627 connection.getPeerDeviceId(); 628 hilog.info(0x0000, TAG, "peerDeviceId=%{public}s" + connection.getPeerDeviceId()); 629} catch (err) { 630 hilog.error(0x0000, TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + 631 (err as BusinessError).message); 632} 633``` 634 635### sendData() 636 637sendData(data: ArrayBuffer): void 638 639Sends data to the server after a connection is established successfully. When the server receives the connection callback, it can also send data to the client. 640 641**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 642 643**System capability**: SystemCapability.DistributedSched.AppCollaboration 644 645**Parameters** 646 647| Name | Type | Mandatory | Description | 648| --------- | --------------------------------------- | ---- | ----- | 649| data | [ArrayBuffer](../../arkts-utils/arraybuffer-object.md) | Yes | Data to send. The maximum length is 1024 bytes.| 650 651**Error codes** 652 653For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 654 655| ID| Error Message| 656| ------- | -------------------------------- | 657| 201 | Permission denied.| 658| 32390205 | Connection is not ready. | 659| 32390206 | Invalid parameter. | 660| 32390300 | Internal error. | 661 662**Example** 663 664```ts 665import { linkEnhance } from '@kit.DistributedServiceKit'; 666import { BusinessError } from '@kit.BasicServicesKit'; 667import { hilog } from '@kit.PerformanceAnalysisKit'; 668const TAG = "testDemo"; 669 670try { 671 let peerDeviceId: string = "00:11:22:33:44:55"; 672 hilog.info(0x0000, TAG, 'connection sever deviceId = ' + peerDeviceId); 673 let connection: linkEnhance.Connection = linkEnhance.createConnection(peerDeviceId, "demo"); 674 connection.on('connectResult', (result: linkEnhance.ConnectResult): void => { 675 hilog.info(0x0000, TAG, 'clientConnectResultCallback result = ' + result.success); 676 if (result.success) { 677 let len = 1; 678 let arraybuffer = new ArrayBuffer(len); // Create the data to send. 679 connection.sendData(arraybuffer); 680 hilog.info(0x0000, TAG, "sendData data connection peerDeviceId=%{public}s" + connection.getPeerDeviceId()); 681 connection.disconnect(); 682 } 683 }); 684 connection.connect(); 685} catch (err) { 686 hilog.error(0x0000, TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + 687 (err as BusinessError).message); 688} 689``` 690 691### on('connectResult') 692 693on(type: 'connectResult', callback: Callback<ConnectResult>): void 694 695Registers a listener for **connectResult** events. This API uses an asynchronous callback to return the result. 696 697**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 698 699**System capability**: SystemCapability.DistributedSched.AppCollaboration 700 701**Parameters** 702 703| Name | Type | Mandatory | Description | 704| --------- | ------------------------------------- | ---- | ----- | 705| type | string | Yes | Event type, which is **connectResult**. This event is triggered when `connect()` is called. | 706| callback | Callback<[ConnectResult](#connectresult)> | Yes | Registered callback. | 707 708**Error codes** 709 710For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 711 712| ID| Error Message| 713| ------- | -------------------------------- | 714| 201 | Permission denied.| 715| 32390206 | Invalid parameter.| 716 717**Example** 718 719```ts 720import { linkEnhance } from '@kit.DistributedServiceKit'; 721import { BusinessError } from '@kit.BasicServicesKit'; 722import { hilog } from '@kit.PerformanceAnalysisKit'; 723const TAG = "testDemo"; 724 725try { 726 let peerDeviceId: string = "00:11:22:33:44:55"; 727 hilog.info(0x0000, TAG, 'connection sever deviceId = ' + peerDeviceId); 728 let connection: linkEnhance.Connection = linkEnhance.createConnection(peerDeviceId, "demo"); 729 // Subscribe to connectResult events. 730 connection.on('connectResult', (result: linkEnhance.ConnectResult): void => { 731 hilog.info(0x0000, TAG, 'clientConnectResultCallback result = ' + result.success); 732 }); 733 734 // Initiate a connection. 735 connection.connect(); 736} catch (err) { 737 hilog.error(0x0000, TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + 738 (err as BusinessError).message); 739} 740``` 741 742### off('connectResult') 743 744off(type: 'connectResult', callback?: Callback<ConnectResult>): void 745 746Unregisters the listener for **connectResult** events. 747 748**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 749 750**System capability**: SystemCapability.DistributedSched.AppCollaboration 751 752**Parameters** 753 754| Name | Type | Mandatory | Description | 755| --------- | ------------------------------------- | ---- | ----- | 756| type | string | Yes | Event type, which is **connectResult**. This event is triggered when `connect()` is called. | 757| callback | Callback<[ConnectResult](#connectresult)> | No | Registered callback. | 758 759**Error codes** 760 761For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 762 763| ID| Error Message| 764| ------- | -------------------------------- | 765| 201 | Permission denied.| 766| 32390206 | Invalid parameter. | 767 768**Example** 769 770```ts 771import { linkEnhance } from '@kit.DistributedServiceKit'; 772import { BusinessError } from '@kit.BasicServicesKit'; 773import { hilog } from '@kit.PerformanceAnalysisKit'; 774const TAG = "testDemo"; 775 776try { 777 let peerDeviceId: string = "00:11:22:33:44:55"; 778 hilog.info(0x0000, TAG, 'connection sever deviceId = ' + peerDeviceId); 779 let connection: linkEnhance.Connection = linkEnhance.createConnection(peerDeviceId, "demo"); 780 connection.on('connectResult', (result: linkEnhance.ConnectResult): void => { 781 hilog.info(0x0000, TAG, 'clientConnectResultCallback result = ' + result.success); 782 }); 783 // Unsubscribe from connectResult events. 784 connection.off('connectResult', (result: linkEnhance.ConnectResult): void => { 785 hilog.info(0x0000, TAG, 'clientConnectResultCallback result = ' + result.success); 786 }); 787} catch (err) { 788 hilog.error(0x0000, TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + 789 (err as BusinessError).message); 790} 791``` 792 793### on('disconnected') 794 795on(type: 'disconnected', callback: Callback<number>): void 796 797Registers a listener for **disconnected** events. This API uses an asynchronous callback to return the result. 798 799**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 800 801**System capability**: SystemCapability.DistributedSched.AppCollaboration 802 803**Parameters** 804 805| Name | Type | Mandatory | Description | 806| --------- | ------------------------------------- | ---- | ----- | 807| type | string | Yes | Event type, which is **disconnected**. This event is triggered when the connection is passively terminated or encounters an exception. | 808| callback | Callback<number> | Yes | Registered callback, where **number** indicates the returned error code. | 809 810**Error codes** 811 812For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 813 814| ID| Error Message| 815| ------- | -------------------------------- | 816| 201 | Permission denied.| 817| 32390206 | Invalid parameter.| 818 819**Example** 820 821```ts 822import { linkEnhance } from '@kit.DistributedServiceKit'; 823import { BusinessError } from '@kit.BasicServicesKit'; 824import { hilog } from '@kit.PerformanceAnalysisKit'; 825const TAG = "testDemo"; 826 827try { 828 let peerDeviceId: string = "00:11:22:33:44:55"; 829 hilog.info(0x0000, TAG, 'connection sever deviceId = ' + peerDeviceId); 830 let connection: linkEnhance.Connection = linkEnhance.createConnection(peerDeviceId, "demo"); 831 // Subscribe to disconnected events. 832 connection.on('disconnected', (number: number)=> { 833 hilog.info(0x0000, TAG, 'connection disconnected reason = ' + number); 834 }); 835} catch (err) { 836 hilog.info(0x0000, TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + 837 (err as BusinessError).message); 838} 839``` 840 841### off('disconnected') 842 843off(type: 'disconnected', callback?: Callback<number>): void 844 845Unregisters the listener for **disconnected** events. This API uses an asynchronous callback to return the result. 846 847**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 848 849**System capability**: SystemCapability.DistributedSched.AppCollaboration 850 851**Parameters** 852 853| Name | Type | Mandatory | Description | 854| --------- | ------------------------------------- | ---- | ----- | 855| type | string | Yes | Event type, which is **disconnected**. This event is triggered when the connection is passively terminated or encounters an exception. | 856| callback | Callback<number> | No | Registered callback, where **number** indicates the returned error code. | 857 858**Error codes** 859 860For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 861 862| ID| Error Message| 863| ------- | -------------------------------- | 864| 201 | Permission denied.| 865| 32390206 | Invalid parameter. | 866 867**Example** 868 869```ts 870import { linkEnhance } from '@kit.DistributedServiceKit'; 871import { BusinessError } from '@kit.BasicServicesKit'; 872import { hilog } from '@kit.PerformanceAnalysisKit'; 873const TAG = "testDemo"; 874 875try { 876 let peerDeviceId: string = "00:11:22:33:44:55"; 877 hilog.info(0x0000, TAG, 'connection sever deviceId = ' + peerDeviceId); 878 let connection: linkEnhance.Connection = linkEnhance.createConnection(peerDeviceId, "demo"); 879 connection.on('disconnected', (number: number)=> { 880 hilog.info(0x0000, TAG, 'connection disconnected reason = ' + number); 881 }); 882 // Unsubscribe from disconnected events. 883 connection.off('disconnected', (number: number)=> { 884 hilog.info(0x0000, TAG, 'connection disconnected reason = ' + number); 885 }); 886} catch (err) { 887 hilog.error(0x0000, TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + 888 (err as BusinessError).message); 889} 890``` 891 892### on('dataReceived') 893 894on(type: 'dataReceived', callback: Callback<ArrayBuffer>): void 895 896Registers a listener for the **dataReceived** events. This API uses an asynchronous callback to return the result. 897 898**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 899 900**System capability**: SystemCapability.DistributedSched.AppCollaboration 901 902**Parameters** 903 904| Name | Type | Mandatory | Description | 905| --------- | ------------------------------------- | ---- | ----- | 906| type | string | Yes | Event type, which is **dataReceived**. This event is triggered when data is received. | 907| callback | Callback<[ArrayBuffer](../../arkts-utils/arraybuffer-object.md)> | Yes | Registered callback.| 908 909**Error codes** 910 911For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 912 913| ID| Error Message| 914| ------- | -------------------------------- | 915| 201 | Permission denied.| 916| 32390206 | Invalid parameter. | 917 918**Example** 919 920```ts 921import { linkEnhance } from '@kit.DistributedServiceKit'; 922import { BusinessError } from '@kit.BasicServicesKit'; 923import { hilog } from '@kit.PerformanceAnalysisKit'; 924const TAG = "testDemo"; 925 926try { 927 let peerDeviceId: string = "00:11:22:33:44:55"; 928 hilog.info(0x0000, TAG, 'connection sever deviceId = ' + peerDeviceId); 929 let connection: linkEnhance.Connection = linkEnhance.createConnection(peerDeviceId, "demo"); 930 connection.connect(); 931 connection.on('dataReceived', (data: ArrayBuffer)=> { 932 hilog.info(0x0000, TAG, 'recv dataLen = ' + data.byteLength); 933 }); 934} catch (err) { 935 hilog.error(0x0000, TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + 936 (err as BusinessError).message); 937} 938``` 939### off('dataReceived') 940 941off(type: 'dataReceived', callback?: Callback<ArrayBuffer>): void 942 943Unregisters the listener for **dataReceived** events. 944 945**Required permissions**: ohos.permission.DISTRIBUTED_DATASYNC 946 947**System capability**: SystemCapability.DistributedSched.AppCollaboration 948 949**Parameters** 950 951| Name | Type | Mandatory | Description | 952| --------- | ------------------------------------- | ---- | ----- | 953| type | string | Yes | Event type, which is **dataReceived**. This event is triggered when data is received. | 954| callback | Callback<[ArrayBuffer](../../arkts-utils/arraybuffer-object.md)> | No | Registered callback.| 955 956**Error codes** 957 958For details about the error codes, see [Enhanced Connection Error Codes](errorcode_linkEnhance.md). 959 960| ID| Error Message| 961| ------- | -------------------------------- | 962| 201 | Permission denied.| 963| 32390206 | Invalid parameter. | 964 965**Example** 966 967```ts 968import { linkEnhance } from '@kit.DistributedServiceKit'; 969import { BusinessError } from '@kit.BasicServicesKit'; 970import { hilog } from '@kit.PerformanceAnalysisKit'; 971const TAG = "testDemo"; 972 973try { 974 let peerDeviceId: string = "00:11:22:33:44:55"; 975 hilog.info(0x0000, TAG, 'connection sever deviceId = ' + peerDeviceId); 976 let connection: linkEnhance.Connection = linkEnhance.createConnection(peerDeviceId, "demo"); 977 connection.on('dataReceived', (data: ArrayBuffer)=> { 978 hilog.info(0x0000, TAG, 'recv dataLen = ' + data.byteLength); 979 }); 980 connection.off('dataReceived', (data: ArrayBuffer)=> { 981 hilog.info(0x0000, TAG, 'recv dataLen = ' + data.byteLength); 982 }); 983} catch (err) { 984 hilog.error(0x0000, TAG, 'errCode: ' + (err as BusinessError).code + ', errMessage: ' + 985 (err as BusinessError).message); 986} 987``` 988