Android Question Multiple devices on bluetooth?

techknight

Well-Known Member
Licensed User
Longtime User
Using the Bluetooth chat tutorial, I was able to get 2 devices talking to each other no problems.

But, I want to connect 3 devices together. Basically, there is a master device, and 2 remotes. So Device Number 1 gets chat data from Remote 1, and Remote 2.

Connecting Remote 1, works perfectly as expected. But if I define a second serial and make a serial2.listen, the 2nd device still connects through serial1! and it doesnt work.

any ideas?
 

techknight

Well-Known Member
Licensed User
Longtime User
Yes, I tried to use 2, and I tried to use a single. Didnt make a difference. The connection happens, but no data gets through. The first connection works fine. 2nd connection never does while the first connection is active.

The issue appears to be the connection wants to route through the First serial.listen, which in this case is Remote1. First Device connects, and then the second device wants to connect to the same listener instead of the 2nd listener.

Here is my code:

B4X:
Sub cmdDiscover_click
    'this intent makes the device discoverable For 300 seconds.
    Dim i As Intent
    i.Initialize("android.bluetooth.adapter.action.REQUEST_DISCOVERABLE", "")
    i.PutExtra("android.bluetooth.adapter.extra.DISCOVERABLE_DURATION", 300)
    StartActivity(i)
    Remote1.Listen
    Remote2.Listen
End Sub
'=========================================================================================
'REMOTE CONTROL SUBROUTINES
'=========================================================================================
Sub Remote1_Connected (Success As Boolean)
    Log("Remote 1 connected: " & Success)
    If Success = True Then
        If AstreamRemote.IsInitialized = False Then
            AstreamRemote.InitializePrefix(Remote1.InputStream, True, Remote1.OutputStream, "AStreamRemote")
        End If
    Else
        Log("Connection Broke")
    End If
End Sub
Sub Remote2_Connected (Success As Boolean)
    Log("Remote 2 connected: " & Success)
    If Success = True Then
        If AstreamRemote2.IsInitialized = False Then
            AstreamRemote2.InitializePrefix(Remote2.InputStream, True, Remote2.OutputStream, "AStreamRemote2")
        End If
    Else
        Log("Connection Broke")
    End If
End Sub
Sub AStreamRemote_NewData (Buffer() As Byte)
    Log("Remote 1: " & BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

Sub AStreamRemote_Error
    ToastMessageShow("Remote 1 has Disconnected.", True)
    AstreamRemote.Close
    Remote1.Disconnect
End Sub

Sub AStreamRemote_Terminated
    AStreamRemote_Error
End Sub

Sub AStreamRemote2_NewData (Buffer() As Byte)
    Log("Remote 2: " & BytesToString(Buffer, 0, Buffer.Length, "UTF8"))
End Sub

Sub AStreamRemote2_Error
    ToastMessageShow("Remote 2 has Disconnected.", True)
    AstreamRemote2.Close
    Remote2.Disconnect
End Sub

Sub AStreamRemote2_Terminated
    AStreamRemote2_Error
End Sub
 
Upvote 0
Top