Category Archives: Programming Method

Coming soon: Bluetooth to connect Android App Inventor code to Arduino

I  have an App Inventor app running on my Nexus 5 and talking to an Arduino board via Bluetooth. I finally had a chance to work on this!

UPDATE: Here is the link to the final code and tutorial information!

What I have  now is some experimental code not suitable for posting online. I will be revising this code to turn it into a simple example that will provide basic functionality, and then present a tutorial on putting it to use in your own applications.

Update Tuesday Cinco de Mayo (in the U.S.): I have the demo code up and running. Next up is to test and write up the tutorial! It’s coming! The first tutorial will be simple – intended to get you up and running.  I will eventually create some more advanced features.

Longer term, I may create a more general solution for passing data packets back and forth between an Android App Inventor app and an Arduino board, so that many types of applications may be supported using my basic code library.

Arduino is a small microcontroller board used by hobbyists and others to add computing to small devices, art projects, robots, Internet connected devices and much more. Arduino is not part of App Inventor. Arduino is, for an “embedded system” easy to use in terms of building electronics hardware and writing control software. By writing App Inventor code to talk to an Arduino board, we open an entire world of new possibilities using simplified development (App Inventor on Android, and Arduino on the hardware side).

  • Use your phone or tablet to remote control an Arduino device over a Bluetooth link
  • Use an Arduino device to monitor remote sensors, and then link sensor inputs to an Android phone over Bluetooth
  • Conceptually, an Arduino device could monitor local sensors (temperature, humidity, security alarms), and transmit sensor data to an Android phone, which, in turn, could forward the data onto an Internet location.

Identifying the cause of serious errors in App Inventor apps

Some times App Inventor 2 apps stop working (well mine sometimes do this, may be your’s don’t!)

Besides looking carefully at your blocks code, there are some additional steps that may be helpful in identifying the cause of the problem.

1. Check your blocks code very carefully to see if you can spot an error in your program.

2. Add a status message area in the user interface (using the Designer) and then in your blocks code, add blocks to assign status texts to this message area. Your status message could include a message indicating what section of your code is running, or to display the values of variables in your program.

3. Add a special “Error handler” to catch and display information when the program encounters a serious problem. The following section is adapted from my tutorial on basic Bluetooth communications.

Error  Handling

For most of our App Inventor apps, we ignore potential errors – if errors occur, the app stops running and Android displays error messages.

Our app can intercept the error condition by adding an error event handler to the main screen, Screen1. In the Blocks editor, select the Screen1 component and then select the When Screen1.ErrorOccurred event. Drag this to the Blocks editor and then add the following code.

The ErrorOccurred event has four parameter values (local variables) that contain information about the error. This error handler displays the error values on your device screen, rather than immediately shutting down the app.

BTServerErrorHandler

 

To use this code, add a text label to your user interface – in the example above, the label has been renamed to lblStatus.

There is no guarantee that these error values will identify the specific problem but they may give an indication as to where the problem is occurring in your program!

Displaying web pages in your Android apps

Last year, I presented a short tutorial on displaying web pages from inside your App Inventor apps. Now, here is a some what improved version that prompts for a web address URL, checks to see if http:// has been entered, and if not, prepends http:// to the front of the address. Then the web page is displayed.

The Designer View

There is not much to the user interface – a text box to enter the web URL and a button to display the web page. The page then appears below the button, and the content may be scrolled on the screen.

To create this user interface, drag a horizontal layout onto the screen and then add a label for the “Web page URL” prompt, followed by a text box for the data entry. Then add the Display web page button.

From the User Interface section of the Palette, at the left of the Designer screen, drag and drop a WebViewer component on to the design area.

Screenshot_2015-02-10-15-15-17

 

The WebViewer is not a full Internet browser – it is a component that displays the specified web page only. The WebViewer does not support standard browser features, such as saving web page content nor does it provide a history of the web pages visited.

The Blocks Code

Continue reading Displaying web pages in your Android apps

Part 2: Sending numeric data using App Inventor Bluetooth communications

Part 1 of this tutorial introduced Bluetooth communications and implemented a simple method of sending text data back and forth between two Android devices over the Bluetooth wireless link. If you are not familiar with using App Inventor’s Bluetooth component, start with Part 1.

In Part 2, a data packet concept is introduced to guide the communications between devices, and is used to send a combination of text and numeric data. This section introduces the concept of binary numbers so that you can understand why we would handle text and numbers in different ways.

This tutorial modifies the user interface of both the client and server programs introduced in Part 1. Then, blocks code is added to send text and numeric data. Numeric data is sent as binary data using special methods of the Bluetooth components.

Related:

Continue reading Part 2: Sending numeric data using App Inventor Bluetooth communications

App Inventor Bluetooth Communication tutorial coming soon

Android phones and tablets support Bluetooth communications (“BT”).

BT is a very low power, very short range communication technology used for connecting earphone and headphone adapters, wireless microphones, and wireless keyboards and mice to computers and tablets.

App Inventor supports BT links and can be used to transmit data back and forth between two Android devices.

When two devices communicate, one device acts as a “server” and the other as a “client”, although either send and receive data.

I now have basic Bluetooth communication working between two Android phones. As I finalize the Bluetooth sample app, I will post a tutorial here on how to set up Bluetooth and how to write apps that use Bluetooth communication.

Longer term, I intend to connect an App Inventor app to an Arduino microcontroller board using the Bluetooth link. I will post information on that once it is available.

(Due to volunteering every evening with a FIRST Robotics team, and a few extra things going on, my updates are a little less frequent at the moment – sorry!)

Updated: Writing and Reading Text Files Using App Inventor

This post is a major update to a previous post on reading and writing text files using App Inventor. This revision includes information on how to locate the text files you create in your App Inventor apps, plus how to transfer those files from your smart phone or tablet to your computer.

An earlier blog post described how to store data using TinyDB so that an app’s data can persist between uses of the program, or even to share data between screens in a program.

Another way to save data is to write the data to a file on your Android device. App Inventor has introduced a File control that lets us write text data to a file and then read it back, later. As we will see, the File control is not the easiest thing to use but with some work, the control can be used to store data from our program into a file.

Once data is in a file, you could, hypothetically, transfer the file from an Android to device to another computer. Because Android stores the files in a way that they may not be readily accessible – or even visible – we need to use some simple tricks to find the file and transfer the file to a computer.

Update: To learn more about text files and transferring data in the CSV file format, check out Volume 3 of “App Inventor 2 Databases and Files” – thanks!

Continue reading Updated: Writing and Reading Text Files Using App Inventor