我是使用Java进行蓝牙通信的新手。在查看bluecove文档时,我做了以下操作以能够与蓝牙设备进行通信:
StreamConnection
InputStream
和OutputStream
进行交流我在文档中找不到的唯一内容是如何关闭通讯。
我实现的猜测断开连接策略的代码如下:
public void Connect() throws IOException
{
final int service = 5;
String conString = "btspp://"+Dev.getBluetoothAddress()+":"+service;
Connection = (StreamConnection) Connector.open(conString);
inStream = Connection.openInputStream();
outStream = Connection.openOutputStream();
}
public void Disconnect() throws IOException
{
Connection.close();
}
Dev
是我从Discovery那里获得的RemoteDevice
。
当断开连接后调用Connect函数时,我总是收到以下异常:
javax.bluetooth.BluetoothConnectionException: Failed to connect; [10048] Only one usage of each socket address (protocol/network address/port) is normally permitted.
有人可以告诉我如何关闭连接吗?
答案 0 :(得分:0)
我找到了解决方案。
对Connection.close()
的调用必须在从对函数Connection.openInputStream()
和Connection.openOutputStream()
断开连接功能如下:
public void Disconnect() throws IOException
{
inStream.close();
outStream.close();
Connection.close();
inStream = null;
outStream = null;
Connection = null;
}