Multiple Bluetooth connection in one screen

Hi all,
Can we add multiple Bluetooth devices in one screen?
If possible how can we do that.

Yes I have done that for 10 channels running on two Bluetooth devices running asynchronously. An aia file for the project is attached and the first few blocks on Screen1 mainly relate to the set up for two BLE blocks. If you look at the following procedures in Screen1 in the example that may help.

TwoBLE.aia (274.7 KB)

  • When ButtonStartScan
  • When BluetoothLE1 DeviceFound
  • When BluetoothLE2 DeviceFound
  • When BluetoothLE1 DeviceConnected
  • When BluetoothLE2 DeviceConnected
  • When BluetoothLE1 DeviceDisconnected
  • When BluetoothLE2 DeviceDisconnected
  • When BluetoothLE1 StringsReceived
  • When BluetoothLE2 StringsReceived
  • When ButtonRun
  • When ClockMainLoop

Start scan
Starts up two Bluetooth low energy channels. The two channels run in parallel. and each receives a continual stream of data from its sensors. The data are strings containing three hexadecimal numbers representing the sensor measurements ax, ay, az. Each channel extracts and stores the individual sensor values as floating-point decimal numbers.The first time the Bluetooth set of procedures runs it will request your permission to use the Bluetooth capability in your phone. After the BLE connections have been made a separate ButtonRun is used when needed to to trigger downloading and processing the data.

Note the two quite different separate methods used to download data. One method triggered by ButtonRun registers each BLE for continuous download of high speed data. The other runs in the ClockMainLoop procedure, using a read BLE request for much slower data streams.

Note also the Characteristic and Service UUID’s are at the bottom of the Screen1 listing.

When ButtonRun

  • Checks if the Bluetooth channels are connected and if so, registers each Bluetooth channel to receive accelerometer data continually from the telescope gravity sensors.
  • Reads the calibration factor file
  • Calls a block that starts the main clock running to calculate and display the sensor values and to manage an auxiliary timer for reading other sensor values at regular intervals.

When ClockMainLoop
One of this procedures functions is to use a read BLE request for much slower data streams.

Hope some of this helps, Regards, Peter

5 Likes

TwoBleV2.aia (189.3 KB)
DeclinationSensorArduinoCode.txt (5.4 KB)
HourAngleSensorArduinoCode.txt (5.4 KB)
Here is a second version of the TwoBle demonstrator. It has all the non-relevant project material removed for clarity leaving just the two BLE channels and a display of the measurements transmitted from each of the sensors. I’ve attached the two sensor programs as an example of how the sensors might work with the App.

Worth noting that

  • The two sensors being read are called respectively IMUA and IMUB and these names are used to identify the BLEs when scanning.
  • In the continuous download method the measurements are asynchronous and are a sample of measurements from a high speed source. Whereas in the ‘read’ method each measurement sent is read albeit at a lower rate. The performance can sometimes be improved by prefiltering the data in the remote sensors. In these examples I’m prefiltering and transmitting from each sensor.
  • When scanning you may have to wait for a while until the BLE channel identifies the requested BLE, depending on how many other BLE transceivers are operating nearby.
  • You may need to check if the request for permission has switched off the phone BLE transceiver and if so switch it on again. But this only needs to be done once when the App is installed.
1 Like

(added to BLE FAQ)

1 Like

Here are two simplified examples for reading two Bluetooth sensor peripherals on one screen.

The version I posted earlier showed a phone App that received data from two Bluetooth sensor peripherals using a combination of registration and polling. The first example given here just uses registration, the second just uses polling. It should be possible to expand each of these to more than two sensors though I haven’t tried this - see notes attached to blocks.

TwoBleRegistered.aia (189.2 KB)
TwoBlePolled.aia (188.4 KB)

Registration is used for data that is transmitted intermittently by the sensors. It can save energy and can be faster than polling, but the sensor data rate and the phone app have to be set up so that the incoming data doesn’t interfere with the phone App. With polling it is more straightforward to sample the data at a rate that can be managed by the phone App.

Both examples include data compression in which three measurements are sent in one string using signed hexadecimal values. The data is extracted and converted to separate decimal values. Likewise both examples include a filter that is switched on for small changes in the data values. This allows a rapid response to large data changes and a more precise response for small changes. The filter can be switched off by setting the threshold variable to zero or fully on by setting it to the maximum signal value.

Both examples work with sensors set up using the Arduino code text files attached below. The hardware used were the accelerometers and BLE interfaces on two CurieNano boards, They should also work on Genuino101/Arduino101 boards or clones. It typically takes from 7 to 30 seconds for the app to scan and connect to both sensors.

IMUA.txt (5.4 KB)
IMUB.txt (5.4 KB)

Hope this helps, Peter.

2 Likes

@ABG
Hi ABG, I've just seen your revised comment "PeterO BLE app freezes then crashes - registered too many times" As far as I can see I only register for updates once for each device. Could you describe the operation that caused the crash so I can fix this issue. I had revised the App to read data rather than register for updates as there's no difference in performance and read should use less energy but I'd still like to fix the earlier version if its faulty.

Any advice appreciated, Thanks Peter

