js-apis-net-sharing.md 36.4 KB
Newer Older
S
shawn_he 已提交
1
# # @ohos.net.sharing (Network Sharing)
G
Gloria 已提交
2

S
shawn_he 已提交
3
The **sharing** module allows you to share your device's Internet connection with other connected devices by means of Wi-Fi hotspot, Bluetooth, and USB sharing. It also allows you to query the network sharing state and shared mobile data volume.
G
Gloria 已提交
4

S
shawn_he 已提交
5 6
> **NOTE**
>
G
Gloria 已提交
7 8 9 10 11 12 13 14
> The initial APIs of this module are supported since API version 9. Newly added APIs will be marked with a superscript to indicate their earliest API version.

## Modules to Import

```js
import sharing from '@ohos.net.sharing'
```

S
shawn_he 已提交
15
## sharing.isSharingSupported<sup>9+</sup>
G
Gloria 已提交
16 17 18 19 20

isSharingSupported(callback: AsyncCallback\<boolean>): void

Checks whether network sharing is supported. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
21 22
**System API**: This is a system API.

G
Gloria 已提交
23 24
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
25
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
26 27 28 29 30 31 32

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\<boolean> | Yes  | Callback used to return the result. The value **true** means that network sharing is supported, and **false** means the opposite.|

S
shawn_he 已提交
33 34 35 36 37 38 39 40 41
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |
| 2202011 | Cannot get network sharing configuration.           |

G
Gloria 已提交
42 43 44 45
**Example**

```js
sharing.isSharingSupported((error, data) => {
S
shawn_he 已提交
46 47
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
G
Gloria 已提交
48 49 50
});
```

S
shawn_he 已提交
51
## sharing.isSharingSupported<sup>9+</sup>
G
Gloria 已提交
52 53 54 55 56

isSharingSupported(): Promise\<boolean>

Checks whether network sharing is supported. This API uses a promise to return the result.

S
shawn_he 已提交
57 58
**System API**: This is a system API.

G
Gloria 已提交
59 60
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
61
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
62 63 64 65 66 67 68

**Return value**

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<boolean> | Promise used to return the result. The value **true** means that network sharing is supported, and **false** means the opposite.|

S
shawn_he 已提交
69 70 71 72 73 74 75 76 77
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |
| 2202011 | Cannot get network sharing configuration.           |

G
Gloria 已提交
78 79 80 81
**Example**

```js
sharing.isSharingSupported().then(data => {
S
shawn_he 已提交
82
  console.log(JSON.stringify(data));
G
Gloria 已提交
83
}).catch(error => {
S
shawn_he 已提交
84
  console.log(JSON.stringify(error));
G
Gloria 已提交
85 86 87
});
```

S
shawn_he 已提交
88
## sharing.isSharing<sup>9+</sup>
G
Gloria 已提交
89 90 91 92 93

isSharing(callback: AsyncCallback\<boolean>): void

Checks whether network sharing is in progress. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
94 95
**System API**: This is a system API.

G
Gloria 已提交
96 97
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
98
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
99 100 101 102 103 104 105

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\<boolean> | Yes  | Callback used to return the result. The value **true** means that network sharing is in progress, and **false** means the opposite.|

S
shawn_he 已提交
106 107 108 109 110
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
S
shawn_he 已提交
111
| 202     | Non-system applications use system APIs.                           |
S
shawn_he 已提交
112 113
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |
S
shawn_he 已提交
114
| 2202011 | Cannot get network sharing configuration.                      |
S
shawn_he 已提交
115

G
Gloria 已提交
116 117 118 119
**Example**

```js
sharing.isSharing((error, data) => {
S
shawn_he 已提交
120 121
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
G
Gloria 已提交
122 123 124
});
```

S
shawn_he 已提交
125
## sharing.isSharing<sup>9+</sup>
G
Gloria 已提交
126 127 128 129 130

isSharing(): Promise\<boolean>

Checks whether network sharing is in progress. This API uses a promise to return the result.

S
shawn_he 已提交
131 132
**System API**: This is a system API.

S
shawn_he 已提交
133 134
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
135
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
136 137 138 139 140 141 142

