Nearby Connections 2.0: Mixing different strategies?

人走茶凉 提交于 2019-12-11 08:48:44

问题


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).

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.

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.

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).

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.

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


回答1:


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.



来源:https://stackoverflow.com/questions/46509973/nearby-connections-2-0-mixing-different-strategies

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!