Disconnect and reconnect multiple Bluetooth clients

Hi, hi!

I'm making an app that connects 3 phones to 1 via Bluetooth: the 3 phones (with 1 bluetooth client each) working as separated screens controlled by the other phone (with 3 bluetooth servers).
They connect and everything works fine, the problem comes when I want to disconnect one of them and then reconnect it. It just doesn't want to work correctly.

I'd like to explain what happens with an example:

Imagine that the phone with the bluetooth servers has 3 boxes (3 Bluetooth servers), and the ones with bluetooth clients are melons (or whatever fruit you want). When I put a melon in box 1 and another one in box 2, it works fine, but if I want to remove the melon from box 1 and then put another melon (or the same one) back in, it goes into box 3 (or any empty one), detecting as if there was still a melon in box 1. I want to be able to empty my boxes correctly.

I've tried disconnecting the bluetooth connections from the clients and the server, stopping accepting connections from the server and accepting them again.

Controller (Server) [There's one for each server]

Screen (Client)

It uhh... Yeah, it doesn't do anything.
The only way I find that works is to reset the screen, but that "empties all my boxes" instead of the one I want.

Hello Sonjey

Tricky! From your description, it sounds like the system cannot differentiate between your server instances. My gut feeling is you should use only one server. If nothing else, there will be conflicts between servers if more than one client sends data at the same time.

Bluetooth disconnection is not guaranteed.

The clients connect correctly to the separate servers, because they can send and receive messages from their own server (to be fair, I have no idea how or why, but it just works).
The problem, more or less, is that the server "retains" connections. Rather, it doesn't free up connection space after disconnecting.
:package: :melon:

I see - your description suggests that they don't:

When disconnecting a Server, don't use a Block that attempts to connect it. Try this:

blocks

I think disconnection might not be as instantaneous as one would expect.

I tried disconnecting the server, waiting a bit and reconnecting it, but nothing.

And sorry, I'm not the best at explaining, I'll try this way:

I have 3 servers. If I connect a client, it connects to the first free server it finds and only interacts with that one. If I connect another client it connects to the next free server. I need a way to free up a server connection space without disconnecting the others.

..........but if you have 3 servers and 3 clients, why not dedicate each?

Edit: Also, in normal circumstances, you should not need to disconnect a server until the App is about to be closed. So we need to know what is really being sent by the Clients and what your App actually does with what has been sent - that is probably where the issue really is.

Upload your Project .aia files for Server and Clients - I may not be available soon but other Power Users may take-up the reins.

1 Like

Dear @Sonjey,
as far as I've understood, your app has three real screens, each one of them is controlling one different phone: is it correct ?
So each phone is a server and the app features three clients, one for each screen.
If this assumption is wrong, you can discard the rest of my answer :fearful:

Pplease bear in mind the the server is the device which sends data, typically a sensor, but in your case a phone, while the client is the one that receives the data, in your case the app with three screens (= three clients).

Wording apart, what you say is that it seems that the disconnection doesn't work properly. My suggestion, in this case, is to do a step backwards: make a simple app, with only one client-server structure and verify how the disconnection activity works, and how many seconds it takes to effectively disconnect itself To this purpose you can setup a clock that, for example every second, verify the disconnection, after having pressed a button for disconnecting. (as @ChrisWard has said, the disconnection takes time).
Please also be aware that by working with multiple screens in AI2 very often you'll have problems with BT, when switching between screens, In addition to this, also the switching between screens must be done with the correct sequance of blocks in order to free the memory of the system, which, on the contrary, can be replenished soon.
To avoid these problem, typically we suggest to use Visrtual Screens instead.
To this purpose you can have a look to a very recent post, here:

... where you'll find the instruction on how to create and manage virtual screens, and how to correctly switch back & forth between real screens (by @Anke ).
Best wishes.

Does it work if you only use one client and server?
See also

Taifun

1 Like

Before reading all this, check the last line first.

Okay, I'm so sorry, I'll try to explain it clearly, without any melons or other weird example, explaining what the app does and with some screenshots (I should have started there).

It's all one app with 3 screens. It works by sending a URL from one phone to another, where it is projected in a web viewer. That works fine for now, but I want to be able to disconnect and reconnect them, which is what's causing the problem.

Screen number 1: Selector
Captura de pantalla 2025-04-03 093032
^ Here you select whether the device will act as a controller or as a display (To differentiate between screen and screen).


It also asks for connection permissions and such on this screen



Screen number 2: Controller
Only one of the phones should be the controller. It doesn't matter which one.
It has 3 bluetooth servers.

And yes, I just tried that and it works, with only 1 server it reconnects correctly (But I need to connect multiple devices).


^ Here we choose the number of devices we want to connect (purely visual, there are always 3 available, that's not the problem right now), and we manage them from here. Spinners are used to select a web page to project on the display web viewer.

^ First we click on the button above that says "Permitir Conexiones" (Accept Connections), this accepts connections on all 3 Bluetooth servers.


^ When we select an option in the spinner, a URL is sent to the display.
There are 3 of these blocks (1 per spinner and server).


^ Here we have a .Timer checking every second if the screen has been connected. If so, it changes a label to "connected" (text it receives from the display), if not, the label says "Disconnected".
Again, there are 3 of these blocks (Although it can work just fine by putting the 3 If-else blocks in a single Timer, right now I have 3 different Timers for testing stuff).

I also plan to add that when you disconnect from the screen, the server will also disconnect.

Controller Block: Go back, Disconnect and Reconnect
^ We have the Back button to disconnect all the servers and return to the selector screen (not visible in the screenshot, but it's there), a button to disconnect the server and stop accepting connections, and another to accept them again. At the moment there's only 1 of these last two for testing, which only disconnects server 1.



And uh... Yeah, It just fixed itself. Like just now. And I don't know why.
Gotta love programming.

^ I think the problem was that the .StopAccepting block is unnecessary, but it could have been anything, really.

1 Like

Don't count your chickens before they are hatched.

If you swap screens, the current BT connection is 'lost', but it is still lurking in the allocated memory, waiting to cause a problem at an inconvenient moment. This can happen because each screen occupies it's own memory.

The solution is to use Virtual Screens,
When we define virtual screens, we use one 'real' App Inventor Screen (most often Screen1). Screen-sized Vertical Arrangements on it are displayed/hidden as required - they are the Virtual Screens. This is generally a better approach for multi-screen Apps, they share data without having to "pass" it between screens and it also reduces code duplication, making the App more efficient and the code easier to follow if you have to return to it at a later date.

So, instead of separate "houses", virtual screens are "rooms" of the same "house".

1 Like

As @ChrisWard has said, ease your the life by using virtual screens :grin:
To this purpose please find annexed an example.
Hoping it might help for your next app.
Cheers.

This is for 8" (or larger screens) pads
MultipleScreen.aia (849.0 KB)

This is screen optimized for phones:
MultipleScreenLittle.aia (849.0 KB)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.