Check Peter's library.What are the other possibility?
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
[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:
B4X: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
Yes, it happens to me too.I got a script shell error: timeout ?
#Region Project Attributes
#MainFormWidth: 600
#MainFormHeight: 600
#End Region
Sub Process_Globals
Private fx As JFX
Private MainForm As Form
Private xui As XUI
Private Button1 As B4XView
Private sh As Shell
End Sub
Sub AppStart (Form1 As Form, Args() As String)
MainForm = Form1
MainForm.RootPane.LoadLayout("Layout1")
MainForm.Show
' Start searching for the Bluetooth port
SearchBluetoothPort
End Sub
Sub Button1_Click
xui.MsgboxAsync("Hello World!", "B4X")
End Sub
Sub SearchBluetoothPort
' We use the PowerShell pipeline without complex WQL queries: filtering PnP objects directly.
Dim psCommand As String = "Get-WmiObject Win32_PnPEntity | Where-Object { $_.Name -like '*Bluetooth*' } | Select-Object DeviceID, Name"
sh.InitializeDoNotHandleQuotes("sh", "powershell", Array As String("-Command", psCommand))
sh.Run(10000)
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
psCommand and InitializeDoNotHandleQuotes.you changed just the "dim pscommand"?, but I still have a timeout error with your new script ...
With jSerial Enhanced Library:May be I made a wrong reading, but to get the port name it must be open. So open the port means we already know its name ...
Private Serial As jSerialEnhanced
Log(Serial.ListPorts)
For Each port As String In Serial.ListPorts
Dim PortInfo As Map = Serial.GetPortInfo(port)
Log("Port Name: " & PortInfo.Get("PortName"))
Log("Port Description: " & PortInfo.Get("PortDescription"))
Log("Descriptive Port: " & PortInfo.Get("DescriptivePort"))
Log("VID: " & PortInfo.Get("VID"))
Log("PID: " & PortInfo.Get("PID"))
Log("Manufacturer: " & PortInfo.Get("Manufacturer"))
Log("Serial Number: " & PortInfo.Get("SerialNumber"))
Log("Product: " & PortInfo.Get("Product"))
Next
I didn't need to open the ports.(ArrayList) [COM1, COM3]
Port Name: COM1
Port Description: Communications Port (COM1)
Descriptive Port: Communications Port (COM1)
VID: -1
PID: -1
Manufacturer: (Standard port types)
Serial Number: Unknown
Product: null
Port Name: COM3
Port Description: US232R
Descriptive Port: USB Serial Port (COM3)
VID: 1027
PID: 24577
Manufacturer: FTDI
Serial Number: FTU93OAW
Product: null
So what do you get?it seems not ...
Public Sub DetecterPortBluetooth
' Commande PowerShell pour lister les ports COM avec leur description
Dim Query As String = "Get-CimInstance Win32_SerialPort | Select-Object DeviceID, Name | Format-Table -HideTableHeaders"
sh.Initialize("powershell", "powershell.exe", Array As String("-NoProfile", "-Command", Query))
sh.Run(-1) ' -1 pour aucun timeout
End Sub
Private Sub powershell_ProcessCompleted (Success As Boolean, ExitCode As Int, StdOut As String, StdErr As String)
If Success And ExitCode = 0 Then
' Analyse de la chaîne reçue
Dim Lines() As String = Regex.Split(CRLF, StdOut)
For Each Line As String In Lines
Line = Line.Trim
' Utilisation correcte de .ToLowerCase en B4X
If Line.ToLowerCase.Contains("bluetooth") Then
Dim Matcher1 As Matcher = Regex.Matcher("COM\d+", Line)
If Matcher1.Find Then
Dim PortTrouve As String = Matcher1.Match
Log("Port Bluetooth détecté avec succès : " & PortTrouve)
Return
End If
End If
Next
Log("Aucun port série Bluetooth trouvé dans le système.")
Else
Log("Erreur lors de l'exécution du Shell : " & StdErr)
End If
End Sub
Private sh As Shell
DetecterPortBluetooth
Port Bluetooth détecté avec succès : COM5