0

我为 Windows 窗体编写了一个应用程序。我在将设备与 PC 配对时遇到问题。现在程序以下一个方式工作:打开潜水器,启动程序,将潜水器添加到蓝牙设备,按下连接按钮。我使用下一个功能:

    public BluetoothClient client = new BluetoothClient();
    public string selectedItem { get; set; }
    public BluetoothDeviceInfo[] AllDevices;

    public void GetDevices()
    {
        AllDevices = client.DiscoverDevicesInRange();
        foreach (BluetoothDeviceInfo Device in AllDevices)
        {
            if(Device.DeviceName.Contains("Kortes"))
                onSetDevices(Device.DeviceName); // event to get device name and add it to ComoBox element on form
        }
        onSetProgress(); // event, that all devices were found, set progress bar and etc.
    }
    public void GoConnect()
    {
        foreach (BluetoothDeviceInfo Device in AllDevices)
        {
            if (Device.DeviceName.Equals(selectedItem)) // item from ComboBox
            {
                if (!client.Connected)
                    client = new BluetoothClient();
                client.BeginConnect(Device.DeviceAddress, Device.InstalledServices[0], this.BluetoothClientConnectCallback, client);
                break;
            }
            else 
            {
                MessageBox.Show("Choose the device");
            }
        }
    }
    private void BluetoothClientConnectCallback(IAsyncResult ar)
    {
        //Have no problem with this
    }

这些功能运作良好。我可以找到并连接所需的设备。但问题是首先我需要将我的设备添加到操作系统中的蓝牙设备并输入 PIN 码。如何改进我的代码来解决这个问题?

我不想添加设备。我想直接使用它。我可以使用哪些方法以编程方式输入 PIN 码?程序必须按以下方式工作:打开设备,启动程序,然后按下连接按钮。

4

1 回答 1

4

您尝试在不配对的情况下进行连接。您的代码无法正常工作,因为您必须在连接前进行配对。

代替

client = new BluetoothClient();

client.BeginConnect(Device.DeviceAddress, Device.InstalledServices[0], this.BluetoothClientConnectCallback, client);

经过

BluetoothSecurity.PairRequest(Device.DeviceAddress,"123456");

查看http://mrbikash.com/bluetooth-discovery-pairing-32feet-net/#pairing以获得更详细的说明。

于 2016-01-05T03:29:37.050 回答