在 Android 中建立蓝牙微微网
我正在致力于在测试台中的多个设备之间建立蓝牙微微网。所有设备都知道该网络的拓扑。
测试台中的设备是 Ubuntu 台式电脑和 Android (Eclair) 设备。现在,我正在寻找一种以确定性方式在这些设备之间建立主从关系的方法。具体来说,我正在寻找一种方法来将 Android 设备建立为主设备并打开与其他 7 个设备的多个连接。
我已经查看了使用 bluez 堆栈和 NDK 的本机实现,但是我的设备(Samsung GT 15503)上的 bluez 堆栈实现不符合我猜测的标准,甚至像 hcitool、hciconfig 这样的普通应用程序也无法工作。
因此,我尝试使用官方的SDK,甚至成功地与我的笔记本电脑建立了RFCOMM套接字(以蓝牙聊天示例应用程序作为参考)。但我陷入了尝试使用相同的 BluetoothServerSocket 连接两个或多个设备的境地。除非我关闭原来的套接字,否则我似乎无法打开新的连接。
非常感谢这方面的任何建议。
I am working on establishing a Bluetooth Piconet among multiple devices in a testbed. The topology of this network is known to all devices.
The devices in the testbed are Ubuntu Desktop PCs and Android (Eclair) devices. Now, I'm looking at a way of establishing a master slave relationship among these devices in a deterministic way. Specifically, I'm looking for a way to establish an android device as master and open multiple connections with 7 other devices.
I have looked at native implementations using the bluez stack and the NDK, but the bluez stack implementation on my device (Samsung GT 15503) does not conform to the standards I guess and even normal apps like hcitool, hciconfig don't work.
Therefore, I tried using the official SDK and even succeeded in establishing an RFCOMM socket with my laptop (Using the bluetooth chat sample app as a reference). But I'm stuck at the point where I try connecting two or more devices using the same BluetoothServerSocket. Unless I close the original socket, I can't seem to open new connections.
Any suggestions in this regard are greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我终于明白我做错了什么。显然,每当您从BluetoothServerSocket调用accept方法并取回套接字时,您必须在再次调用accept之前关闭该套接字。
我解决了这个问题,通过创建 7 个不同的 UUID 并使用 BluetoothServerSocket 来侦听和接受这些 UUID 中每个 UUID 的连接,从而建立了我想要的微微网。一旦获得特定 UUID 的连接,我就会关闭相应的服务器套接字,并为下一个 UUID 重新打开另一个连接。
以下代码片段说明了我从 BTClickLinkCompete。
这里,mUuid是一个数组,存储了7个不同的uuid。尝试连接到服务器的客户端也将拥有这些 uuid,并且会按顺序一一尝试它们,因为它们不知道已经连接到服务器的客户端数量。
I finally figured out what I was doing wrong. Apparently, whenever you call the accept method from a BluetoothServerSocket and get back a socket, you have to close this socket before calling accept again.
I worked around this problem to establish the piconet I wanted by creating 7 different UUIDs and using a BluetoothServerSocket to listen and accept a connection for each of these UUIDs. Once I get a connection for a particular UUID, I close the corresponding server socket and reopen another one for the next UUID.
The following snippet illustrates the idea, which I got from BTClickLinkCompete.
Here, mUuid is an array that stores 7 different uuids. The clients trying to connect to the server will also possess these uuids and will try them out one by one in order because they do not know the number of clients already connected to the server.
也许这会有所帮助:
您必须为每个连接调用方法
accept()
(来自BluetoothServerSocket
),记住这一点。当您收到连接时,您应该启动一个新线程,其中包含您想要对该连接执行的操作,然后再次调用
accept()
Maybe this helps:
You have to call the method
accept()
(fromBluetoothServerSocket
) for each connection remember that.When you receive a connection you should start a new thread with what you want to do to that connection and then call the
accept()
again