Background bluetooth auto connect

Hello to everyone this is my first time using this forum so please be gentle with me if I broke a rule or two , now let’s come to the topic I was creating an application for the device which uses bluetooth connection to turn / off the other devices connected to it. I created that thing no problem in that and it worked fine but a problem that rose is that whenever my phone goes out of the range of the device’s bluetooth range the app doesn’t auto connect to the phone. I tried several things but they didn’t worked out can someone help me ??
Please help.

Hi Anshumaan

So what you actually mean is, when your phone is out of Bluetooth range, the App loses connection to the device and when back in range, it does not reconnect.

What BT are you using, Classic or BLE? If BLE, the next edition of the MIT BLE extension has a solution for your issue. If using Classic, you will have to alert the User about the loss of connection (suggest a sound) and unfortunately they will have to repeat the connection process.

If this is going to happen too often, then either a hardware solution (an intermediary device for example) or a change of comms (WiFi?) could be considered…

1 Like

Thanks for replying.I am currently using a classic bluetooth module (HC-05). I want to inform you that my app auto - connects to the already specified bluetooth address when screen is initialized so I thought maybe I could run a timer and run that auto connect process whenever the app loses connection this means that whenever the app loses connection it could periodically attempt to connect to that specified address.

I think this problem is not of any interest to anyone comeone this must have happened to someone I am this close to creating a perfect app can some one help me ??Any help will be highly appreciated!

  • App Inventor 2 Projects do not and can not run in the Background.

  • Much of what has been discussed about lost BlueTooth connections in the AI2 Forums is here . The posts might contain advice that could help you.

  • Here are some more

  • You have done a lot but you have not shared an image of the Blocks you use or posted an aia of your app ( or a small example showing what you try and what happens).

So, perhaps share an example aia and explain in detail what someone has to do to get the response you see and show how you tried to use your Clock? You might get specific advice in that case.

1 Like

Oh.That's somewhat shocking I didn't knew about it. The bluetooth remains connected to the phone when the app is in the ram (until you clear phone memory)the app remains connected and I tend to use that condition.
This is what I observed about the condition and what I want :-

  1. I noticed that the app remains connected if I switch to some other app in my phone , this means that the app is being out of sight but is still running.
  2. I intend to use that period that if in that period the connection gets lost I could attempt to reconnect the phone to my bluetooth module.
    Thanks for replying to my post.
    This is what I have done to achieve the above thing:-

...running but not responding probably. An example: while using the LocationSensor and the app Screen goes to sleep, the app stops recording geo-coordinate changes..until the screen is active again. I expect when you enable another app, the same thing happens with your AI2 app. It stops responding. The moral of the story is to remain connected, don't attempt to use other apps while the AI2 app is in use.

yes you are absolutely right I thought this unresponsiveness is due to the fact that I have used three clocks in my app. Please tell me one more thing what is correct using separate clocks for each tasks or to use a single clock to execute all tasks. I want my app to be as fast as possible ,at present it is kind of sluggish but works.

I do not know because I cannot test your app Mah. Only you can do that.

My experience with Clocks is some applications need one or several to accomplish a goal. If you use multiple Clocks, ensure that the TimerInterval of the multiple Clocks are each set to different values. For example, it Clock1 fires at 1000 ms, Clock2 should fire at a different value, perhaps 1029 ms. Staggering will help avoid (but not eliminate) collisions (instances when both Clocks fire more or less simultaneously, creating issues). Experiment.

Sluggish? Experiment with different TimerInterval settings. What values; sorry I don't know.

Here is an example of how to make one Clock do the work of three Clocks …!msg/mitappinventortest/BOj2-ThEe3s/VlmXdcVOAgAJ

This is what I have thought. Thanks for increasing my knowledge about this .
Well I haven't posted the blocks because they might look silly to experts like you and of course I didn't thought that someone could actually test my app to help me. Thanks still.

Ooo nice one this one is really good.Thank you guys you are the best.

One thing more that I want to ask that I have a mechanism to auto connect to the device but the address has to be hard - coded that means in case of a bluetooth module replacement I have to change the app too please inspect my blocks and guide me on how can I make the address changable from within the application.

I believe it might be time to slow down and do some reading and tutorials. What you hope to do using a changeable address is incredibly easy. Instead of hard coding the BluetoothClient.Connect...address, use a variable in that spot. How to acquire the additional address information depends on where you expect it to come from. It can come from a File loaded in Media, or on the sdCard as a single value or as an element of a List or it can be manually input on the app using a TextBox where you assign the value of the variable to what is typed in the box or ... yes, there are many other ways.

Here are some resources to help you learn to use the AI2 tools. A very good way to learn App Inventor is to read the free Inventor's Manual here in the AI2 free online eBook ... the links are at the bottom of the Web page. The book 'teaches' users how to program with AI2 blocks.

There is a free programming course here and the aia files for the projects in the book are here:

How to do a lot of basic things with App Inventor are described here: .

Also look here App inventor español. Offline. Tutorial. Ejemplos. Instalación. Códigos. Juegos. Curso gratis de App inventor. and here for more tutorials.

Just try some blocks, show us what you did and someone will probably provide specific advice. This is your app, not ours. Learn to use the tools. You didn't post an image of all your blocks 'they might look silly to experts like you ' ; not posting is what is silly if you want advice.

Yes i want to achieve this.

You are soo nice you are really a helpful person I will try some blocks and then post what I did to achieve the above mentioned problem. Thanks for the motivation.

wow. You are not even arrogant. I want to share my app blocks but they are soo much to fit in a screen shot what can i do to share all of my blocks at once??

you might want to share only the relevant blocks

Oh thanks SteveJG. It’s seems that I was lacking a bit of info and bit of motivation and you gave me these both, with your guidance I created this. The user pairs first and then open the apps list picker to connect to the paired device for the first time then the connected device’s address will be stored in the app and the app will use it to automatically connect to that address automatically. Thank you good sir you helped me a lot.Below I have shared the blocks for you to see and ofcourse other can also use it also please check this for any improvements.!

1 Like

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