[Gemini - I haven't tested it]
On B4J (which runs on Java), the main problem is that standard serial libraries (like the jSerialor library jSerialComm) are able to return the list of
all active COM ports on the PC (e.g. COM1, COM3, COM5), but
they do not natively specify which of these is associated with a Bluetooth device.
To brilliantly solve the issue on
Windows systems (the most common scenario for this type of needs on PC), the best way is not to rely on complex Bluetooth Java libraries (often obsolete or cumbersome like BlueCove), but rather to use the
ShellB4J object to directly query the operating system via
PowerShell or
WMI ( Win32_SerialPort).
Solution in B4J with PowerShell
You can run a lightweight PowerShell script that filters serial ports for the word
"Bluetooth" (such as
"Standard Serial over Bluetooth link" ).
Here is an example of ready-to-use B4J code:
Sub Process_Globals
Private MainForm As Form
Private sh As Shell
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Main") ' Your potential layout
MainForm.Show
' Start searching for the Bluetooth port
SearchBluetoothPort
End Sub
Sub SearchBluetoothPort
Dim psCommand As String = "Get-CimInstance Win32_SerialPort | Where-Object { $_.Description -like '*Bluetooth*' } | Select-Object DeviceID, Description"
sh.Initialize("sh", "powershell", Array As String("-Command", psCommand))
sh.Run(10000) ' 10 seconds timeout
End Sub
Sub sh_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success And ExitCode = 0 Then
If StdOut.Trim.Length > 0 Then
Log("Bluetooth port(s) found:")
Log(StdOut)
' Example: Extract the port (e.g., COM3) to use with jSerial
Else
Log("No active Bluetooth serial port found.")
End If
Else
Log("Error executing command: " & StdErr)
End If
End Sub