繁体   English   中英

连接设备时发生异常

[英]Exception when connecting to device

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

是的,您应该关闭连接并处置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;
            } 

    }

暂无
暂无

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

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