Android——为一个连接打开多个套接字(输入和输出流)

发布于 2024-10-16 17:40:40 字数 585 浏览 2 评论 0原文

我可以通过蓝牙成功将我的 Nexus One Android 手机连接到另一部蓝牙 Android 手机。我可以使用输入和输出流进行连接并编写我的请求并读取它们的响应。我的应用程序使用实时数据进行处理。我想知道有没有办法在不同的套接字(有点像专用套接字)上打开两个输入/输出流进行通信?如果是的话我怎样才能做到这一点?任何指示都会有帮助...

mmSocket.connect();

公共无效连接() 自:API 级别 5 尝试连接到远程设备。

该方法将阻塞,直到建立连接或连接失败。如果此方法返回且没有异常,则此套接字现在已连接。

在设备发现过程中,不应尝试创建与远程蓝牙设备的新连接。设备发现是蓝牙适配器上的一个重量级过程,会显着减慢设备连接速度。使用cancelDiscovery() 取消正在进行的发现。发现不是由 Activity 管理,而是作为系统服务运行,因此应用程序应该始终调用 cancelDiscovery(),即使它没有直接请求发现,只是为了确定。

现在我的问题是:我如何使用两个套接字进行连接...因为上面的函数使用套接字进行连接...并且在连接后它会断开另一个套接字的任何其他尝试????

I can successfully connect through Bluetooth my Nexus One Android Phone to another Bluetooth Android Phone. I can use the input and output streams for connection and write my requests and read their responses . My application uses real time data for processing. I was wondering is there any way to open two input/output streams on different sockets (sort of like dedicated sockets) for communication? And if yes how could i accomplish that ? Any pointers would be helpful ...

mmSocket.connect();

public void connect ()
Since: API Level 5
Attempt to connect to a remote device.

This method will block until a connection is made or the connection fails. If this method returns without an exception then this socket is now connected.

Creating new connections to remote Bluetooth devices should not be attempted while device discovery is in progress. Device discovery is a heavyweight procedure on the Bluetooth adapter and will significantly slow a device connection. Use cancelDiscovery() to cancel an ongoing discovery. Discovery is not managed by the Activity, but is run as a system service, so an application should always call cancelDiscovery() even if it did not directly request a discovery, just to be sure.

Now my question is : How can i connect using two sockets ... because the function above uses a socket to connect ... And after a connection it disconnects any other attempt by another socket ????

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

醉殇 2024-10-23 17:40:40

这当然是可能的,但我猜您对套接字使用了阻塞调用,因此您将需要多个线程来实时完成有用的工作。这也意味着如果线程要进行通信,则需要在线程之间进行一些同步。

坦率地说,你的问题有点模糊,所以我认为你不会得到任何好的答案,除非你稍微限定一下:你到底想做什么?为什么需要多个插座?第二个插座是做什么用的?

It is of course possible, but I am guessing you use blocking calls for the sockets, so you will need more than one thread to do useful work in real time. This also means you need a bit of synchronization between threads if they are to communicate.

Your'e question is frankly a bit vague, so I don't think you will get any good answers unless you qualify it a bit: what exactly do you want to do? Why do you need more than one socket? What is the second socket for?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文