2017년 3월 29일 수요일

Android MIT App Inventor – Auto Connect To Bluetooth


In a previous post I showed how to connect an app inventor Android app to a Bluetooth module connected to an Arduino to control an LED. See Turning a LED on and off with an Arduino, a HC-06 and Android
A few people have asked how to make it so that the app auto-connect to the Arduino on start up and I thought I would offer my solution. This example adds to the previous guide.
The easiest way to add an auto connect would be to use a fixed address and put the connection function call in the Screen1.Initialize block. Since the Screen1.Initialize block runs automatically when the app is started, the app would try to make the connection every time it started.
appInventorScreen_001
This is quick and easy but it means the mac address for the BT module is hard coded in to the app and to change the address you would need to change the app and recompile. This makes it very difficult to use a different Bluetooth module.
A better way is to allow the user to connect to any BT module and then save the address for next time. Then, when the the app is run again it can use the saved address to auto-connect to the Bluetooth module. By saving the address every time a new connection is made the user can change Bluetooth modules with ease.

The App

The first time the app is run there is no saved address and the app waits for the user to make a connection.
– Click the BT button to bring up a list of paired devices.
– Select one of the paired devices, in my case the HC-06.
– If the connection is successful the BT button changes to “Connected”.
– The address of the connected BT device is displayed on screen.
ScreenShots_01
The next time the app is run it will try to auto-connect to the saved device.
– While trying to connect the BT button will display “Connecting”.
– If the connection is successful the BT button will show “Connected”.
ScreenShots_02

App Designer Screen

designerScreen_001
There are only a few screen elements to note:
A button to activate the connection process.
A label to show the saved address.
The LED button to turn an LED on or off.
A list to store the list of paired devices.
The non-visible components required are:
TinyDB.
Clock1.
And of course the Bluetooth client.

The Blocks

Android MIT App Inventor - Auto Connect To Bluetooth_blocks_001

The Main Details

Blocks001_Screen1_Initialize
When the app starts, the Screen1.Initialize function is called. This retrieves the saved Bluetooth address from the TinyDB and copies in to the global variable “savedDeviceAddress”, it then starts the timer. If there isn’t a saved address the variable is set to “” and the timer is not started.
I use a timer to overcome a minor annoyance caused by how app inventor works. If the connection blocks are all placed in the Screen1.Initialize function, the connection process works but the screen is not updated correctly and the “Connecting” button text is not displayed.
Blocks002_Clock1_Timer
When the timer fires, the Clock1.Timer function is called. It is here the app tries to connect to the saved Bluetooth address. If a connection is successful the BT_BUTTON text is changed to “Connected”. If the connection is not successful an error message is displayed. The function also checks that Bluetooth is turned on before trying to make a connection.
The BT_BUTTON allows for new connections, when it is clicked it activates the list picker and allows the user to select another device. When a new connection is made the mac address of the newly connected BT module is saved to the TinyDB.
Blocks002_BT_BUTTON
Blocks002_ListPicker
The rest of the code is the same as the previous example. For further details see Turning a LED on and off with an Arduino, a HC-06 and Android

Download

Download the app inventor aia file

댓글 없음:

댓글 쓰기