简体   繁体   English

连接设备时发生异常

[英]Exception when connecting to device

I have a problem with connection to my Bluetooth device (HC-05). 我与蓝牙设备(HC-05)的连接有问题。 When call BluetoothClient.Connect() , sometimes exception occures - "An invalid argument was supplied.", or another. 调用BluetoothClient.Connect() ,有时会发生异常-“提供了无效的参数。”或其他。 But sometimes device connects (usually at first connection)! 但是有时设备会连接(通常是第一次连接)! Do I have to close connection when I leave app? 离开应用程式时是否必须关闭连线?

Yes you should close the connection and dispose the BluetoothClient. 是的,您应该关闭连接并处置BluetoothClient。

private InTheHand.Net.Sockets.BluetoothClient BTClient = 
new InTheHand.Net.Sockets.BluetoothClient(); 
private System.Net.Sockets.NetworkStream stream;

//Somewhere on the code: //代码中的某处:

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;
            } 

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM