You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

VB.NET中使用InTheHand/Feet32蓝牙库遇命名冲突求助

VB.NET 蓝牙设备枚举、选择、连接及数据捕获示例(Windows 10 + VS2022)

歧义错误解决

BC30560错误根源是32Feet.Net与InTheHand.net.bluetooth库存在类命名冲突,两个库都包含InTheHand.Net.Sockets.BluetoothClient类。解决方式:

  • 彻底清理冲突库:保留InTheHand.Net.Bluetooth(4.14版本),卸载32Feet.Net后删除项目目录下的packages文件夹,重新打开项目还原NuGet包。
  • 若需保留双库,声明类时使用程序集限定名明确指定版本(示例见下文)。

前置准备

确保项目已安装InTheHand.Net.Bluetooth NuGet包,且无冲突库残留。


1. 设备枚举与选择

以下代码枚举附近蓝牙设备,弹出选择框让用户挑选目标设备:

Imports InTheHand.Net.Sockets
Imports InTheHand.Net.Bluetooth

Public Class BluetoothDeviceSelector
    Public Shared Function SelectBluetoothDevice() As BluetoothDeviceInfo
        Dim client As New BluetoothClient()
        ' 枚举20台以内的可配对、已连接设备
        Dim devices() As BluetoothDeviceInfo = client.DiscoverDevices(20, True, True, False, False)
        
        If devices.Length = 0 Then
            MessageBox.Show("未发现蓝牙设备")
            Return Nothing
        End If
        
        ' 构建简易选择窗体
        Dim selectorForm As New Form() With {
            .Text = "选择蓝牙设备",
            .Size = New Size(400, 300)
        }
        
        Dim listBox As New ListBox() With {.Dock = DockStyle.Fill}
        For Each dev In devices
            listBox.Items.Add($"{dev.DeviceName} ({dev.DeviceAddress})")
        Next
        
        Dim btnSelect As New Button() With {
            .Text = "选择",
            .Dock = DockStyle.Bottom,
            .DialogResult = DialogResult.OK
        }
        
        selectorForm.Controls.Add(listBox)
        selectorForm.Controls.Add(btnSelect)
        
        If selectorForm.ShowDialog() = DialogResult.OK AndAlso listBox.SelectedIndex >= 0 Then
            Return devices(listBox.SelectedIndex)
        End If
        
        Return Nothing
    End Function
End Class

2. 设备连接与数据捕获

针对SPP(串口协议)蓝牙设备,实现连接和实时数据读取:

Imports System.IO
Imports InTheHand.Net.Sockets

Public Class BluetoothConnection
    Private _client As BluetoothClient
    Private _stream As Stream

    Public Async Function ConnectToDevice(device As BluetoothDeviceInfo) As Task(Of Boolean)
        Try
            ' 标准SPP服务UUID
            Dim sppUuid As Guid = BluetoothService.SerialPort
            _client = New BluetoothClient()
            Await _client.ConnectAsync(device.DeviceAddress, sppUuid)
            
            _stream = _client.GetStream()
            MessageBox.Show("设备连接成功")
            
            ' 启动后台数据读取线程
            Task.Run(AddressOf ReadDataLoop)
            Return True
        Catch ex As Exception
            MessageBox.Show($"连接失败:{ex.Message}")
            Return False
        End Try
    End Function

    Private Sub ReadDataLoop()
        Dim buffer(1023) As Byte
        Dim bytesRead As Integer
        
        While _client.Connected
            Try
                bytesRead = _stream.Read(buffer, 0, buffer.Length)
                If bytesRead > 0 Then
                    Dim receivedData As String = System.Text.Encoding.ASCII.GetString(buffer, 0, bytesRead)
                    ' 跨线程更新UI需用Invoke
                    Me.Invoke(Sub()
                                  txtReceivedData.AppendText($"{DateTime.Now:HH:mm:ss} - {receivedData}{Environment.NewLine}")
                              End Sub)
                End If
            Catch ex As Exception
                MessageBox.Show($"读取数据出错:{ex.Message}")
                Exit While
            End Try
        End While
    End Sub

    Public Sub Disconnect()
        _stream?.Close()
        _client?.Close()
        MessageBox.Show("已断开连接")
    End Sub
End Class

3. 主窗体调用示例

Public Class MainForm
    Private _bluetoothConn As New BluetoothConnection()

    Private Async Sub btnSelectDevice_Click(sender As Object, e As EventArgs) Handles btnSelectDevice.Click
        Dim selectedDevice = BluetoothDeviceSelector.SelectBluetoothDevice()
        If selectedDevice IsNot Nothing Then
            Await _bluetoothConn.ConnectToDevice(selectedDevice)
        End If
    End Sub

    Private Sub btnDisconnect_Click(sender As Object, e As EventArgs) Handles btnDisconnect.Click
        _bluetoothConn.Disconnect()
    End Sub
End Class

双库共存时的歧义处理

若必须保留两个库,声明BluetoothClient时使用程序集限定名:

' 指定使用InTheHand.Net.Bluetooth 4.14版本的类
Dim client As New InTheHand.Net.Sockets.BluetoothClient("InTheHand.Net.Bluetooth, Version=4.14.0.0, Culture=neutral, PublicKeyToken=e60f486d3d65678b")

内容的提问来源于stack exchange,提问作者Alan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.18 08:20:06