HarmonyOS鸿蒙Next中公共事件: 订阅其他设备的公共事件无效

HarmonyOS鸿蒙Next中公共事件: 订阅其他设备的公共事件无效 两台设备: 手机与平板

查找在线设备,订阅其它设备事件

结果:

收到自己设备的事件,而不是其它设备的事件


更多关于HarmonyOS鸿蒙Next中公共事件: 订阅其他设备的公共事件无效的实战教程也可以访问 https://www.itying.com/category-93-b0.html

9 回复

楼主 你的问题解决了嘛? 我也是一样,只能接收到自己的,前提条件也都满足的。。

更多关于HarmonyOS鸿蒙Next中公共事件: 订阅其他设备的公共事件无效的实战系列教程也可以访问 https://www.itying.com/category-93-b0.html


api还没支持到位, 目前使用其它方式解决这类问题的
Service Ability建立通信连接,发送数据过去,本机进行通知

开发者你好,

需要同时满足下面条件。

1、打开设置中多设备协同开关。

2、登录同一个华为账号。

3、连接同一Wifi。

以上条件都满足了,测试PA迁移是没问题的, 不知道你们是否有测试成功了,还是我的写法有问题。

最好能出一份相关功能的demo,以便校正。

开发者你好,麻烦请提供一下发布事件的代码。

scheduleRemoteAbility(ISelectResult listener) { List<ohos.distributedschedule.interwork.DeviceInfo> onlineDevices = DeviceManager.(ohos.distributedschedule.interwork.DeviceInfo.); HiLog.(, );

(onlineDevices.isEmpty()) {
    listener.onSelectResult();
    HiLog.(, );
    ;
}
HiLog.(, );

numDevices = onlineDevices.size();
ArrayList<String> deviceIds = ArrayList<>(numDevices);
ArrayList<String> deviceNames = ArrayList<>(numDevices);
onlineDevices.forEach((device) -> {
    .add(device.getDeviceId());
    .add(device.getDeviceName());
    HiLog.(,device.getDeviceName() + );
    HiLog.(,device.getDeviceType() + );
});
HiLog.(,deviceIds.size() + );
HiLog.(,deviceIds.get() + );
HiLog.(,,deviceIds.size(), deviceIds.get());
String selectDeviceId = deviceIds.get();
listener.onSelectResult(selectDeviceId);

}

@Override public void onStart(Intent intent) { // onStart方法中订阅事件 scheduleRemoteAbility(new ISelectResult() { @Override public void onSelectResult(String deviceId) { String event = “com.my.test1”; MatchingSkills matchingSkills = new MatchingSkills(); matchingSkills.addEvent(event); // 自定义事件 matchingSkills.addEvent(CommonEventSupport.COMMON_EVENT_SCREEN_ON); // 亮屏事件 CommonEventSubscribeInfo subscribeInfo = new CommonEventSubscribeInfo(matchingSkills); HiLog.info(label,“订阅了其他设备:” + deviceId);

        subscribeInfo.setDeviceId(deviceId);
        MyCommonEventSubscriber subscriber = new MyCommonEventSubscriber(subscribeInfo);

        try {
            CommonEventManager.subscribeCommonEvent(subscriber);
        } catch (RemoteException e) {
            HiLog.info(label,"subscribeCommonEvent occur exception.");
        }

    }
});

//发布公共事件
findComponentById(ResourceTable.Id_send_event)
    .setClickedListener(v->{
        try {
            Intent i = new Intent();
            Operation operation = new Intent.OperationBuilder()
                .withAction("com.my.test1")
                .build();
            i.setOperation(operation);
            CommonEventData eventData = new CommonEventData(i);
            CommonEventManager.publishCommonEvent(eventData);
        } catch (RemoteException e) {
            HiLog.info(label,"publishCommonEvent occur exception.");
        }
    });

}

两台设备,在onStart中互相订阅CommonEventData事件, 点击按钮其中一台设备发送

开发者你好,您的问题已经为您转相关人员待处理中,有回复后我们会及时同步。

同时欢迎论坛开发小伙伴们互帮互助…

感谢您对开发者论坛的支持!!

在HarmonyOS鸿蒙Next中,订阅其他设备的公共事件无效,可能是由于以下原因:

  1. 设备未连接:确保设备在同一网络下,并且已通过分布式能力连接。
  2. 权限问题:检查是否已授予必要的权限,如分布式数据管理权限。
  3. 事件未发布:确认目标设备已发布相应的公共事件。
  4. 订阅代码错误:检查订阅代码是否正确,包括事件名称、设备ID等参数。
  5. 系统版本不匹配:确保所有设备的HarmonyOS版本兼容,支持相同的公共事件机制。
  6. 网络问题:检查网络连接是否稳定,避免因网络问题导致事件无法传递。

排查以上问题后,若仍无效,建议检查日志以获取更多信息。

在HarmonyOS鸿蒙Next中,订阅其他设备的公共事件无效,可能是由于以下原因:

  1. 设备未连接:确保设备在同一网络下,且已通过分布式能力连接。
  2. 权限不足:检查是否已授予必要的分布式权限,如ohos.permission.DISTRIBUTED_DATASYNC
  3. 事件未发布:确认目标设备已正确发布公共事件。
  4. 订阅配置错误:检查订阅代码,确保事件类型和参数匹配。
  5. 系统版本不兼容:确保所有设备运行兼容的HarmonyOS版本。

建议逐一排查上述问题,确保设备间通信正常。

回到顶部