**Return value**

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<boolean> | Promise used to return the result. The value **true** means that network sharing is in progress, and **false** means the opposite.|

S
shawn_he 已提交
143 144 145 146 147
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
S
shawn_he 已提交
148
| 202     | Non-system applications use system APIs.                           |
S
shawn_he 已提交
149 150
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |
S
shawn_he 已提交
151
| 2202011 | Cannot get network sharing configuration.                      |
S
shawn_he 已提交
152

G
Gloria 已提交
153 154 155 156
**Example**

```js
sharing.isSharing().then(data => {
S
shawn_he 已提交
157
  console.log(JSON.stringify(data));
G
Gloria 已提交
158
}).catch(error => {
S
shawn_he 已提交
159
  console.log(JSON.stringify(error));
G
Gloria 已提交
160 161 162
});
```

S
shawn_he 已提交
163
## sharing.startSharing<sup>9+</sup>
G
Gloria 已提交
164 165 166 167 168

startSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void

Starts network sharing of a specified type. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
169 170
**System API**: This is a system API.

S
shawn_he 已提交
171 172
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
173
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
174 175 176 177 178 179 180 181

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

S
shawn_he 已提交
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 401     | Parameter error.                             |
| 2200001 | Invalid parameter value.                     |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |
| 2202004 | Try to share an unavailable iface.           |
| 2202005 | WiFi sharing failed.                         |
| 2202006 | Bluetooth sharing failed.                    |
| 2202009 | Network share enable forwarding error.       |
| 2202011 | Cannot get network sharing configuration.           |

G
Gloria 已提交
197 198 199 200
**Example**

```js
import SharingIfaceType from '@ohos.net.sharing'
S
shawn_he 已提交
201 202

let SHARING_WIFI = 0;
S
shawn_he 已提交
203
sharing.startSharing(SHARING_WIFI, (error) => {
S
shawn_he 已提交
204
  console.log(JSON.stringify(error));
G
Gloria 已提交
205 206 207
});
```

S
shawn_he 已提交
208
## sharing.startSharing<sup>9+</sup>
G
Gloria 已提交
209 210 211 212 213

startSharing(type: SharingIfaceType): Promise\<void>

Starts network sharing of a specified type. This API uses a promise to return the result.

S
shawn_he 已提交
214 215
**System API**: This is a system API.

G
Gloria 已提交
216 217
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
218
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
219 220 221 222 223 224 225 226 227 228 229 230 231

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|

**Return value**

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<void> | Promise used to return the result.|

S
shawn_he 已提交
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 401     | Parameter error.                             |
| 2200001 | Invalid parameter value.                     |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |
| 2202004 | Try to share an unavailable iface.           |
| 2202005 | WiFi sharing failed.                         |
| 2202006 | Bluetooth sharing failed.                    |
| 2202009 | Network share enable forwarding error.       |
| 2202011 | Cannot get network sharing configuration.           |

G
Gloria 已提交
247 248 249 250
**Example**

```js
import SharingIfaceType from '@ohos.net.sharing'
S
shawn_he 已提交
251 252

let SHARING_WIFI = 0;
S
shawn_he 已提交
253
sharing.startSharing(SHARING_WIFI).then(() => {
S
shawn_he 已提交
254
  console.log("start wifi sharing successful");
G
Gloria 已提交
255
}).catch(error => {
S
shawn_he 已提交
256
  console.log("start wifi sharing failed");
G
Gloria 已提交
257 258 259
});
```

S
shawn_he 已提交
260
## sharing.stopSharing<sup>9+</sup>
G
Gloria 已提交
261 262 263 264 265

stopSharing(type: SharingIfaceType, callback: AsyncCallback\<void>): void

Stops network sharing of a specified type. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
266 267
**System API**: This is a system API.

G
Gloria 已提交
268 269
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
270
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
271 272 273 274 275 276 277 278

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|
| callback | AsyncCallback\<void> | Yes  | Callback used to return the result.|

