我正在开发一个蓝牙应用程序,它涉及将我的 android 蓝牙应用程序与计算机上运行的服务器连接。该服务器也是使用 blue-cove api 制作的蓝牙应用程序。
我现在面临的问题是我无法在我的移动应用程序和该计算机服务器应用程序之间建立连接。
这是我的安卓应用代码:
try {
// Connect the device through the socket. This will block
// until it succeeds or throws an exception
mySocket = myDevice.createRfcommSocketToServiceRecord(MY_UUID);
mySocket.connect();
toast = Toast.makeText(context, "Connection established", thisClass.duration);
toast.show();
} catch (IOException connectException) {
// Unable to connect; close the socket and get out
try {
mySocket.close();
toast = Toast.makeText(context, "Connection not established", thisClass.duration);
toast.show();
} catch (IOException closeException) { }
return;
}
问题出在哪里,或者我可能遗漏了什么。而且我对 socket.connect() 方法的理解也有歧义。请在这方面帮助我。
最佳答案
我刚刚完成了一个类似的应用程序,但在连接时遇到了问题。查看更多您的代码会有所帮助,但上面的摘录看起来像我正在关注的示例。首先,我会检查您尝试连接的 PC 上是否存在 MY_UUID。
我已经在我的博客 http://digitalhacksblog.blogspot.com/2012/05/android-example-bluetooth-simple-spp.html 上发布了我的示例,包括客户端和服务器。 .
希望这对您有所帮助。
关于java - android和PC蓝牙应用程序之间的连接,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10436785/