简体   繁体   中英

Exception when connecting to device

I have a problem with connection to my Bluetooth device (HC-05). When call BluetoothClient.Connect() , sometimes exception occures - "An invalid argument was supplied.", or another. 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.

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

    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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