B4J Question Any equivalent to B4A Serial.GetPairedDevices?

udg

Expert
Licensed User
Longtime User
Hi all,
I use the following code to discover paired devices in a given list of candidates (device must be in it otherwise terminate the sw), then connect to them.
This works nice in B4A but jSerial for B4J misses a few functions relative to lib Serial for B4A.
So the question is: which is the alternative path to follow when compiling for B4J? TIA

B4X:
Public Sub Connect As ResumableSub
    Dim PairedDevices As Map = Serial1.GetPairedDevices
    Dim trovato As Boolean = False
    For Each k As String In KPrinters                           <-- list of authorized printers
        If PairedDevices.ContainsKey(k) Then
            Log("Test Paired: "& k)
            Serial1.Connect(PairedDevices.Get(k))        <- Connect doesn't exist in jSerial
            wait for Serial1_Connected(res As Boolean)
            If res Then
                trovato = True
                PrnName = k
                Exit
            End If
        End If
    Next
    If trovato Then
        Log("Found printer: "& PrnName)
        Astream.Initialize(Serial1.InputStream, Serial1.OutputStream, "astream")
        Connected = True
        ConnectedError = ""
        Serial1.Listen
    Else
        Log("No authorized printer found or connection to printer failed")
        Connected = False
        ConnectedError = LastException.Message
    End If
    If SubExists(CallBack, EventName & "_Connected") Then
        CallSub2(CallBack, EventName & "_Connected", trovato)
    End If
    
    Return True
end Sub

Should I look for Bluetooth/BLE libs? Or should I go the USB way when dealing with B4j/Windows?
If the latter, would it work with Windows XP (to reuse a very old small-factor PC)?
 

udg

Expert
Licensed User
Longtime User
Thank you @Erel.
I agree about jSerial being dedicated to what its name implies, serial ports.
So, out of curiosity, why B4A Serial extends to paired devices? Just historical reasons, OS features or anything else?

Have a nice day

Edit: meanwhile I found this. Could it be an alternative closer to what I have in B4A?
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
So, out of curiosity, why B4A Serial extends to paired devices? Just historical reasons, OS features or anything else?
The underlying API and features are completely different. B4A Serial is built specifically for Bluetooth SPP connections.

jBluetooth will list all paired devices after you call StartDiscovery. The downside of jBluetooth is that it doesn't work with all types of Bluetooth drivers.
 
Upvote 0
Top