S
shawn_he 已提交
279 280 281 282 283
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
S
shawn_he 已提交
284
| 202     | Non-system applications use system APIs.                           |
S
shawn_he 已提交
285 286 287 288
| 401     | Parameter error.                             |
| 2200001 | Invalid parameter value.                     |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |
S
shawn_he 已提交
289
| 2202004 | Try to share an unavailable iface.                       |
S
shawn_he 已提交
290 291 292 293
| 2202005 | WiFi sharing failed.                         |
| 2202006 | Bluetooth sharing failed.                    |
| 2202011 | Cannot get network sharing configuration.           |

G
Gloria 已提交
294 295 296 297
**Example**

```js
import SharingIfaceType from '@ohos.net.sharing'
S
shawn_he 已提交
298 299

let SHARING_WIFI = 0;
S
shawn_he 已提交
300
sharing.stopSharing(SHARING_WIFI, (error) => {
S
shawn_he 已提交
301
  console.log(JSON.stringify(error));
G
Gloria 已提交
302 303 304
});
```

S
shawn_he 已提交
305
## sharing.stopSharing<sup>9+</sup>
G
Gloria 已提交
306 307 308 309 310

stopSharing(type: SharingIfaceType): Promise\<void>

Stops network sharing of a specified type. This API uses a promise to return the result.

S
shawn_he 已提交
311 312
**System API**: This is a system API.

G
Gloria 已提交
313 314
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
315
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
316 317 318 319 320 321 322 323 324 325 326 327 328

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|

**Return value**

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<void> | Promise used to return the result.|

S
shawn_he 已提交
329 330 331 332 333
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
S
shawn_he 已提交
334
| 202     | Non-system applications use system APIs.                           |
S
shawn_he 已提交
335 336 337 338
| 401     | Parameter error.                             |
| 2200001 | Invalid parameter value.                     |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |
S
shawn_he 已提交
339
| 2202004 | Try to share an unavailable iface.                       |
S
shawn_he 已提交
340 341 342 343
| 2202005 | WiFi sharing failed.                         |
| 2202006 | Bluetooth sharing failed.                    |
| 2202011 | Cannot get network sharing configuration.           |

G
Gloria 已提交
344 345 346 347
**Example**

```js
import SharingIfaceType from '@ohos.net.sharing'
S
shawn_he 已提交
348 349

let SHARING_WIFI = 0;
S
shawn_he 已提交
350
sharing.stopSharing(SHARING_WIFI).then(() => {
S
shawn_he 已提交
351
  console.log("stop wifi sharing successful");
G
Gloria 已提交
352
}).catch(error => {
S
shawn_he 已提交
353
  console.log("stop wifi sharing failed");
G
Gloria 已提交
354 355 356
});
```

S
shawn_he 已提交
357
## sharing.getStatsRxBytes<sup>9+</sup>
G
Gloria 已提交
358 359 360 361 362

getStatsRxBytes(callback: AsyncCallback\<number>): void

Obtains the volume of mobile data traffic received via network sharing. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
363 364
**System API**: This is a system API.

G
Gloria 已提交
365 366
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
367
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
368 369 370 371 372 373 374

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\<number> | Yes  | Callback used to return the data volume, in KB.|

S
shawn_he 已提交
375 376 377 378 379 380 381 382
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |

G
Gloria 已提交
383 384 385 386
**Example**

```js
sharing.getStatsRxBytes((error, data) => {
S
shawn_he 已提交
387 388
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
G
Gloria 已提交
389 390 391
});
```

S
shawn_he 已提交
392
## sharing.getStatsRxBytes<sup>9+</sup>
G
Gloria 已提交
393 394 395 396 397

getStatsRxBytes(): Promise\<number>

Obtains the volume of mobile data traffic received via network sharing. This API uses a promise to return the result.

S
shawn_he 已提交
398 399
**System API**: This is a system API.

G
Gloria 已提交
400 401
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
402
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
403 404 405 406 407 408 409

**Return value**

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<number> | Promise used to return the data volume, in KB.|

S
shawn_he 已提交
410 411 412 413 414 415 416 417
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |

G
Gloria 已提交
418 419 420 421
**Example**

```js
sharing.getStatsRxBytes().then(data => {
S
shawn_he 已提交
422
  console.log(JSON.stringify(data));
G
Gloria 已提交
423
}).catch(error => {
S
shawn_he 已提交
424
  console.log(JSON.stringify(error));
G
Gloria 已提交
425 426 427
});
```

