Is it possible to connect to multiple bluetooth modules ??

tolisn

Member
Licensed User
Longtime User
Hi.
I want to startup in home automation using my nexus 7 tablet.
I have 3 bluetooth modules ( bluesmirf from sparkfun) and what to be able to connect to all of them so I can send specific commands from my tablet to each one.
Each module has a specific Mac address and a Spesific name.
Has anyone done this sort of project and how ?
Any ideas:sign0085: ?
 

Beja

Expert
Licensed User
Longtime User
This is what I have been looking for since long time.. some bullet answers didn't help much.
I hope Erel or another fellow member can come up with an example with a spinner of a number of Mac addresses to chose from then pair and connect to the selected one. I know my Mac addresses so NO Need for the search function at all.
A dream.
 
Upvote 0

Beja

Expert
Licensed User
Longtime User
If Erel and the B4A team took the hardship and long march to present us with this wonderful tool, then it is only fair that we read the documentaion they generously provided for us.
But it is human nature to start with a walker!! and then 4-wheel bike, and that's why all languages start with a complete, self-contained and ready to compile hello world example. When I end this long trip I will go to the nearest Kinko's, opps FedEx and print out all of docs. for me it is much easier and comfortable to brows hardcopy user guide than reading it online.
But until then I hope Erel considers a little detailed replies..
Examples for Dummies!
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
What you are asking doesn't fit a simple example. It is a complete project.

There is no difference between connecting to one device or multiple devices. Start with connecting to the first device. Then you should create another Serial object and create to the second device.

If you have any specific question then I will be happy to help...
 
Upvote 0

tolisn

Member
Licensed User
Longtime User
What you are asking doesn't fit a simple example. It is a complete project.

There is no difference between connecting to one device or multiple devices. Start with connecting to the first device. Then you should create another Serial object and create to the second device.

If you have any specific question then I will be happy to help...

So does this mean that you can connect the android device to 2 bluetooth modules at the same time ?

I have seen somewhere in the documentation that the serial object can handle only one connection at a time
 
Upvote 0

tolisn

Member
Licensed User
Longtime User
You need one Serial object and one AsyncStreams object for each connection. Your program can include any number of these objects.

I did this in my code

B4X:
Sub Process_Globals
   Dim AStream As AsyncStreams
        Dim AStream2 As AsyncStreams
End Sub

B4X:
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("2")

If AStream.IsInitialized = False Then      
AStream.Initialize(Main.Serial1.InputStream, Main.Serial1.OutputStream, "AStreams") 

AStream2.Initialize(Main.Serial2.InputStream, Main.Serial2.OutputStream, "Astreams2")

End If

End Sub

but the code always pauses in the second AStream2.initialize
What could be the problem?


Of course I have also done this

B4X:
Sub Process_Globals
   Dim admin As BluetoothAdmin
   Dim serial1 As Serial
   Dim serial2 As Serial
   Dim foundDevices As List
   Type NameAndMac (Name As String, Mac As String)
   Dim connectedDevice As NameAndMac
End Sub


and this

B4X:
Sub Activity_Create(FirstTime As Boolean)
   If FirstTime Then
      admin.Initialize("admin")
      serial1.Initialize("serial1")
      serial2.Initialize("serial2")
   End If
   Activity.LoadLayout("1")
End Sub
 
Upvote 0

tolisn

Member
Licensed User
Longtime User
I got the below error message


AStream2.Initialize(Main.Serial2.InputStream, Main.Serial2.OutputStream, "AStreams2")
java.lang.NullPointerException



Sometimes the program runs OK and the connection to the 2 bluetooth modules is made without any errors but most of the time I get the above error message and the program stops.
 
Upvote 0

tolisn

Member
Licensed User
Longtime User
Hi Erel.

Attached are the project files as well as the log.txt file.:sign0085:
 

Attachments

  • Bluetooth V1.05.zip
    377.9 KB · Views: 472
  • log.zip
    7.9 KB · Views: 358
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Note that you should use File - Export as zip when uploading files.

It seems like you start the second activity before both streams were connected. You cannot initialize AsyncStreams before the stream is connected.

I recommend you to initialize AsyncStreams in Serial_Connected event. I also recommend you to move all the communication code to one service. It will be simpler.
 
Upvote 0

tolisn

Member
Licensed User
Longtime User
Hi.
I'm trying to do what you said but since I'm not a programmer I'm having a very hard time understanding your suggestions.
Is it possible to point me to the correct direction?
Sorry for all the questions but it's been 2 days now that I'm trying this but cannot make it work. I do not know what else to do.
 
Upvote 0
Top