两个 Android 设备之间的 RFCOMM 连接?
我想使用蓝牙连接两个 Android 设备,并通过 RFCOMM 通道传输数据。我只有一个设备接收数据,而另一台设备发送数据...
使用此代码,我能够连接到另一台设备并开始监听 RFCOMM 通道:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(device, 2);
socket.connect();
class BasicThread implements Runnable{
public void run() {
try {
InputStream stream = socket.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
while (true){
Log.d("myapp", "now listening...");
latestLine = r.readLine();
Log.d("myapp", latestLine);
}
} catch (IOException e) {
}
}
}
new Thread(new BasicThread()).run();
使用另一台设备,我实现了一个监听套接字像这样:
Method m = blue.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class });
BluetoothServerSocket socket = (BluetoothServerSocket) m.invoke(blue, 2);
BluetoothSocket sock = socket.accept();
Log.d("myapp", "Connected...\n\n\n\n\n\n\n\n");
OutputStream s = sock.getOutputStream();
final PrintWriter out = new PrintWriter(s);
它们都在 RFCOMM 通道 2 上连接,并且都互相看到对方,但是,第二个设备始终在 BluetoothSocket sock = socket.accept();
处保持阻塞状态
有帮助吗?
I have two Android devices which I want to connect, using Bluetooth, and transfer data over an RFCOMM channel. I only one one device to receive data, while the other device sends it...
Using this code, I am able to connect to the other device and begin listening to an RFCOMM channel:
Method m = device.getClass().getMethod("createRfcommSocket", new Class[] { int.class });
socket = (BluetoothSocket) m.invoke(device, 2);
socket.connect();
class BasicThread implements Runnable{
public void run() {
try {
InputStream stream = socket.getInputStream();
BufferedReader r = new BufferedReader(new InputStreamReader(stream));
while (true){
Log.d("myapp", "now listening...");
latestLine = r.readLine();
Log.d("myapp", latestLine);
}
} catch (IOException e) {
}
}
}
new Thread(new BasicThread()).run();
Using the other device, I have implemented a listening socket like this:
Method m = blue.getClass().getMethod("listenUsingRfcommOn", new Class[] { int.class });
BluetoothServerSocket socket = (BluetoothServerSocket) m.invoke(blue, 2);
BluetoothSocket sock = socket.accept();
Log.d("myapp", "Connected...\n\n\n\n\n\n\n\n");
OutputStream s = sock.getOutputStream();
final PrintWriter out = new PrintWriter(s);
They both connect on RFCOMM channel 2, and both SEE eachother, however, the second device always remains blocked at the BluetoothSocket sock = socket.accept();
Any help?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好的,我是新手,但我可以尽力提供帮助。这是我的经验,我设法使用反射连接两个设备。我的 Android 手机使用
listenUsingInsecureRfcommOn
方法接收数据,而其他设备是通信主设备并通过 BT SPP 发送数据。我在使用此方法时遇到了问题,因为它没有生成可见的 SDP 记录,因此我无法使用其他设备检测到它。因此,我使用 Bluecove 和 Java SE 制作了小型嗅探器,尝试连接到给定范围内的每个端口。这是代码:据我所知,某些端口被占用,而某些端口无法使用(如文档所述,例如端口 0)。例如,我认为端口 2 已被占用,因为当我向其发送一些数据时,我会收到以 ERR 开头的 5 个字符:)。
另一方面,我的线程仍在等待?! :)
这让我们注意到我注意到的另一件事,端口(或通道)并不总是映射到所需的数字。例如,对我来说,经常发生这样的情况:我想在端口 15 上发送一些东西,但在 Android 上,等待端口 9 的线程收到了数据:)
所以我建议,检查哪个端口真正被分配了!
您可以使用我发布的代码来实现这一点。
另一件事,这里是一个 link 到
channelPicker
函数,当使用普通 API 时选择通道,如果我是没有记错,里面的一些常量应该代表保留通道。我刚刚注意到一些事情,我的注册端口的代码略有不同,这是我的做法:
无论如何,我知道这可能为时已晚,但是,也许将来有人有类似的问题。
OK, I am newbie, but I can try to help. So here is my experience, I managed to connect two devices using reflection. My Android phone is receiving data using method
listenUsingInsecureRfcommOn
, while other devices are masters in communication and send the data over BT SPP. I had a problem with this method since it makes no visible SDP record, so I could not detect it with other devices. Because of that, I made small sniffer using Bluecove and Java SE that tries to connect to every port in given range. Here's the code:What I've learned it that some ports are taken, and that some ports can not be uses (as documentation says, e.g. port 0). For example, port 2 I believe was taken, because when I send some data to it I receive 5 chars back beginning with ERR :).
While, on the other hand, my thread is still waiting?! :)
That leads us to another thing I noticed, ports (or channels) are not always mapped to desired number. For example, to me often happened that I want to send something on port 15, but on Android, thread waiting on port 9 received the data :)
So I suggest, check which port is really allocated!
You can achieve that using the code I posted.
And another thing, here is a link to
channelPicker
function, which selects channel when ordinary API is used, if I am not mistaken, inside some constants should represent reserved channels.I just noticed something, my code for registering port is slightly different, here is how I do it:
Anyway, I know that this is probably too late, but, maybe someone in future has similar question.