S
shawn_he 已提交
428
## sharing.getStatsTxBytes<sup>9+</sup>
G
Gloria 已提交
429 430 431 432 433

getStatsTxBytes(callback: AsyncCallback\<number>): void

Obtains the volume of mobile data traffic sent via network sharing. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
434 435
**System API**: This is a system API.

G
Gloria 已提交
436 437
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
438
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
439 440 441 442 443 444 445

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\<number> | Yes  | Callback used to return the data volume, in KB.|

S
shawn_he 已提交
446 447 448 449 450 451 452 453
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |

G
Gloria 已提交
454 455 456 457
**Example**

```js
sharing.getStatsTxBytes((error, data) => {
S
shawn_he 已提交
458 459
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
G
Gloria 已提交
460 461 462
});
```

S
shawn_he 已提交
463
## sharing.getStatsTxBytes<sup>9+</sup>
G
Gloria 已提交
464 465 466 467 468

getStatsTxBytes(): Promise\<number>

Obtains the volume of mobile data traffic sent via network sharing. This API uses a promise to return the result.

S
shawn_he 已提交
469 470
**System API**: This is a system API.

G
Gloria 已提交
471 472
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
473
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
474 475 476 477 478 479 480

**Return value**

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<number> | Promise used to return the data volume, in KB.|

S
shawn_he 已提交
481 482 483 484 485 486 487 488
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |

G
Gloria 已提交
489 490 491 492
**Example**

```js
sharing.getStatsTxBytes().then(data => {
S
shawn_he 已提交
493
  console.log(JSON.stringify(data));
G
Gloria 已提交
494
}).catch(error => {
S
shawn_he 已提交
495
  console.log(JSON.stringify(error));
G
Gloria 已提交
496 497 498
});
```

S
shawn_he 已提交
499
## sharing.getStatsTotalBytes<sup>9+</sup>
G
Gloria 已提交
500 501 502 503 504

getStatsTotalBytes(callback: AsyncCallback\<number>): void

Obtains the volume of mobile data traffic sent and received via network sharing. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
505 506
**System API**: This is a system API.

G
Gloria 已提交
507 508
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
509
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
510 511 512 513 514 515 516

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| callback | AsyncCallback\<number> | Yes  | Callback used to return the data volume, in KB.|

S
shawn_he 已提交
517 518 519 520 521 522 523 524
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |

G
Gloria 已提交
525 526 527 528
**Example**

```js
sharing.getStatsTotalBytes((error, data) => {
S
shawn_he 已提交
529 530
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
G
Gloria 已提交
531 532 533
});
```

S
shawn_he 已提交
534
## sharing.getStatsTotalBytes<sup>9+</sup>
G
Gloria 已提交
535 536 537 538 539

getStatsTotalBytes(): Promise\<number>

Obtains the volume of mobile data traffic sent and received via network sharing. This API uses a promise to return the result.

S
shawn_he 已提交
540 541
**System API**: This is a system API.

G
Gloria 已提交
542 543
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
544
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
545 546 547 548 549 550 551

**Return value**

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<number> | Promise used to return the data volume, in KB.|

S
shawn_he 已提交
552 553 554 555 556 557 558 559
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |

G
Gloria 已提交
560 561 562 563
**Example**

```js
sharing.getStatsTotalBytes().then(data => {
S
shawn_he 已提交
564
  console.log(JSON.stringify(data));
G
Gloria 已提交
565
}).catch(error => {
S
shawn_he 已提交
566
  console.log(JSON.stringify(error));
G
Gloria 已提交
567 568 569
});
```

S
shawn_he 已提交
570
## sharing.getSharingIfaces<sup>9+</sup>
G
Gloria 已提交
571 572 573 574 575

getSharingIfaces(state: SharingIfaceState, callback: AsyncCallback\<Array\<string>>): void

Obtains the names of NICs in the specified network sharing state. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
576 577
**System API**: This is a system API.

