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

时间:2019-02-25 14:33:07

标签: c# wpf bluetooth 32feet

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

注意:我已经尝试连接移动设备和PC等设备,但都出现了与下面所述相同的错误。

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

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

有人帮我。谢谢。

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

Exception Details:

我的代码示例:

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);
    }
}

3 个答案:

答案 0 :(得分:1)

UWP中提供了一些库,您可以在其中轻松地在台式机和其他设备之间建立连接,还可以轻松处理蓝牙适配器。

答案 1 :(得分:-1)

32英尺有多个下载

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

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

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

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

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

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

答案 2 :(得分:-2)

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

感谢大家的时间和支持。