- 1 year ago
How to list all Bluetooth devices in Visual Basic (vb.net) on WINDOWS
To list all Bluetooth devices in Visual Basic.NET (VB.NET) on Windows, you can use the System.Net.Sockets namespace along with the Bluetooth API provided by Windows. Here's an example code snippet that demonstrates how to achieve this:
vbImports System.Net.Sockets
Imports System.Runtime.InteropServices
Imports InTheHand.Net.Bluetooth
Public Class MainForm
Private Sub ListBluetoothDevices()
Try
Dim bluetoothClient As New BluetoothClient()
Dim bluetoothDevices As BluetoothDeviceInfo() = bluetoothClient.DiscoverDevices()
For Each device As BluetoothDeviceInfo In bluetoothDevices
Console.WriteLine("Device Name: " & device.DeviceName)
Console.WriteLine("Device Address: " & device.DeviceAddress)
Console.WriteLine()
Next
Catch ex As Exception
Console.WriteLine("Error: " & ex.Message)
End Try
End Sub
Private Sub MainForm_Load(sender As Object, e As EventArgs) Handles MyBase.Load
ListBluetoothDevices()
End Sub
End Class
Make sure to add the following references to your VB.NET project:
- Right-click on your project in the Solution Explorer and select "Add Reference."
- Under the ".NET" tab, locate and select "InTheHand.Net.Personal.dll" to add the Bluetooth library reference.
- Click "OK" to add the reference.
The code creates a BluetoothClient
object and uses its DiscoverDevices()
method to retrieve an array of BluetoothDeviceInfo
objects representing the Bluetooth devices available. The DeviceName
property retrieves the name of each device, and the DeviceAddress
property retrieves the address of each device. The code then prints out the device information.
Please note that the code uses the InTheHand.Net.Bluetooth library, which provides a managed wrapper around the Bluetooth API in Windows. You can install the InTheHand.Net.Personal NuGet package to add this library to your project.
Remember to handle any exceptions that may occur during the Bluetooth device discovery process.