如何使用 bluecove 断开 Java 中的 BT 设备?
How Can I disconnect a BT device in Java using bluecove?
我是使用 Java 进行蓝牙通信的新手。查看 bluecove 文档,我执行了以下操作以便能够与蓝牙设备通信:
- 发现它
- 通过 Connector.open` 获取
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
。
当在断开连接之后调用连接函数时,我总是会遇到以下异常:
javax.bluetooth.BluetoothConnectionException: Failed to connect; [10048] Only one usage of each socket address (protocol/network address/port) is normally permitted.
谁能告诉我如何关闭连接?
我找到了解决方案。
必须在调用函数 Connection.openInputStream()
和 Connection.openOutputStream()
获得的流结束后调用 Connection.close()
断开连接函数如下:
public void Disconnect() throws IOException
{
inStream.close();
outStream.close();
Connection.close();
inStream = null;
outStream = null;
Connection = null;
}
我是使用 Java 进行蓝牙通信的新手。查看 bluecove 文档,我执行了以下操作以便能够与蓝牙设备通信:
- 发现它
- 通过 Connector.open` 获取
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
。
当在断开连接之后调用连接函数时,我总是会遇到以下异常:
javax.bluetooth.BluetoothConnectionException: Failed to connect; [10048] Only one usage of each socket address (protocol/network address/port) is normally permitted.
谁能告诉我如何关闭连接?
我找到了解决方案。
必须在调用函数 Connection.openInputStream()
和 Connection.openOutputStream()
Connection.close()
断开连接函数如下:
public void Disconnect() throws IOException
{
inStream.close();
outStream.close();
Connection.close();
inStream = null;
outStream = null;
Connection = null;
}