Java 中的蓝牙同时充当客户端/服务器
我正在尝试创建一个蓝牙应用程序,该应用程序将同时侦听传入连接(作为服务器)并使用 Java 和 BlueCove 寻找可用连接(作为客户端)。前提是两个蓝牙设备(例如智能手机)可以自动发现彼此,并在传输范围内立即开始某种文件交换或其他协作(这是针对机会网络的实验)。
我有一个在自己的线程中运行的基本蓝牙客户端和在自己的线程中运行的基本蓝牙服务器,我试图让它们同时工作。这已经成功了一半,但是,无论出于何种原因,一台设备最终总是接收传入连接,但从未找到其他设备,而一台设备最终找到其他连接,但从未接收到它们(两台机器都运行相同的代码,具有相同的蓝牙)软件狗 - 一台机器运行 Windows XP,另一台机器运行 Windows 7)。到目前为止,我只在两台设备上尝试过此操作。
我收到异常,例如:
“java.io.IOException:无法查询远程设备”
“java.io.IOException:名称查找错误;[10108] 没有此类服务。无法在指定的名称空间。”
“连接失败;[10051] 尝试对无法访问的网络进行套接字操作。”
两个线程都必须调用 LocalDevice 和 StreamConnection,因此我是否需要同步线程对这些资源的访问,或者线程可以同时访问这些资源吗?我可以锁定这些对象,还是需要循环线程,以便客户端在服务器侦听连接时休眠,反之亦然(我试图避免只是循环它们,因为蓝牙中的设备发现序列是如此慢,并且客户端线程会让服务器线程在大部分时间等待)?
我对多线程缺乏经验,并且仍在学习蓝牙方面的知识,因此如果有人有提示或信息,我将非常感激。
I am trying to create a Bluetooth application that will simultaneously listen for incoming connections (as a server) and seek out available connections (as a client) using Java and BlueCove. The premise being that two Bluetooth devices (such as smart phones) can automatically discover one another and begin some sort of file exchange or other collaboration as soon as they're within transmission range (this is for an experiment in opportunistic networking).
I have a basic Bluetooth client running in its own thread and a basic Bluetooth server running in its own thread, and I'm trying to get them working at the same time. This has been halfway successful, but, for whatever reason, one device always ends up receiving incoming connections and never finding other devices, and one device ends up finding other connections, but never receiving them (both machines are running identical code, with identical Bluetooth dongles - one machine in Windows XP and the other in Windows 7). I have only tried this with two devices thus far.
I'm receiving exceptions, such as:
"java.io.IOException: Can't query remote device"
"java.io.IOException: Name Lookup error; [10108] No such service is known. The service cannot be found in the specified name space."
"Failed to connect; [10051] A socket operation was attempted to an unreachable network."
Both threads are having to make calls to LocalDevice and StreamConnection, so do I need to synchronize the threads' access to these resources or can the threads access these concurrently? Can I lock those objects, or do I need to cycle the threads, so that the client sleeps while the server is listening for connections, and vice versa (I'm trying to avoid just cycling them because the device discovery sequence in Bluetooth is so slow, and the client thread would be keeping the server tread waiting much of the time)?
I'm inexperienced with multi-threading and am still learning my way around Bluetooth, so if anyone has tips or info, I would really appreciate it.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
无线电硬件的功能似乎有所不同。正如您所发现的,有些似乎只支持一种连接...所以购买另一个功能更强大的连接,但我不知道如何知道哪个...:-(
Radio hardware seems to vary in its capabilities. Some, as you've found, seem only really to support one connection... So buy another more capable one, but I don't know how one knows which... :-(