附近的连接2.0:混合使用不同的策略? [英] Nearby Connections 2.0: Mixing different strategies?

查看:101
本文介绍了附近的连接2.0:混合使用不同的策略?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试新的 Android附近的连接v2.0 API,并且对用于广告和发现的连接策略有一些疑问.这些问题涉及奇怪的情况,可能不容易回答.但是,当我在另一篇文章的评论中提到这些潜在问题时,Neighborhood Connections开发人员要求我针对这些问题创建一个单独的问题.这个问题涉及我想到的一些奇怪(但有些不自然)的极端情况(但是,那些做普通事情的普通开发人员不太可能遇到).

I've been experimenting with the new Android Nearby Connections v2.0 API, and have some questions about the Connection Strategies used for advertising and discovery. These questions involve strange edge cases, and might not be easy to answer. But when I mentioned these potential problems in a comment on another post, a Nearby Connections developer requested that I create a separate question about these issues. This question concerns some strange (but somewhat unnatural) edge cases that I've thought of (but which normal developers doing ordinary things would be unlikely to encounter).

文档包含以下模棱两可的声明:"Nearby Connection支持不同的策略广告和发现."最初,我将其解释为意味着我们可以选择两种可用的连接策略之一,并将其​​用于广告和发现.由于连接策略在特定节点上定义了(本地)连接拓扑,因此有意义的是,我们需要对广告和发现使用相同的连接策略.

The documentation contains this slightly ambiguous statement: "Nearby Connections supports different Strategies for advertising and discovery." Initially, I interpreted this to mean that we can select one of the two available connection strategies, and use this for both advertising and discovery. Since the connection strategy defines the (local) connection topology at a specific node, it makes sense that we would need to use the same connection strategy for both advertising and discovery.

但是,人们也可以将模棱两可的陈述解释为意味着(字面上)可以使用不同的策略进行广告和发现.这似乎很荒谬,但是似乎没有任何机制可以迫使这些策略完全相同.由于startAdvertising()和startDiscovery()都采用"strategy"参数,因此很有可能将不同的策略传递给每个策略.

However, one could also interpret the ambiguous statement to mean that one can (literally) use different strategies for advertising and discovery. This seems absurd, but there doesn't seem to be any mechanism to actually force the strategies be the same. Since both startAdvertising() and startDiscovery() take a "strategy" parameter, it's quite possible to pass a different strategy to each.

注意:我已经通过实验找到了上述问题的答案.如果广告商和发现者正在使用不匹配的策略,则永远不会到达发现者的onEndpointFound回调(因此,我猜测较低级别的代码必须意识到存在不匹配,并且永远不会将其发送出去).

Note: I've figured out an answer to the above question experimentally. If the advertiser and discoverer are using mismatched strategies, the discoverer's onEndpointFound callback is never reached (so I'm guessing that the lower-level code must realize that there's a mismatch and never send it up).

另一个问题:是否可以使用混合策略的异构网络?由于连接策略会影响两个连接的节点之间使用的通信方式(P2P_CLUSTER使用低带宽进行小数据传输,而P2P_STAR同时使用蓝牙和Wifi热点以获取更高的带宽),因此似乎需要两个节点连接,两者都需要使用相同的策略.

Another question: Is a heterogeneous network (with a mixture of strategies) possible? Since the connection strategy affects the means of communication used between two connected nodes (P2P_CLUSTER using low bandwidth for small data transfers, and P2P_STAR using both Bluetooth and Wifi Hotspots for higher bandwidth), it seems like in order for two nodes to connect, both would need to use the same strategy.

如前所述,实验表明,两个节点确实需要使用相同的策略进行连接.但是,我可以想象另一种极端情况,其中设备A使用群集"策略连接到设备B,切换策略(同时保持连接打开),然后使用星形"策略连接到设备C.这行得通吗?我不知道.由于我当前的测试程序会在配置更改后重新启动,因此我需要编写新代码来测试这种极端情况(而且我还有很多其他我想写的代码).但是附近的连接"开发人员有兴趣了解潜在的边缘情况,因此我在这里提到了这一点.

As mentioned earlier, experiments suggest that both nodes do need to use the same strategy in order to connect. However, I could imagine another edge case in which device A connects to device B in using the "Cluster" strategy, switches strategies (while keeping the connection open), and connects to device C using the "Star" strategy. Would this work? I don't know. Since my current test program restarts after configuration changes, I'd need to write new code in order to test this edge case (and I've got lots of other code that I'd rather be writing). But the Nearby Connections developer was interested in hearing about potential edge cases, so I'm mentioning this here.

这些问题是在我尝试调试其他连接问题时出现的.为了使我的实验程序更具通用性,我为连接策略添加了设置"选项,并添加了另一个设置"选项,用于控制程序是否应仅进行广告发布或仅进行发现,或同时进行这两项操作.然后我必须决定这些设置更改时应该怎么办.

These questions came up while I was trying to debug other connection problems. To make my experimental program more versatile, I added a Settings option for the connection strategy, and another Settings option controlling whether the program should just advertise, or just discover, or both. And then I had to decide what should happen when these setting change.

使用请求的策略停止广告/发现然后重新启动指定的内容就足够了吗?但是,如果以前的策略遗留下来的任何现有连接会发生什么情况?这个答案似乎并不明显.

Would it suffice to just stop advertising/discovery and then restart whatever's specified, using the requested strategy? But then what would happen with any existing connections leftover from the previous strategy? This answer doesn't seem obvious.

也许这些问题只会出现在像我的测试程序这样的人为场景中(我之所以写是因为我试图理解其他连接问题).在最终产品中,我可能只会选择一种策略并始终如一地使用它.而且,如果附近的连接"是开源的,那么我只是看一下代码,以了解在这些奇怪的极端情况下可能发生的情况.但是由于我看不到源代码,所以很高兴看到更多有关预期行为的文档

Maybe these questions would only come up in an artificial scenario like my test program (which I only wrote because I was trying to understand other connection problems). In a final product, I'd probably just pick one strategy and use it consistently. And if Nearby Connections were open-source, I'd just look at that code to learn what might happen in these strange edge cases. But since I can't look at the source, it would be nice to see a little more documentation about expected behaviors

推荐答案

是的,您回答了自己的问题.您必须在广告客户和发现者方面都匹配策略.否则,您将永远找不到广告设备.

Yup, you answered your own questions. You must match strategies on both the advertiser and discoverer side. Otherwise, you will never discover the advertising device.

对于流中间的切换策略,这是明确禁止的.当您仍在与先前的策略保持联系时,开始广告/开始发现时会出错.

As for switching strategies in the middle of a flow, it's explicitly disallowed. You'll get an error when you start advertising / start discovery while you still have connections over a previous strategy.

这篇关于附近的连接2.0:混合使用不同的策略?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