我正在使用Bluecove API为我的计算机构建一个简单的Java蓝牙服务器.我正在构建一个Android客户端应用程序,但问题似乎源于服务器端应用程序.
我确定有人已经在某处发布了任何答案,过去几天我已经严格尝试了不同的解决方案并查看了所有可能的论坛,但我似乎无法保持套接字线程上的连接.
我可以在从android到计算机建立连接之后交换消息,反之亦然,从计算机到android,但是在交换字节后,Bluecove堆栈关闭并关闭套接字连接,即使我没有明确告诉它connection.close()时.
我试图使用一段时间(某些陈述是真的)循环来保持连接活动,它不再在收到第一条消息后关闭套接字但它无法接收我从智能手机发送到我的计算机的任何后续消息无论是.虽然它基本上是活着的,但当我尝试从智能手机发送它们时,它无法接收任何新消息.不知道为什么.
对不起,这是我第一次发帖,我不确定为什么代码的第一部分无法在页面上正确显示.
代码连接:
public BluetoothServer()
{
try
{
service = (StreamConnectionNotifier)
Connector.open("btspp://localhost:" + new UUID(0x1101).toString() +
";name=SampleServer");
System.out.println("open connection");
while(runState == true){
connection = (StreamConnection) service.acceptAndOpen();
//send a greeting across to android
outputStream = connection.openOutputStream();
String greeting = "JSR-82 RFCOMM server says hello";
outputStream.write(greeting.getBytes());
//run thread that listens for incoming bytes through socket connection
Thread t = new Thread(new ConnectedThread(connection));
t.start();
}
} catch(IOException e) {}
}
Run Code Online (Sandbox Code Playgroud)
用于收听数据的线程代码
public void run()
{
try
{
//open input …Run Code Online (Sandbox Code Playgroud)