Thanks ABG for adding my example to the BLE FAQ section. I've recently split it into two simpler apps: one that reads data just by polling the two BLE devices and the other that reads data just using the registration method. Is it OK if I modify the FAQ entry to include these two examples? I've shown it above as a separate post to the original thread.

Thanks Peter

1 Like

Dear Sir,
Thank you for your guidance and codes. It really helped me to understand and design my blocks as per the requirement.

Thank you for your descriptive reply. It worked and I am able to connect with 5 BT devices simultaneously.

Hi Rajkamal,

Very good to hear of your success and that the notes/aia codes helped.

Regards, Peter

Is there a way to use bluetooth with multiscreen without disconnecting the bluetooth after switching between screens ?

Nicely done! Reading that I guess it is possible to make a kind of BT serial repeater that acts on one side as device and on other as master and then connect between two actual devices? Am I correct? You perhaps tried something like that?

Thanks for the feedback. The project I was running has the master running on an Android phone and two devices that each transmit accelerometer data independently of each other. There are two versions of my App one where the devices transmit continuously and asynchronously and the master registers each of them for updated data and accepts information from each of them in turn. The other method issues a read request to each device in turn and the devices only transmit after receiving a read request.

@OptimPro_sp - I haven't tried the modification you suggest as I integrate all data from the two devices in the Android 'Master' app. I'd suggest continuing this strategy using the 'Master' Android phone app as a data processing unit that controls and processes all data coming or going to either of the subsidiary devices. In that sense it could provide a connection between the two devices - is that the idea you were suggesting? A direct connection between the two devices as well as to the Master Android Phone is a radically different network that could also work but would require a substantial rewrite of the Bluetooth section in my App.

Hope that helps

1 Like

Hello Seif

I was looking through this Topic and noticed that nobody answered you - sorry about that. So, probably too late but the answer to your question is yes. The method is to build the App with 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 seperate houses, virtual screens are rooms of the same house. Since the Virtual Screens are all on one 'real' Screen, the Bluetooth connection is not lost when switching between them.

1 Like

Sorry this comes so late Taamalli I didn't realise the thread was still open. To answer your question - yes the app has four separate pages that exchange data without interrupting the Bluetooth connection and without hanging up.

However, there is an important trick when using multiple pages to avoid the system hanging up or running so hard that it overheats the phone. When exiting a page it should be closed and you should return to the main page before going to another page. Otherwise you will be opening additional copies of the page and programs risk failing.

I've attached an App that shows an approach to transferring data between multiple pages that relies on returning to the main page when leaving a subordinate page. Best to focus on the initialisation blocks in each page to get an idea of how multiple pages can be used.

The next few blocks on the main page show a method for reading two separate Bluetooth sensor devices and unpacking their hexadecimal data strings. The remaining blocks on the main page are only relevant to amateur astronomers.

The additional pages are for calibration and for an editable data base but their relevance to your question is just the screen button and the first few initialisation blocks in each page.

PushTo42.aia (354.2 KB)

2 Likes

Hi Chris,

Thanks for that option, While you were typing I was also replying with a description of how to use multiple pages without shutting down the connection.

I used a combination of virtual and multiple pages because it was the only way I could keep the blocks easily readable - partly due to the complexity of the program but also because the bigger a single page gets the slower it is to edit until it can become impossible to edit.

I went to investigate your PushTo42.aia file, and the download link in your post was broken.

Could you fix it?

(I am curious.)

Hi ABG,

Sorry about the broken link and it's fixed now. I'll Look forward to hearing any comments or suggestions for upgrade that you may have.

Regarding your earlier comment, the main page uses read operations instead of registration and has been used regularly for the last year without freezing or crashing. Likewise I didn't experience freezing or crashing with the original version that used registration but have stopped using that method.

I worked on that and when I done searching I found out only few devices support such kind of swtiching mode. So for now I put that on a side.

Yes I want to make a simple logger that would 'sit' between master and client which is serial port basicaly.

Thank you for reply.

Br Alex

Hi Alex,

Thank you for your enquiry. The example transmits several channels of data from several microcontrollers via Bluetooth to an Android mobile phone. Is it the mobile phone or the microcontroller that doesn't give you the support you need? Could you give a bit more detail on the data you are trying to transmit and the equipment you have tried?

I've used microcontrollers including the Arduino Nano BLE 33 sense, the Genuino 101 BLE and the DF Robot CurieNano. The mobile phones I've tried are both very basic Android units: one is a Samsung Galaxy Core Prime running Android Prime 5.1.1 and the other a Nokia 2.1 running Android Go later upgraded to Android 10. Each of these units had all that was necessary to set up and run the multiple Bluetooth App.

Hope this helps, Regards, Peter

1 Like

I know things could be easy done with microcontrollers (I used to work with microchip and texas a lot this one is mine https://bluetraker.com/products/vms/ :slight_smile: )

Let me explain more detailed.

I have a main phone that connects to BT serial device. I want to put a 2nd phone between those two in a way that main phone would communicate to that phone and that to BT serial port while logging data between them. I did not put to much effort into that as not important, more like a play. But I did found out that when I switch from client to master connection to the other is lost. I used BT client and BT server.

Best regards, Alex