js-apis-continuation-continuationManager.md 15.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
# continuationManager


> ![icon-note.gif](public_sys-resources/icon-note.gif) **说明:**
> 本模块首批接口从API version 8开始支持。后续版本的新增接口,采用上角标单独标记接口的起始版本。

continuationManager模块提供了流转/协同入口管理服务能力,包括连接/取消流转管理服务,注册/解注册设备连接变化监听,拉起互联面板,更新连接状态。

## 导入模块

```js
import continuationManager from '@ohos.continuation.continuationManager'
```

## continuationManager.register

register(callback: AsyncCallback\<number>): void;

注册流转管理服务,并获取对应的注册token,无过滤条件,使用AsyncCallback方式作为异步方法。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | callback | AsyncCallback\<number> | 是 | AsyncCallback形式返回流转管理服务连接后生成的token。 |

**示例:**

  ```js
  let token = -1;
  continuationManager.register((err, data) => {
    if (err.code != 0) {
      console.error('register failed, cause: ' + JSON.stringify(err));
      return;
    }
    console.info('register finished, ' + JSON.stringify(data));
    token = data;
  });
  ```

## continuationManager.register

register(options: ContinuationExtraParams, callback: AsyncCallback\<number>): void;

连接流转管理服务,并获取对应的注册token,使用AsyncCallback方式作为异步方法。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | options | [ContinuationExtraParams](js-apis-continuation-continuationExtraParams.md) | 是 | 过滤可选择设备列表的额外参数。 |
  | callback | AsyncCallback\<number> | 是 | AsyncCallback形式返回流转管理服务连接后生成的token。 |

**示例:**

  ```js
  let token = -1;
  let continuationExtraParams = {
    deviceType: ["00E"]
  };
  continuationManager.register(continuationExtraParams, (err, data) => {
    if (err.code != 0) {
      console.error('register failed, cause: ' + JSON.stringify(err));
      return;
    }
    console.info('register finished, ' + JSON.stringify(data));
    token = data;
  });
  ```

## continuationManager.register

register(options?: ContinuationExtraParams): Promise\<number>;

连接流转管理服务,并获取对应的注册token,使用Promise方式作为异步方法。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | options | [ContinuationExtraParams](js-apis-continuation-continuationExtraParams.md) | 否 | 过滤可选择设备列表的额外参数,该参数可缺省|

**示例:**

  ```js
  let continuationExtraParams = {
    deviceType: ["00E"]
  };
  continuationManager.register(continuationExtraParams)
    .then((data) => {
      console.info('register finished, ' + JSON.stringify(data));
      token = data;
    })
    .catch((err) => {
      console.error('register failed, cause: ' + JSON.stringify(err));
    });
  ```

