分布式跨设备hap无法弹出选择设备弹窗

首先我通过@ohos.abilityAccessCtrl模块向用户申请使用多设备协同的权限,然后使用@ohos.continuation.continuationManager模块的registerContinuation方法获取token和注册,之后使用startContinuationDeviceManager方法想要拉起设备选择弹窗,但是该接口错误码提示"code":16600001,不知道该如何解决,部分代码如下


import continuationManager from '@ohos.continuation.continuationManager'
import hiTraceChain from '@ohos.hiTraceChain';
import hiTraceMeter from '@ohos.hiTraceMeter';
import hilog from '@ohos.hilog';
import abilityAccessCtrl, { Permissions } from '@ohos.abilityAccessCtrl';
import common from '@ohos.app.ability.common';

const permissions: Array<Permissions> = ['ohos.permission.DISTRIBUTED_DATASYNC'];

@Entry
@Component
struct Index {
  reqPermissionsFromUser(permissions: Array<Permissions>): void {
    let context = getContext(this) as common.UIAbilityContext;
    let atManager = abilityAccessCtrl.createAtManager();
    // requestPermissionsFromUser会判断权限的授权状态来决定是否唤起弹窗
    atManager.requestPermissionsFromUser(context, permissions).then((data) => {
      let grantStatus: Array<number> = data.authResults;
      let length: number = grantStatus.length;
      for (let i = 0; i < length; i++) {
        if (grantStatus[i] === 0) {
          // 用户授权,可以继续访问目标操作
        } else {
          // 用户拒绝授权,提示用户必须授权才能访问当前页面的功能,并引导用户到系统设置中打开相应的权限
          return;
        }
      }
      hilog.error(0x00000, "%s", "MMMMMMMMM success");
    }).catch((err) => {
      console.error(`requestPermissionsFromUser failed, code is ${err.code}, message is ${err.message}`);
    })
  }


  @State message: string = 'Hello World'
  textInputValue: string = ''
  @State progressValue: number = 0
  token: number = -1;
  build() {
    Row() {
      Column() {
        Text(this.message)
          .fontSize(50)
          .fontWeight(FontWeight.Bold)
        Button("获取流转权限")
          .onClick(()=>{
            this.reqPermissionsFromUser(permissions)
          })
          .margin(20)
          .width('100%')
          .height('100')
        Button("注册token")
          .onClick(()=>{
            try {
              continuationManager.registerContinuation((err, data) => {
                if (err.code != 0) {
                  console.error('MMMMMM registerContinuation failed, cause: ' + JSON.stringify(err));
                  return;
                }
                console.info('MMMMMM registerContinuation finished, ' + JSON.stringify(data));
                this.token = data;
              });
            } catch (err) {
              console.error('MMMMMM registerContinuation catch failed, cause: ' + JSON.stringify(err));
            }
          })
          .margin(20)
          .width('100%')
          .height('100')
        Button("选择设备")
          .onClick(()=>{
            let remoteDeviceId: string = ""; // 用于保存用户选择的远端设备信息,后续使用其完成跨端迁移或多端协同操作
            try {
              // 参数token为注册token
              continuationManager.on("deviceSelected", this.token, (continuationResults) => {
                console.info('registerDeviceSelectedCallback len: ' + continuationResults.length);
                if (continuationResults.length <= 0) {
                  console.info('no selected device');
                  return;
                }
                remoteDeviceId = continuationResults[0].id; // 将选择的第一个远端设备deviceId赋值给remoteDeviceId变量
                for (let i = 0; i < continuationResults.length; ++i) {
                  console.log("remoteDeviceId " + continuationResults[0].id)
                }

                // 将remoteDeviceId参数传给want
                let want = {
                  deviceId: remoteDeviceId,
                  bundleName: 'ohos.samples.continuationmanager',
                  abilityName: 'MainAbility'
                };
                globalThis.abilityContext.startAbility(want).then((data) => {
                  console.info('StartRemoteAbility finished, ' + JSON.stringify(data));
                }).catch((err) => {
                  console.error('StartRemoteAbility failed, cause: ' + JSON.stringify(err));
                });
              });
            } catch (err) {
              console.error('on failed, cause: ' + JSON.stringify(err));
            }
            if (canIUse("SystemCapability.Ability.DistributedAbilityManager")) {
              console.log("MMMMM 该设备支持SystemCapability.Ability.DistributedAbilityManager");
            } else {
              console.log("MMMMM 该设备不支持SystemCapability.Ability.DistributedAbilityManager");
            }


            try {
              console.error("MMMMMM token: " + this.token)
              continuationManager.startContinuationDeviceManager(this.token, (err, data) => {
                if (err.code != 0) {
                  console.error('MMMMMM startContinuationDeviceManager failed, cause: ' + JSON.stringify(err));
                  return;
                }
                console.info('MMMMMM startContinuationDeviceManager finished, ' + JSON.stringify(data));
              });
            } catch (err) {
              console.error('MMMMMM startContinuationDeviceManager failed, cause: ' + JSON.stringify(err));
            }

            // continuationManager.unregisterContinuation(this.token, (err, data) =>{})
          })
          .margin(20)
          .width('100%')
          .height('100')
      }
      .width('100%')
    }
    .height('100%')
  }
}


