c# 蓝牙客户端重新连接很慢

问题描述

我的 bluetooth connection(PC -- 模块)遇到问题,reconnect 到已经配对的非常慢(与标准的“第一”相比)联系 )。很可能是 coding issue 但我不知道是什么

我仍然熟悉蓝牙连接。当我在 ClientConnectThread() 中调用它时,catch 可能有问题。因为一开始我通过创建一个new Thread然后自己来调用它。

它可以工作,但至少需要 8 seconds 或更多才能连接到模块。原来的时间好像是2 seconds顶。

    private void ScanBT_btn_Click(object sender,EventArgs e)
    {
        StartScan();
    }
    private void StartScan()
    {
        comboBox1.DataSource = null;
        comboBox1.Items.Clear();
        items.Clear();
        Thread BluetoothScanThread = new Thread(new ThreadStart(scanBT));
        BluetoothScanThread.Start();
    }
    private void scanBT()
    {
        updateUI("Starting Scan..");

        BluetoothClient client = new BluetoothClient();
        devices = client.DiscoverDevicesInRange();
        updateUI("Scan complete");
        foreach (BluetoothDeviceInfo d in devices)
        {
            items.Add(d.DeviceName);
        }
        updateDeviceList();
    }
    BluetoothDeviceInfo deviceInfo;

    private void ClientConnectThread()
    {
        timer1.Start();
        pictureBox1.BackColor = Color.Red;
        BluetoothClient client = new BluetoothClient();
        updateUI("attemping connection...");
        client.BeginConnect(deviceInfo.DeviceAddress,mUUID,this.bluetoothClientConnectCallBack,client);

        
    }
    void bluetoothClientConnectCallBack(IAsyncResult result)
    {
        try
        {
            BluetoothClient client = (BluetoothClient)result.AsyncState;
            client.EndConnect(result);
            NetworkStream stream = client.GetStream();
            stream.ReadTimeout = 1000;
            updateUI(timer1.Elapsed.ToString());
            byte[] recv = new byte[1024];
            while (true)
            {

                Array.Clear(recv,recv.Length);
                do
                {
                    stream.Read(recv,recv.Length);
                    var str = System.Text.Encoding.ASCII.GetString(recv);
                    updateUI(str);
                    pictureBox1.BackColor = Color.LightGreen;
                    Thread.Sleep(100);

                } while (stream.DataAvailable);

            }
        }
        catch
        {
            pictureBox1.BackColor = Color.Red;
            ClientConnectThread(); // call method to retry ( when client lost )
        }
    }
    private void ConnectBtn_Click(object sender,EventArgs e)
    {
        deviceInfo = devices.ElementAt(comboBox1.SelectedIndex);
        updateUI(deviceInfo.DeviceName + " was selected");


        if (pairDevice())
        {
            updateUI("Device Paired");
            updateUI("Starting connection... ");
            Thread bluetoothClientThread = new Thread(new ThreadStart(ClientConnectThread));
            bluetoothClientThread.Start();
        }
        else
        {
            updateUI("Pair failed!!!");
        }

    }
    private bool pairDevice()
    {
        
        if (!deviceInfo.Authenticated)
        {
            PairForm pairForm = new PairForm();

            var dialogResult = pairForm.ShowDialog();
            
            if (!BluetoothSecurity.PairRequest(deviceInfo.DeviceAddress,Environment.GetEnvironmentVariable("PairPW")))
            {
                return false;
            }

        }
        return true;
    }

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...