## continuationManager.on("deviceConnect")<sup>(deprecated)</sup>
> 从API Version 9开始不再维护,建议使用[on](#continuationmanagerondeviceconnect)替代。

on(type: "deviceConnect", callback: Callback\<ContinuationResult>): void;

异步方法,监听设备连接状态,使用Callback形式返回连接的设备信息。

## continuationManager.on("deviceDisconnect")<sup>(deprecated)</sup>
> 从API Version 9开始不再维护,建议使用[on](#continuationmanagerondevicedisconnect)替代。

on(type: "deviceDisconnect", callback: Callback\<string>): void;

异步方法,监听设备断开状态,使用Callback形式返回断开的设备信息。

## continuationManager.off("deviceConnect")<sup>(deprecated)</sup>
> 从API Version 9开始不再维护,建议使用[off](#continuationmanageroffdeviceconnect)替代。

off(type: "deviceConnect", callback?: Callback\<ContinuationResult>): void;

异步方法,取消监听设备连接状态,使用Callback形式返回连接的设备信息。

## continuationManager.off("deviceDisconnect")<sup>(deprecated)</sup>
> 从API Version 9开始不再维护,建议使用[off](#continuationmanageroffdevicedisconnect)替代。

off(type: "deviceDisconnect", callback?: Callback\<string>): void;

异步方法,取消监听设备断开状态,使用Callback形式返回连接的设备信息。

## continuationManager.on("deviceConnect")

on(type: "deviceConnect", token: number, callback: Callback\<Array\<ContinuationResult>>): void;

异步方法,监听设备连接状态,使用Callback形式返回连接的设备信息。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | type | string | 是 | 监听的事件类型,固定值"deviceConnect"。 |
  | token | number | 是 | 注册后的token。 |
  | callback | Callback\<Array\<[ContinuationResult](js-apis-continuation-continuationResult.md)>> | 是 | 当用户从互联面板中选择设备时调用,返回设备ID、设备类型和设备名称供开发者使用。 |

**示例:**

  ```js
  continuationManager.on("deviceConnect", token, (data) => {
    console.info('onDeviceConnect len: ' + data.length);
    for (let i = 0; i < data.length; i++) {
      console.info('onDeviceConnect deviceId: ' + JSON.stringify(data[i].id));
      console.info('onDeviceConnect deviceType: ' + JSON.stringify(data[i].type));
      console.info('onDeviceConnect deviceName: ' + JSON.stringify(data[i].name));
    }
  });
  ```

## continuationManager.on("deviceDisconnect")

on(type: "deviceDisconnect", token: number, callback: Callback\<Array\<string>>): void;

异步方法,监听设备断开状态,使用Callback形式返回断开的设备信息。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | type | string | 是 | 监听的事件类型,固定值"deviceDisconnect"。 |
  | token | number | 是 | 注册后的token。 |
  | callback | Callback\<Array\<string>> | 是 | 当用户从互联面板中断开设备时调用,返回设备ID供开发者使用。 |

**示例:**

  ```js
  continuationManager.on("deviceDisconnect", token, (data) => {
    console.info('onDeviceDisconnect len: ' + data.length);
    for (let i = 0; i < data.length; i++) {
      console.info('onDeviceDisconnect deviceId: ' + JSON.stringify(data[i]));
    }
    console.info('onDeviceDisconnect finished.');
  });
  ```

## continuationManager.off("deviceConnect")

off(type: "deviceConnect", token: number): void;

取消监听设备连接状态。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | type | string | 是 | 取消监听的事件类型,固定值"deviceConnect"。 |
  | token | number | 是 | 注册后的token。 |

**示例:**

  ```js
  continuationManager.off("deviceConnect", token);
  ```

## continuationManager.off("deviceDisconnect")

off(type: "deviceDisconnect", token: number): void;

取消监听设备断开状态。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | type | string | 是 | 取消监听的事件类型,固定值"deviceDisconnect"。 |
  | token | number | 是 | 注册后的token。 |

**示例:**

  ```js
  continuationManager.off("deviceDisconnect", token);
  ```

## continuationManager.startDeviceManager

startDeviceManager(token: number, callback: AsyncCallback\<void>): void;

拉起互联面板,可显示组网内可选择设备列表信息,无过滤条件,使用AsyncCallback方式作为异步方法。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | token | number | 是 | 注册后的token。 |
  | callback | AsyncCallback\<void> | 是 | AsyncCallback形式返回接口调用结果。 |

**示例:**

  ```js
  continuationManager.startDeviceManager(token, (err, data) => {
    if (err.code != 0) {
      console.error('startDeviceManager failed, cause: ' + JSON.stringify(err));
      return;
    }
    console.info('startDeviceManager finished, ' + JSON.stringify(data));
  });
  ```

## continuationManager.startDeviceManager

startDeviceManager(token: number, options: ContinuationExtraParams, callback: AsyncCallback\<void>): void;

拉起互联面板,可显示组网内可选择设备列表信息,使用AsyncCallback方式作为异步方法。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | token | number | 是 | 注册后的token。 |
  | options | [ContinuationExtraParams](js-apis-continuation-continuationExtraParams.md) | 是 | 过滤可选择设备列表的额外参数。 |
  | callback | AsyncCallback\<void> | 是 | AsyncCallback形式返回接口调用结果。 |

**示例:**

  ```js
  let continuationExtraParams = {
    deviceType: ["00E"]
  };
  continuationManager.startDeviceManager(token, continuationExtraParams, (err, data) => {
    if (err.code != 0) {
      console.error('startDeviceManager failed, cause: ' + JSON.stringify(err));
      return;
    }
    console.info('startDeviceManager finished, ' + JSON.stringify(data));
  });
  ```

## continuationManager.startDeviceManager

startDeviceManager(token: number, options?: ContinuationExtraParams): Promise\<void>;

拉起互联面板,可显示组网内可选择设备列表信息,使用Promise方式作为异步方法。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | token | number | 是 | 注册后的token。 |
  | options | [ContinuationExtraParams](js-apis-continuation-continuationExtraParams.md) | 否 | 过滤可选择设备列表的额外参数,该参数可缺省|

**示例:**

  ```js
  let continuationExtraParams = {
    deviceType: ["00E"]
  };
  continuationManager.startDeviceManager(token, continuationExtraParams)
    .then((data) => {
      console.info('startDeviceManager finished, ' + JSON.stringify(data));
    })
    .catch((err) => {
      console.error('startDeviceManager failed, cause: ' + JSON.stringify(err));
    });
  ```

## continuationManager.updateConnectStatus

updateConnectStatus(token: number, deviceId: string, status: DeviceConnectState, callback: AsyncCallback\<void>): void;

通知互联面板,更新当前的连接状态,使用AsyncCallback方式作为异步方法。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | token | number | 是 | 注册后的token。 |
  | deviceId | string | 是 | 设备ID。 |
  | status | [DeviceConnectState](#deviceconnectstate) | 是 | 设备连接状态。 |
  | callback | AsyncCallback\<void> | 是 | AsyncCallback形式返回接口调用结果。 |

**示例:**

  ```js
  let deviceId: string = "test deviceId";
  continuationManager.updateConnectStatus(token, deviceId, continuationManager.DeviceConnectState.CONNECTED, (err, data) => {
    if (err.code != 0) {
      console.error('updateConnectStatus failed, cause: ' + JSON.stringify(err));
      return;
    }
    console.info('updateConnectStatus finished, ' + JSON.stringify(data));
  });
  ```

## continuationManager.updateConnectStatus

updateConnectStatus(token: number, deviceId: string, status: DeviceConnectState): Promise\<void>;

通知互联面板,更新当前的连接状态,使用Promise方式作为异步方法。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | token | number | 是 | 注册后的token。 |
  | deviceId | string | 是 | 设备ID。 |
  | status | [DeviceConnectState](#deviceconnectstate) | 是 | 设备连接状态。 |

**示例:**

  ```js
  let deviceId: string = "test deviceId";
  continuationManager.updateConnectStatus(token, deviceId, continuationManager.DeviceConnectState.CONNECTED)
    .then((data) => {
      console.info('updateConnectStatus finished, ' + JSON.stringify(data));
    })
    .catch((err) => {
      console.error('updateConnectStatus failed, cause: ' + JSON.stringify(err));
    });
  ```

## continuationManager.unregister

unregister(token: number, callback: AsyncCallback\<void>): void;

解注册流转管理服务,传入注册时获取的token进行解注册,使用AsyncCallback方式作为异步方法。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | token | number | 是 | 注册后的token。 |
  | callback | AsyncCallback\<void> | 是 | AsyncCallback形式返回接口调用结果。 |

**示例:**

  ```js
  continuationManager.unregister(token, (err, data) => {
    if (err.code != 0) {
      console.error('unregister failed, cause: ' + JSON.stringify(err));
      return;
    }
    console.info('unregister finished, ' + JSON.stringify(data));
  });
  ```

## continuationManager.unregister

unregister(token: number): Promise\<void>;

解注册流转管理服务,传入注册时获取的token进行解注册,使用Promise方式作为异步方法。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 必填 | 说明 |
  | -------- | -------- | -------- | -------- |
  | token | number | 是 | 注册后的token。 |

**示例:**

  ```js
  continuationManager.unregister(token)
    .then((data) => {
      console.info('unregister finished, ' + JSON.stringify(data));
    })
    .catch((err) => {
      console.error('unregister failed, cause: ' + JSON.stringify(err));
    });
  ```

## DeviceConnectState

设备连接状态。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 值 | 说明 |
  | -------- | -------- | -------- | -------- |
  | IDLE | number | 0 | 设备连接初始状态。 |
  | CONNECTING | number | 1 | 设备连接中状态。 |
  | CONNECTED | number | 2 | 设备已连接状态。 |
  | DISCONNECTING | number | 3 | 设备断开连接状态。 |

## ContinuationMode

互联面板连接模式。

**系统能力**:SystemCapability.Ability.DistributedAbilityManager

**参数:**

  | 参数名 | 类型 | 值 | 说明 |
  | -------- | -------- | -------- | -------- |
  | COLLABORATION_SINGLE | number | 0 | 互联面板单选模式。 |
  | COLLABORATION_MULTIPLE | number | 1 | 互联面板多选模式。 |