申请的权限如下:

其中abilities中的"test"申请的系统权限是"SystemCapability.Ability.DistributedAbilityManager"

{
  "module": {
    "name": "entry",
    "type": "entry",
    "description": "$string:module_desc",
    "mainElement": "EntryAbility",
    "deviceTypes": [
      "phone",
      "tablet"
    ],
    "deliveryWithInstall": true,
    "installationFree": false,
    "pages": "$profile:main_pages",
    "abilities": [
      {
        "name": "EntryAbility",
        "srcEntry": "./ets/entryability/EntryAbility.ts",
        "description": "$string:EntryAbility_desc",
        "icon": "$media:icon",
        "label": "$string:EntryAbility_label",
        "startWindowIcon": "$media:icon",
        "startWindowBackground": "$color:start_window_background",
        "exported": true,
        "skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "action.system.home"
            ]
          }
        ]
      },
      {
        "name": "test",
        "srcEntry": "./ets/entryability/EntryAbility.ts",
        "description": "$string:test",
        "icon": "$media:icon",
        "label": "$string:EntryAbility_label",
        "startWindowIcon": "$media:icon",
        "startWindowBackground": "$color:start_window_background",
        "exported": true,
        "skills": [
          {
            "entities": [
              "entity.system.home"
            ],
            "actions": [
              "action.system.home"
            ]
          }
        ]
      }
    ],
    "requestPermissions": [
      {
        "name": "ohos.permission.DISTRIBUTED_DATASYNC",
        "reason": "$string:reason",
        "usedScene": {
          "abilities": [
            "FormAbility"
          ],
          "when": "inuse"
        }
      }
    ]
  }
}


我只是想简单测试并实现下跨设备相关的一些简单应用,请大佬指点一下,非常感谢。

分布式
流转
跨设备
continuationMana
2024-01-25 19:56:59
浏览
已于2024-1-25 19:57:46修改
收藏 0
回答 1
待解决
回答 1
按赞同
/
按时间
mb5b9c687e5ded2

同时麻烦问一下最新的Dev如何使用openHarmony的接口,我再新建项目时没有看到使用harmonyos还是openHarmony的接口,还是说这2个没区别呢,


分享
微博
QQ
微信
回复
2024-01-26 11:26:27
相关问题
Hi3516进行分布式流转无法获取设备?
5131浏览 • 2回复 待解决
Harmonyos 分布式迁移失败
1055浏览 • 0回复 待解决
分布式是怎么启用的?
3234浏览 • 1回复 待解决
Redis 模板的分布式
1099浏览 • 1回复 待解决
PolarDB分布式架构有什么特点?
1569浏览 • 1回复 待解决
求解Redis 分布式锁问题?
1904浏览 • 1回复 待解决
设备启动FA传参问题
6014浏览 • 1回复 待解决
鸿蒙系统是如何实现分布式的?
11061浏览 • 2回复 待解决
分布式数据管理没有了吗?
1072浏览 • 1回复 待解决
spark 分布式写数据到oracle
357浏览 • 1回复 待解决
请问如何实现异地分布式组网?
5480浏览 • 1回复 待解决
求教大神,分布式软总线的流程问题
8820浏览 • 1回复 已解决
Api 9 Stage 模型分布式如何实现?
1105浏览 • 1回复 待解决
分布式如何读写图库图片或者视频?
2758浏览 • 1回复 待解决
分布式软总线能否支持全协议
6702浏览 • 1回复 已解决