0

我希望我的计算机成为蓝牙服务器,同时处理来自智能手机的至少 100 个连接。这是这样的想法:

我启动服务器,它开始监听连接。对于每个连接,它应该存储设备的 mac 地址。每个设备都应该能够向服务器发送消息,反之亦然。

我一直在寻找很多,但没有找到特定的方法。

如何监听连接并处理它们?存储在数组中?开始一个线程?

我已经能够启动 1 个客户端服务器,但无法改进它。

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Threading;
using InTheHand.Net.Bluetooth;
using InTheHand.Net.Ports;
using InTheHand.Net.Sockets;
using System.IO;

namespace BluetoothChamada
{
    public partial class Form1 : Form
    {

        List<string> items;
        String clientConectedName, clientConectedMAC;

        public Form1()
        {
            InitializeComponent();
            items = new List<string>();
        }

        private void bGo_Click(object sender, EventArgs e)
        {
            if (serverStarted)
            {
                updateUI("---Servidor já foi Iniciado---");
                return;
            }
            if (rbServer.Checked)
            {
                connectAsServer();
            }
            else
            {
                Scan();
            }
        }

        private void startScan()
        {
            listBox1.DataSource = null;
            listBox1.Items.Clear();
            items.Clear();
            Thread bluetoothScanThread = new Thread(new ThreadStart(Scan));
            bluetoothScanThread.Start();
        }

        BluetoothDeviceInfo[] devices;
        private void Scan()
        {
            updateUI("Procurando dispositivos...");
            BluetoothClient client = new BluetoothClient();
            devices = client.DiscoverDevicesInRange();
            updateUI("Procura finalizada");
            updateUI(devices.Length.ToString() + " dispositivos encontrados");
            foreach (BluetoothDeviceInfo d in devices)
            {
                items.Add(d.DeviceName);
            }
            updateDeviceList();
        }

        private void connectAsServer()
        {
            Thread bluetoothServerThread = new Thread(new ThreadStart(ServerConnectThread));
            bluetoothServerThread.Start();
        }


        Guid mUUID = new Guid("00001101-0000-1000-8000-00805F9B34FB");
        bool serverStarted = false;
        public void ServerConnectThread()
        {

            serverStarted = true;
            BluetoothListener blueListener = new BluetoothListener(mUUID);
            blueListener.Start();
            updateUI("---Servidor Iniciado, aguardando clientes---\r\n\n");

            BluetoothClient connection = blueListener.AcceptBluetoothClient();
            getData();
            updateUI("Cliente: " + clientConectedName + "\r\nMac: " + clientConectedMAC);

            Stream mStream = connection.GetStream();
            while (true)
            {
                try
                {
                    byte[] received = new byte[1024];
                    mStream.Read(received, 0, received.Length);
                    updateUI(System.Environment.NewLine + "Recebido: " + Encoding.ASCII.GetString(received));

                    byte[] sent = Encoding.ASCII.GetBytes("FeedBack: OK \r\n");
                    mStream.Write(sent, 0, sent.Length);
                }
                catch (IOException exception)
                {
                    updateUI("Cliente foi desconectado");
                    break;
                }
            }
        }

        private void getData()
        {
            BluetoothClient client = new BluetoothClient();
            BluetoothDeviceInfo[] devices = client.DiscoverDevices();
            foreach (BluetoothDeviceInfo device in devices)
            {
                if (!device.Connected)
                {

                }
                else 
                { 
                    clientConectedName = device.DeviceName;
                    clientConectedMAC = device.DeviceAddress.ToString();
                }       
            }
        }

        private void updateUI(string message)
        {
            Func<int> del = delegate()
            {
                tbOutput.AppendText(message + System.Environment.NewLine);
                return 0;
            };
            Invoke(del);
        }

        private void updateDeviceList()
        {
            Func<int> del = delegate()
            {
                listBox1.DataSource = items;
                return 0;
            };
            Invoke(del);
        }

    }
}
4

0 回答 0