连接到设备时出现异常

时间:2015-02-10 16:49:18

标签: c# bluetooth 32feet

我的蓝牙设备(HC-05)连接有问题。 当调用BluetoothClient.Connect()时,有时会发生异常 - "提供了无效的参数。"或其他。但有时设备连接(通常在第一次连接时)! 离开应用程序时是否必须关闭连接?

1 个答案:

答案 0 :(得分: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;
            } 

    }
相关问题