我的蓝牙设备 (HC-05) 连接出现问题。调用时BluetoothClient.Connect()
,有时会发生异常-“提供了无效的参数。”或其他。但有时设备会连接(通常是在第一次连接时)!离开应用程序时是否必须关闭连接?
问问题
1182 次
1 回答
1
是的,您应该关闭连接并处理 BluetoothClient。
private InTheHand.Net.Sockets.BluetoothClient BTClient =
new InTheHand.Net.Sockets.BluetoothClient();
private System.Net.Sockets.NetworkStream stream;
//代码上的某处:
stream = BTClient.GetStream();
public void Disconnect()
{
if (BTClient == null )
return;
try
{
if (BTClient != null)
{
if (stream != null)
{
stream.ReadTimeout = 500;
stream.WriteTimeout = 500;
stream.Close();
}
if(BTClient.Connected)
BTClient.Close();
BTClient.Dispose();
}
}
catch (Exception ex)
{
throw ex;
}
}
于 2015-02-12T11:58:38.763 回答