繁体   English   中英

无法使用 C# 连接蓝牙设备

[英]Unable to connect Bluetooth device using c#

我正在使用 32 英尺库来开发蓝牙通信 WPF 应用程序,并且能够配对设备但无法连接它并最终出现如下异常。

注意:我已尝试连接手机和 PC 等设备,但两者都给出了如下所述的相同错误。

我在某处看到过关于这个问题的地方,他们提到这个问题可能是因为 32 英尺库与我电脑中的蓝牙设备不兼容。

但实际上,我已经在其他一些运行 Windows 7 OS - 64 位的 PC 上测试过这个,并得到相同的错误消息。

有谁帮帮我。 谢谢你。

错误消息:请求的地址在其上下文中无效 ECD09F51114A:0000110100001000800000805f9b34fb

异常详情:

我的代码示例:

Guid uId = new Guid("0000110E-0000-1000-8000-00805f9b34fb");
bool receiverStarted = false;
private List<BluetoothDeviceInfo> deviceList;
private List<string> deviceNames;
private BluetoothDeviceInfo deviceInfo;
private string myPin = "1234";
private BluetoothClient sender;

private void BtnScan_Click(object sender, RoutedEventArgs e)
{
    ScanAvailableDevices();
}
private void ScanAvailableDevices()
{
    lstAvailableDevices.ItemsSource = null;
    lstAvailableDevices.Items.Clear();
    deviceList.Clear();
    deviceNames.Clear();
    Thread senderThread = new Thread(new ThreadStart(Scan));
    senderThread.Start();
}

private void Scan()
{
     UpdateStatus("Starting scan...");
     sender = new BluetoothClient();
     availableDevices = sender.DiscoverDevicesInRange();
     UpdateStatus("Scan completed.");
     UpdateStatus(availableDevices.Length.ToString() + " device(s) discovered");

     foreach(BluetoothDeviceInfo device in availableDevices)
     {
        deviceList.Add(device);
        deviceNames.Add(device.DeviceName);
     }

     UpdateAvailableDevices();  
}

private void UpdateAvailableDevices()
{
    Func<int> devicesDelegate = delegate ()
            {
                lstAvailableDevices.ItemsSource = deviceNames;
                return 0;
            };

            Dispatcher.BeginInvoke((Action)(() =>
            {
                devicesDelegate.Invoke();
            }));
 }

private void PairDevice()
        {
            deviceInfo = deviceList[lstAvailableDevices.SelectedIndex];
            if (CanPair())
            {
                UpdateStatus("Device paired..");
                UpdateStatus("Starting to connect the device");
                Thread senderThread = new Thread(new ThreadStart(SenderConnectThread));
                senderThread.Start();
            }
        }

        private bool CanPair()
        {
            if(!deviceInfo.Authenticated)
            {
                if(!BluetoothSecurity.PairRequest(deviceInfo.DeviceAddress,myPin))
                {
                    return false;
                }
            }
            return true;
        }

private void LstAvailableDevices_MouseDoubleClick(object sender, MouseButtonEventArgs e)
{
    deviceInfo = deviceList[lstAvailableDevices.SelectedIndex];
    UpdateStatus(deviceInfo.DeviceName + " was selected, attempting connect");

    if (CanPair())
    {
        UpdateStatus("Device paired..");
        UpdateStatus("Starting connect thread");
        Thread senderThread = new Thread(new ThreadStart(ClientConnectThread));
        senderThread.Start();
    }
    else
    {
        UpdateStatus("Pair failed");
    }
}

private void ClientConnectThread()
{
    BluetoothClient sender = new BluetoothClient();
    BluetoothAddress address = deviceInfo.DeviceAddress;
    //sender.SetPin(deviceInfo.DeviceAddress, myPin);
    var endPoint = new BluetoothEndPoint(address, uId);
    sender.Connect(endPoint);

    //Another way that I've tried
    BluetoothClient client = new BluetoothClient();
    UpdateStatus("Attempting connect");
    //client.Connect(deviceInfo.DeviceAddress, uId);
    client.BeginConnect(deviceInfo.DeviceAddress, uId, this.BluetoothClientConnectCallback, client);
}

void BluetoothClientConnectCallback(IAsyncResult result)
{
    BluetoothClient senderE = (BluetoothClient)result.AsyncState;
    senderE.EndConnect(result);

    Stream stream = senderE.GetStream();

    while (true)
    {
        while (!ready) ;
        byte[] message = Encoding.ASCII.GetBytes(txtSenderMessage.Text);

        stream.Write(message, 0, message.Length);
    }
}

UWP 中有一些库,您可以在其中轻松地在桌面和其他设备之间建立连接,您可以轻松处理蓝牙适配器。

我也有完全一样的问题。 您如何解决?

32英尺有多个下载

试试这些下载https://github.com/inthehand/32feet

可在“下载”选项卡上获得下载。 NuGet 也提供软件包:-

InTheHand.Devices.Bluetooth - Modern (v4.x) - 预览 NuGet 版本

32feet.NET - 旧版 (v3.x) NuGet 版本

32feet.NET.Phone - Windows Phone NuGet 版本

InTheHand.Devices.Enumeration(Windows 8 / Windows Phone 设备选择器)NuGet 版本

伙计们,我可以使用在不同 PC 上运行的相同应用程序配对和连接它,并将其用作服务器。 早些时候,我在没有在目标 PC 上运行相同的应用程序的情况下尝试过这个,因此它给出了我上面提到的错误。

感谢大家的时间和支持。

暂无
暂无

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

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