B4J Question [PARTLY SOLVED] Possible to connect to 2 OR MORE Bluetooth devices ?

GMan

Well-Known Member
Licensed User
Longtime User
Can i connect via PC-Bluetooth to 2 different BT-devices ?
 

GMan

Well-Known Member
Licensed User
Longtime User
:D I swear ya' know - but i dont know how :rolleyes:
Simply copy the depending routines (anhanced with i.e. 1 to differ) ?

Or using jSerial to make an additional connection through the virtual COM port of the BT device ?
 
Last edited:
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Got it Erel :rolleyes:
Thx for your detailed help - was a little tricky at all.

btw: a third is also possible ? :cool:
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
I'll share the result, maybe anyone can use it, too
And maybe there are things that can be done better :oops:
 

Attachments

  • 2BTs.zip
    5.5 KB · Views: 184
  • 2BTs.png
    2BTs.png
    8 KB · Views: 198
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Thanks a lot - is there ANY restriction with the amount of connections ?

Is there a way to search for all at the same time ?
In my way it causes an error when i start the 2nd search while the 1st is still searching.

Is it generally possible to autoconnect with the last device(s) that was used before ?
 
Last edited:
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Thx, dont need 7 but 4 was great :)
I'm searching the old school way:

B4X:
Private Sub btnSearch_Action
    Dim res As Boolean = bt.StartDiscovery
    If res Then
        searchingForDevices = True
        ListView1.Items.Clear
        UpdateState
    Else
        Log("Error 1 starting discovery")
    End If
End Sub
Private Sub btnSearch2_Action
    Dim res2 As Boolean = bt2.StartDiscovery
    'lblSearchState2.Text = "Suche 2..."
    If res2 Then
        searchingForDevices2 = True
        ListView2.Items.Clear
        UpdateState2
    Else
        Log("Error 2 starting discovery")
    End If
End Sub

Here the depending routines:

B4X:
Private Sub UpdateState
    If bt.IsEnabled = False Then
        lblSearchState.Text = "Bluetooth not available"
        For Each n As Node In MainForm.RootPane
            n.Enabled = False
        Next
    Else
        btnSearch.Enabled = Not(searchingForDevices)
        btnConnect.Enabled = Not(connected) And ListView1.SelectedIndex > -1
        txtInput.Enabled = connected
        btnSend.Enabled = connected
        btnDisconnect.Enabled = connected
        Dim state As String
        If connected Then
            state = "Verbunden 1 " & currentConnection.Name
        Else
            state = "1 Getrennt"
        End If
        lblConnectionState.Text = state
        lblSearchState.Text = "Suche 1..."
        lblSearchState.Visible = searchingForDevices
    End If

End Sub
Private Sub UpdateState2
    If bt2.IsEnabled = False Then
        lblSearchState2.Text = "Bluetooth 2 not available"
        For Each n2 As Node In MainForm.RootPane
            n2.Enabled = False
        Next
    Else
        btnSearch2.Enabled = Not(searchingForDevices2)
        btnConnect2.Enabled = Not(connected2) And ListView2.SelectedIndex > -1
        txtInput2.Enabled = connected2
        btnSend2.Enabled = connected2
        btnDisconnect2.Enabled = connected2
        Dim state2 As String
        If connected2 Then
            state2 = "Verbunden 2 " & currentConnection2.Name
        Else
            state2 = "2 Getrennt"
        End If
        lblConnectionState2.Text = state2
        lblSearchState2.Text = "Suche 2..."
        lblSearchState2.Visible = searchingForDevices2
    End If
End Sub

Is there a way without searching every time (like in B4A) ?
Or -better- to connect the last connected devices (maybe by name)
 
Upvote 0

GMan

Well-Known Member
Licensed User
Longtime User
Ah, right - jBluetooth.
With this lib 2 devices are working well, didnt test the 3rd one yet
 
Upvote 0
Top