G
Gloria 已提交
578 579
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
580
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
581 582 583 584 585

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
S
shawn_he 已提交
586
| state    | [SharingIfaceState](#sharingifacestate) | Yes  | Network sharing state.|
G
Gloria 已提交
587 588
| callback | AsyncCallback\<Array\<string>> | Yes  | Callback used to return an array of NIC names.|

S
shawn_he 已提交
589 590 591 592 593 594 595 596 597 598
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 401     | Parameter error.                             |
| 2200001 | Invalid parameter value.                     |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |

G
Gloria 已提交
599 600 601
**Example**

```js
S
shawn_he 已提交
602
import SharingIfaceState from '@ohos.net.sharing'
S
shawn_he 已提交
603 604

let SHARING_BLUETOOTH = 2;
S
shawn_he 已提交
605
sharing.getSharingIfaces(SHARING_BLUETOOTH, (error, data) => {
S
shawn_he 已提交
606 607
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
G
Gloria 已提交
608 609 610
});
```

S
shawn_he 已提交
611
## sharing.getSharingIfaces<sup>9+</sup>
G
Gloria 已提交
612 613 614 615 616

getSharingIfaces(state: SharingIfaceState): Promise\<Array\<string>>

Obtains the names of NICs in the specified network sharing state. This API uses a promise to return the result.

S
shawn_he 已提交
617 618
**System API**: This is a system API.

G
Gloria 已提交
619 620
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
621
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
622 623 624 625 626

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
S
shawn_he 已提交
627
| state    | [SharingIfaceState](#sharingifacestate) | Yes  | Network sharing state.|
G
Gloria 已提交
628 629 630 631 632 633 634

**Return value**

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<Array\<string>> | Promise used to return an array of NIC names.|

S
shawn_he 已提交
635 636 637 638 639 640 641 642 643 644
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 401     | Parameter error.                             |
| 2200001 | Invalid parameter value.                     |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |

G
Gloria 已提交
645 646 647 648
**Example**

```js
import SharingIfaceState from '@ohos.net.sharing'
S
shawn_he 已提交
649 650

let SHARING_BLUETOOTH = 2;
S
shawn_he 已提交
651
sharing.getSharingIfaces(SHARING_BLUETOOTH).then(data => {
S
shawn_he 已提交
652
  console.log(JSON.stringify(data));
G
Gloria 已提交
653
}).catch(error => {
S
shawn_he 已提交
654
  console.log(JSON.stringify(error));
G
Gloria 已提交
655 656 657
});
```

S
shawn_he 已提交
658
## sharing.getSharingState<sup>9+</sup>
G
Gloria 已提交
659 660 661 662 663

getSharingState(type: SharingIfaceType, callback: AsyncCallback\<SharingIfaceState>): void

Obtains the network sharing state of the specified type. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
664 665
**System API**: This is a system API.

G
Gloria 已提交
666 667
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
668
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
669 670 671 672 673 674 675 676

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|
| callback | AsyncCallback\<[SharingIfaceState](#sharingifacestate)> | Yes  | Callback used to return the network sharing state.|

S
shawn_he 已提交
677 678 679 680 681 682 683 684 685 686
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 401     | Parameter error.                             |
| 2200001 | Invalid parameter value.                     |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |

G
Gloria 已提交
687 688 689
**Example**

```js
S
shawn_he 已提交
690
import SharingIfaceType from '@ohos.net.sharing'
S
shawn_he 已提交
691 692

let SHARING_WIFI = 0;
S
shawn_he 已提交
693
sharing.getSharingState(SHARING_WIFI, (error, data) => {
S
shawn_he 已提交
694 695
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
G
Gloria 已提交
696 697 698
});
```

S
shawn_he 已提交
699
## sharing.getSharingState<sup>9+</sup>
G
Gloria 已提交
700 701 702 703 704

getSharingState(type: SharingIfaceType): Promise\<SharingIfaceState>

Obtains the network sharing state of the specified type. This API uses a promise to return the result.

S
shawn_he 已提交
705 706
**System API**: This is a system API.

G
Gloria 已提交
707 708
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
709
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
710 711 712 713 714 715 716

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|

S
shawn_he 已提交
717 718 719 720 721 722 723 724 725 726
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 401     | Parameter error.                             |
| 2200001 | Invalid parameter value.                     |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |

G
Gloria 已提交
727 728 729 730 731 732 733 734 735 736
**Return value**

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<[SharingIfaceState](#sharingifacestate)> | Promise used to return the network sharing state.|

**Example**

```js
import SharingIfaceType from '@ohos.net.sharing'
S
shawn_he 已提交
737 738

let SHARING_WIFI = 0;
S
shawn_he 已提交
739
sharing.getSharingState(SHARING_WIFI).then(data => {
S
shawn_he 已提交
740
  console.log(JSON.stringify(data));
G
Gloria 已提交
741
}).catch(error => {
S
shawn_he 已提交
742
  console.log(JSON.stringify(error));
G
Gloria 已提交
743 744 745
});
```

S
shawn_he 已提交
746
## sharing.getSharableRegexes<sup>9+</sup>
G
Gloria 已提交
747 748 749 750 751

getSharableRegexes(type: SharingIfaceType, callback: AsyncCallback\<Array\<string>>): void

Obtains regular expressions of NICs of a specified type. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
752 753
**System API**: This is a system API.

G
Gloria 已提交
754 755
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
756
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
757 758 759 760 761 762 763 764

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|
| callback | AsyncCallback\<Array\<string>> | Yes  | Callback used to return an array of regular expressions.|

S
shawn_he 已提交
765 766 767 768 769 770 771 772 773 774
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 401     | Parameter error.                             |
| 2200001 | Invalid parameter value.                     |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |

G
Gloria 已提交
775 776 777
**Example**

```js
S
shawn_he 已提交
778
import SharingIfaceType from '@ohos.net.sharing'
S
shawn_he 已提交
779 780

let SHARING_WIFI = 0;
S
shawn_he 已提交
781
sharing.getSharableRegexes(SHARING_WIFI, (error, data) => {
S
shawn_he 已提交
782 783
  console.log(JSON.stringify(error));
  console.log(JSON.stringify(data));
G
Gloria 已提交
784 785 786
});
```

S
shawn_he 已提交
787
## sharing.getSharableRegexes<sup>9+</sup>
G
Gloria 已提交
788 789 790 791 792

getSharableRegexes(type: SharingIfaceType): Promise\<Array\<string>>

Obtains regular expressions of NICs of a specified type. This API uses a promise to return the result.

S
shawn_he 已提交
793 794
**System API**: This is a system API.

G
Gloria 已提交
795 796
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
797
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
798 799 800 801 802 803 804 805 806 807 808 809 810

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| type | [SharingIfaceType](#sharingifacetype) | Yes  | Sharing type. The value **0** means Wi-Fi hotspot sharing, **1** means USB sharing, and **2** means Bluetooth sharing.|

**Return value**

| Type                             | Description                                 |
| --------------------------------- | ------------------------------------- |
| Promise\<Array\<string>> | Promise used to return an array of regular expressions.|

S
shawn_he 已提交
811 812 813 814 815 816 817 818 819 820
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
| 201     | Permission denied.                           |
| 401     | Parameter error.                             |
| 2200001 | Invalid parameter value.                     |
| 2200002 | Operation failed. Cannot connect to service. |
| 2200003 | System internal error.                       |

G
Gloria 已提交
821 822 823 824
**Example**

```js
import SharingIfaceType from '@ohos.net.sharing'
S
shawn_he 已提交
825 826

let SHARING_WIFI = 0;
S
shawn_he 已提交
827
sharing.getSharableRegexes(SHARING_WIFI).then(data => {
S
shawn_he 已提交
828
  console.log(JSON.stringify(data));
G
Gloria 已提交
829
}).catch(error => {
S
shawn_he 已提交
830
  console.log(JSON.stringify(error));
G
Gloria 已提交
831 832 833
});
```

S
shawn_he 已提交
834
## sharing.on('sharingStateChange')<sup>9+</sup>
G
Gloria 已提交
835 836 837 838 839

on(type: 'sharingStateChange', callback: Callback\<boolean>): void

Subscribes to network sharing state changes. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
840 841
**System API**: This is a system API.

G
Gloria 已提交
842 843
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
844
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
845 846 847 848 849 850

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| type | string | Yes  | Event name.|
S
shawn_he 已提交
851 852 853 854 855 856
| callback | AsyncCallback\<boolean> | Yes  | Callback invoked when the network sharing state changes.|

**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
S
shawn_he 已提交
857
| 202     | Non-system applications use system APIs.                          |
G
Gloria 已提交
858 859 860 861

**Example**

```js
S
shawn_he 已提交
862 863
sharing.on('sharingStateChange', (data) => {
  console.log('on sharingStateChange: ' + JSON.stringify(data));
G
Gloria 已提交
864 865 866
});
```

S
shawn_he 已提交
867
## sharing.off('sharingStateChange')<sup>9+</sup>
G
Gloria 已提交
868 869 870 871 872

off(type: 'sharingStateChange', callback?: Callback\<boolean>): void

Unsubscribes from network sharing state changes. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
873 874
**System API**: This is a system API.

G
Gloria 已提交
875 876
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
877
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
878 879 880 881 882 883

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| type | string | Yes  | Event name.|
S
shawn_he 已提交
884 885 886 887 888 889
| callback | AsyncCallback\<boolean> | No  | Callback invoked when the network sharing state changes.|

**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
S
shawn_he 已提交
890
| 202     | Non-system applications use system APIs.                          |
G
Gloria 已提交
891 892 893 894

**Example**

```js
S
shawn_he 已提交
895
sharing.off('sharingStateChange', (data) => {
S
shawn_he 已提交
896
  console.log(JSON.stringify(data));
G
Gloria 已提交
897 898 899
});
```

S
shawn_he 已提交
900
## sharing.on('interfaceSharingStateChange')<sup>9+</sup>
G
Gloria 已提交
901

S
shawn_he 已提交
902 903
on(type: 'interfaceSharingStateChange', callback: Callback\<{ type: SharingIfaceType, iface: string, state:
SharingIfaceState }>): void
G
Gloria 已提交
904 905 906

Subscribes to network sharing state changes of a specified NIC. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
907 908
**System API**: This is a system API.

G
Gloria 已提交
909 910
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
911
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
912 913 914 915 916 917 918 919

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| type | string | Yes  | Event name.|
| callback | AsyncCallback\<{ type: [SharingIfaceType](#sharingifacetype), iface: string, state: SharingIfaceState(#sharingifacestate) }> | Yes  | Callback invoked when the network sharing state of the specified NIC changes.|

S
shawn_he 已提交
920 921 922 923
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
S
shawn_he 已提交
924
| 202     | Non-system applications use system APIs.                          |
S
shawn_he 已提交
925

G
Gloria 已提交
926 927 928
**Example**

```js
S
shawn_he 已提交
929 930
sharing.on('interfaceSharingStateChange', (data) => {
  console.log('on interfaceSharingStateChange:' + JSON.stringify(data));
G
Gloria 已提交
931 932 933
});
```

S
shawn_he 已提交
934
## sharing.off('interfaceSharingStateChange')<sup>9+</sup>
G
Gloria 已提交
935

S
shawn_he 已提交
936 937
off(type: 'interfaceSharingStateChange', callback?: Callback\<{ type: SharingIfaceType, iface: string, state:
SharingIfaceState }>): void
G
Gloria 已提交
938 939 940

Unsubscribes from network sharing status changes of a specified NIC. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
941 942
**System API**: This is a system API.

G
Gloria 已提交
943 944
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
945
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
946 947 948 949 950

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
S
shawn_he 已提交
951
| type | string | Yes  | Event name.|
S
shawn_he 已提交
952 953 954 955 956 957
| callback | AsyncCallback\<{ type: [SharingIfaceType](#sharingifacetype), iface: string, state: SharingIfaceState(#sharingifacestate) }> | No  | Callback used to return the result.|

**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
S
shawn_he 已提交
958
| 202     | Non-system applications use system APIs.                          |
G
Gloria 已提交
959 960 961 962

**Example**

```js
S
shawn_he 已提交
963
sharing.off('interfaceSharingStateChange', (data) => {
S
shawn_he 已提交
964
  console.log(JSON.stringify(data));
G
Gloria 已提交
965 966 967
});
```

S
shawn_he 已提交
968
## sharing.on('sharingUpstreamChange')<sup>9+</sup>
G
Gloria 已提交
969 970 971 972 973

on(type: 'sharingUpstreamChange', callback: Callback\<NetHandle>): void

Subscribes to upstream network changes. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
974 975
**System API**: This is a system API.

G
Gloria 已提交
976 977
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
978
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
979 980 981 982 983 984 985 986

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| type | string | Yes  | Event name.|
| callback | AsyncCallback\<NetHandle> | Yes  | Callback invoked when the upstream network changes.|

S
shawn_he 已提交
987 988 989 990
**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
S
shawn_he 已提交
991
| 202     | Non-system applications use system APIs.                          |
S
shawn_he 已提交
992

G
Gloria 已提交
993 994 995
**Example**

```js
S
shawn_he 已提交
996 997
sharing.on('sharingUpstreamChange', (data) => {
  console.log('on sharingUpstreamChange:' + JSON.stringify(data));
G
Gloria 已提交
998 999 1000
});
```

S
shawn_he 已提交
1001
## sharing.off('sharingUpstreamChange')<sup>9+</sup>
G
Gloria 已提交
1002 1003 1004 1005 1006

off(type: 'sharingUpstreamChange', callback?: Callback\<NetHandle>): void

Unsubscribes from upstream network changes. This API uses an asynchronous callback to return the result.

S
shawn_he 已提交
1007 1008
**System API**: This is a system API.

G
Gloria 已提交
1009 1010
**Required permissions**: ohos.permission.CONNECTIVITY_INTERNAL

S
shawn_he 已提交
1011
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
1012 1013 1014 1015 1016 1017

**Parameters**

| Name  | Type                                   | Mandatory| Description      |
| -------- | --------------------------------------- | ---- | ---------- |
| type | string | Yes  | Event name.|
S
shawn_he 已提交
1018 1019 1020 1021 1022 1023
| callback | AsyncCallback\<NetHandle> | No  | Callback used for unsubscription from upstream network changes.|

**Error codes**

| ID| Error Message                                     |
| ------- | -------------------------------------------- |
S
shawn_he 已提交
1024
| 202     | Non-system applications use system APIs.                          |
G
Gloria 已提交
1025 1026 1027 1028

**Example**

```js
S
shawn_he 已提交
1029
sharing.off('sharingUpstreamChange', (data) => {
S
shawn_he 已提交
1030
  console.log(JSON.stringify(data));
G
Gloria 已提交
1031 1032 1033
});
```

S
shawn_he 已提交
1034
## SharingIfaceState<sup>9+</sup>
G
Gloria 已提交
1035 1036 1037

Enumerates the network sharing states of an NIC.

S
shawn_he 已提交
1038 1039 1040
**System API**: This is a system API.

**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
1041 1042 1043 1044 1045 1046 1047

| Name                 | Value  | Description                  |
| ------------------------ | ---- | ---------------------- |
| SHARING_NIC_SERVING    | 1 | Network sharing is in progress.|
| SHARING_NIC_CAN_SERVER | 2 | Network sharing is supported.|
| SHARING_NIC_ERROR      | 3 | An error occurred during network sharing.|

S
shawn_he 已提交
1048
## SharingIfaceType<sup>9+</sup>
G
Gloria 已提交
1049

S
shawn_he 已提交
1050
Enumerates the network sharing types of an NIC.
G
Gloria 已提交
1051

S
shawn_he 已提交
1052
**System API**: This is a system API.
G
Gloria 已提交
1053

S
shawn_he 已提交
1054
**System capability**: SystemCapability.Communication.NetManager.NetSharing
G
Gloria 已提交
1055 1056 1057 1058

| Name                 | Value  | Description                  |
| ------------------------ | ---- | ---------------------- |
| SHARING_WIFI       | 0 | Wi-Fi hotspot sharing.|
S
shawn_he 已提交
1059
| SHARING_USB     | 1 | USB sharing.|
G
Gloria 已提交
1060
| SHARING_BLUETOOTH    | 2 | Bluetooth sharing.|