当前位置:编程学习 > C#/ASP.NET >>

蓝牙连接过程中,拔下适配器,再插上。无法重连接!

用的是32feet.NET的InTheHand.Net.
【问题】
PC通过蓝牙适配器和蓝牙设备连接通信。通信过程中,如果关闭蓝牙设备后再开启,可以重连。但是如果拔下蓝牙适配器,再插上,就重接不上。

【代码】

public class BlueBase
{
    protected BluetoothClient blueClient;

    //Bluetooth service GUID//
    private Guid serviceGuid = BluetoothService.SerialPort;//BluetoothService.RFCommProtocol;

    //蓝牙地址//
    private BluetoothAddress addr;

    //匹配pin//
    private string pin = "";

    //线程//
    private bool isRun = true;
    private Thread t = null;

    public BlueBase() { }

    /// <summary>
    /// 通过地址连接
    /// </summary>
    public void ConnectByAddr(string addrName, string pwd)
    {
        if (!BluetoothAddress.TryParse(addrName.Trim(), out addr))
        {
            Trace.WriteLine("Invalid address.");
            return;
        }

        pin = pwd;
        RunBlueThread();
    }

    /// <summary>
    /// 启动线程
    /// </summary>
    private void RunBlueThread()
    {
        isRun = true;

        if (t != null && t.IsAlive) return;

        t = new Thread(new ThreadStart(ListenLoop));
        t.IsBackground = true;
        t.Start();
    }

    private void ListenLoop()
    {
        while (isRun)
        {
            try
            {
                if (!BluetoothRadio.IsSupported) continue;

                BluetoothRadio BuleRadio = BluetoothRadio.PrimaryRadio;
                BuleRadio.Mode = RadioMode.PowerOff;
                BuleRadio.Mode = RadioMode.Connectable;

                blueClient = new BluetoothClient();
                blueClient.Client.ReceiveTimeout = 5000;
                blueClient.SetPin(addr, pin);
                blueClient.Connect(addr, serviceGuid);  //---拔下适配器再插上报错位置-----//
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }

            while (blueClient.Connected && isRun)
            {
                try
                {
                    //处理数据发送和接收//
                    TransData();
                }
                catch (System.Exception ex)
                {
                    Trace.WriteLine(ex.Message);
                    break;
                }
            }

            try
            {
                blueClient.Close();
                blueClient.Dispose();
                blueClient = null;
                Thread.Sleep(1000);
            }
            catch (System.Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }
    }
}


出错的堆栈信息:

提供了一个无效的参数。 070112000028:0000110100001000800000805f9b34fb
   at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
   at System.Net.Sockets.Socket.Connect(EndPoint remoteEP)
   at InTheHand.Net.Bluetooth.Msft.SocketBluetoothClient.Connect(BluetoothEndPoint remoteEP)
   at InTheHand.Net.Sockets.BluetoothClient.Connect(BluetoothEndPoint remoteEP)
   at InTheHand.Net.Sockets.BluetoothClient.Connect(BluetoothAddress address, Guid service)
   at BlueCom.BlueBase.ListenLoop() 此处对应上面的 blueClient.Connect(addr, serviceGuid);这一行

纠结了好多天了。请教,请教。 --------------------编程问答-------------------- 有人知道吗,在线等 --------------------编程问答-------------------- --------------------编程问答-------------------- /upload/20131227/27.gif --------------------编程问答-------------------- 楼主这是做什么遇到的?
补充:.NET技术 ,  C#
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,