java 安卓与PC蓝牙应用的连接
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10436785/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Connection between android and PC bluetooth applications
提问by Muhammad Salman Farooq
I am working on a bluetooth application which involves connection of my android bluetooth application with a server running on computer. The server is also a bluetooth application made using blue-cove api.
我正在开发一个蓝牙应用程序,它涉及将我的 android 蓝牙应用程序与计算机上运行的服务器连接起来。服务器也是一个使用 blue-cove api 制作的蓝牙应用程序。
The problem i am facing now that i am unable to make connection between my mobile app and that computer server application.
我现在面临的问题是我无法在我的移动应用程序和该计算机服务器应用程序之间建立连接。
here is my code for android app:
这是我的 android 应用程序代码:
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;
}
Where is the problem or what possibly i am missing in it. And also i am having ambiguity in the understanding of socket.connect() method. Kindly help me in this regard.
问题出在哪里,或者我可能遗漏了什么。而且我对 socket.connect() 方法的理解也有歧义。请在这方面帮助我。
回答by digitalhack
I just finished working on a similar application and had problems connecting. It would help to see a bit more of your code but the excerpt above looks like the example I was following. I would start by checking that the MY_UUID exists on the PC that you are trying to connect to.
我刚刚完成了一个类似的应用程序,但在连接时遇到了问题。查看更多代码会有所帮助,但上面的摘录看起来像我正在遵循的示例。我将首先检查您尝试连接的 PC 上是否存在 MY_UUID。
I have posted my example, both the client and server, on my blog at http://digitalhacksblog.blogspot.com/2012/05/android-example-bluetooth-simple-spp.html.
我已经在我的博客http://digitalhacksblog.blogspot.com/2012/05/android-example-bluetooth-simple-spp.html上发布了我的示例,包括客户端和服务器。
Hope this helps.
希望这可以帮助。