Tag Archives: appinventor

Part 1: Basic Bluetooth communications using App Inventor

(Some very minor updates were made to this in November 2019).

This tutorial covers basic App Inventor Bluetooth communications  code.   Subsequent tutorials will add additional features. To implement and test this sample code, you need access to two Android devices – one to act as a Bluetooth “server” and the other to act as a “Bluetooth” client.

I tested this code using an old LG smart phone running Android 2.2 and a new Nexus 5 running Android 5.0.1.  I also tested this code using the Nexus 5 paired with a Nexus 7 tablet. (Update I have since also run this on an Honor 8 phone and a Pixel 2, with much newer versions of Android.)

This tutorial is lengthy – it introduces Bluetooth communications, then presents the user interface and blocks code for both the server and client programs, and then discusses how to set up the Bluetooth Communications link using “pairing”.

Downloadable App Inventor source code for the client and server is at the end of this post.

This is the first of several  posts on Bluetooth. This first post covers basic connections and the sending and receiving of text between two Bluetooth devices. The two halves of the link – client and server – are kept in separate apps to keep this simple,  however, it is possible for a single app to act as both a client and a server. A subsequent post will show how to send other types of data, such as numbers, and introduce additional features for using Bluetooth communications.

Related:

Introduction to Bluetooth

Bluetooth is the communications technology with a funny name.[1] Bluetooth is actually named for a long ago Danish king who worked to unite groups of people, which is similar to Bluetooth’s goal of interconnecting different devices.  The King’s real name was “Harald” but he had a nickname that translates as “Bluetooth” – no one knows for sure why he had this nickname but one thought is he had one dark tooth that may have appeared black or blue. And that is certainly an obscure way to choose a name for new technologies!

Bluetooth establishes a very low power, short range (up to 10 meters) communications link between two devices. Bluetooth uses the same frequency band (2.4 Ghz) as Wi-Fi, but uses different technology. Both Bluetooth and Wi-Fi use forms of spread spectrum radio links that result in signals moving around within a wide band in ways that enable sharing of the spectrum by multiple devices. But the two technologies serve different purposes, are  not identical, and cannot communicate with one another.

Bluetooth applications include common wireless headsets for wired and cellular phones, and in-ear cordless adapters for phones. Bluetooth is also used by cordless headphones and to exchange address cards between devices, and for industrial applications where sensors collect and send data into a network.

There are two forms of Bluetooth – classic Bluetooth, which  we use in the sample applications, and a newer version known as Bluetooth low energy, Bluetooth BLE, Bluetooth LE or Bluetooth Smart – all referring to the same new technology.  The newest Android devices running Android 4.3 or newer, usually support the newest Bluetooth Smart technology. Regardless, we use classic Bluetooth which is backwards compatible to older phones, and is the technology supported by App Inventor.

Setting up a Bluetooth devices involves “pairing” the two devices and establishing a connection. This will be covered later in this tutorial.

Footnote:

[1] Actually there is another communications technology with a funny name called TWAIN, which is an acronym for “Technology without and interesting name” (really!)

The Designer View

Continue reading Part 1: Basic Bluetooth communications using App Inventor

How to change button colors in App Inventor

You can change the colors of buttons (and some other components) in your App Inventor apps. Using some programming tricks, buttons can be made to change color continuously!

Here is a very short demo of a button whose color is continuously changing on the screen:

I will soon post a tutorial on how to do simple color changes and also how to do “tricks” like that shown in this video.

Be sure to keep checking back, like us on Facebook, Google+ and Twitter for more updates! Thanks!

Android App Inventor Tip Calculator version 4: Introduction to Procedures

What the App Does

This is the fourth version of 5 versions that implement a tip calculator. Each version has added improvements or new programming methods.

  • Version 1 introduced the basic app and the use of error checking to handle user data entry mistakes.
  • Version 2 introduced the Slider user interface component to select the tip amount. Using the Slider, the user selects a tip from 0% to 30%. Since there is no text entry, it is not possible for an incorrect tip value to be entered.
  • Version 3 introduced an improved user interface to eliminate user data entry errors.
  • Version 4 introduces “procedures” to clean up the code in preparation for version 5.
  • Version 5 revises Version 4 to make the calculation of the tip fully automatic plus fixes some minor oddities.

The Tip Calculator User Interface

No changes from version 3.

The Designer View

No changes from version 3.

The Blocks Code

Continue reading Android App Inventor Tip Calculator version 4: Introduction to Procedures

Tip Calculator App (version 2) for App Inventor

What the App Does

This is a simple app to calculate the tip and total bill at a restaurant – or other service provider where a tip is common place.

This is the second of 3 apps that implement a tip calculator.

  • Version 1 introduced the basic app and the use of error checking to handle user data entry mistakes.
  • Version 2 (this tutorial) introduces the Slider user interface component to select the tip amount. Using the Slider, the user selects a tip from 0% to 30%. Since there is no text entry, it is not possible for an incorrect tip value to be entered.
  • Version 3 will introduce a way to avoid all user data entry errors – by designing the app in such a way that the user can enter only correct values.

The Tip Calculator User Interface

In version 2, we eliminate the text box for tip percentage and replace it with a Slider component and we use a Label component to display the current setting of the Slider. A slider is something you drag with your finger, as shown in this screen shot:

Screenshot_2014-11-05-12-31-57

We set the tip amount by putting our finger on the “thumb” and dragging the thumb left or right. “Thumb” is the name given to the moveable control that appears on the slider.

As we drag the “thumb” control on the slider, the amount of the selected tip is shown – which is 20% in this screen shot.  Using this design with a Slider, it is impossible for someone to enter an invalid tip!

Continue reading Tip Calculator App (version 2) for App Inventor

A Tip Calculator App (version 1) written in App Inventor

What the App Does

This is a simple app to calculate the tip and total bill at a restaurant – or other service provider where a tip is common place.

This is the first of 3 apps that will implement a tip calculator.

  • Version 1 (this tutorial) introduces the basic app and demonstrates the use of error checking to handle user data entry mistakes.
  • Version 2 will introduce an App Inventor feature that makes the app more interactive and reduces data entry errors.
  • Version 3 will introduce a way to avoid user data entry errors – by designing the app in such a way that the user can enter only correct values.

Check back in the days ahead to see how this app evolves to Version 2 and then Version 3. I think you will learn a lot, have a bit of fun, and end up with an app that might be rather useful!

The Tip Calculator User Interface

In version 1.0, The user interface features two inputs: one for the amount of the bill and one for the tip, as a percentage (such as 20 for 20%), plus a button to calculate the amount of the tip and the total bill including the tip.

TipCalculatorUI

A Notifier component displays a warning message when non-numeric values are entered for the amount of the bill and the tip. See “Display Warning and Alert Boxes in App Inventor apps” for a tutorial on the use of message boxes.

The label fields below the button display the result of the calculation.

The arrangement of the user interface controls relies on vertical and horizontal layouts.  Please see “Chapter 4 – Layouts in Detail” – available here – to learn how to use the layout features to arrange components in the user interface. The layouts are arranged as shown in this components list:

TipCalculatorComponents

If layouts are confusing for you, you may just drag and drop controls on to the Viewer any way you wish, but by using the Vertical and Horizontal Arrangement layouts, the controls can be arranged in a more pleasing way and centered on the screen. Your best bet is to learn how to use layouts as they provide a way of creating nice user interfaces that work in both portrait and landscape modes – automatically. Refer to Chapter 4-Layouts in Detail for a tutorial on layouts.

The labels used to display the calculation result are shown in 18 point font size. To set the font size, select the label in the Viewer and then enter a FontSize value of 18 (or other size – your choice) as shown here:

TipCalculatorPropertiesLabel

For the text box data entry, use the Hint property to enter an example data entry, as shown below:

TipCalculatorPropertiesTxtBox

 

The Blocks Code

This tutorial introduces data error checking – specifically, if the user enters non-numeric values for the bill or the tip amount, then a message box is displayed. The blocks code, below, is too large to display full size. However, you can view an enlarged version by clicking on the image. Depending on your Internet browser, you should be able to click a second time to zoom in on the image.

TipCalculatorBlocks

How This Works

An if-then-else block is used – together with the is a number? block – to ensure that only valid numbers are processed by the program. Note that there are two if-then else blocks – the first checks that the entered bill amount is a numeric value, and the second checks that the entered tip value is numeric. If either data entry contains non-numeric characters, a message is displayed on the screen about the problem.

The calculation of the tip is straight forward. The percent value (such as 20 for 20%) is converted to a decimal fraction – 20 becomes .20 – by dividing the percent (20) by 100. The bill amount is then multiplied by the tip percentage. For example, if the bill is 30 and the tip is 20 (%), the tip is 30 x 0.2 or 6. (I am leaving off $ signs to make this tip calculator generic for any currency.)

The total bill, including tip, is just the sum of the calculated tip amount and the original bill.

Key Features Shown

  • Use of is a number?
  • Use of error checking and processing
  • Use of layouts

Downloads

  • Source code App Inventor “.aia” source file (App Inventor source code files have the filename extension .aia)
  • Download the source code to your computer. Then, in App Inventor, go to the Projects menu and select “Import project (.aia) from my computer…”

Using TinyDB in App Inventor

(This post was completely rewritten and updated on October 30, 2015)

What is TinyDB?

TinyDB is a simple “database” that stores data on your phone or tablet. Unlike program variables that go away when your app is finished running or your phone is re-set, values stored in TinyDB remain on your phone for use the next time your app is run.

About Memory on your Phone or Tablet

Your smart phone or tablet typically has two primary types of memory: RAM and FLASH memory.

RAM stands for “random access memory” – but today we mostly think of RAM as memory that can be accessed very fast (as compared to Flash or hard drive memory storage). RAM retains values as long as power is applied to the RAM circuity. Once we turn off the power, the values stored in RAM are lost. (In some applications, extra batteries are used to continuously provide power to RAM even when the “normal” power is turned off.)

Flash memory retains values when the power is turned off. But access to Flash RAM is not as fast as access to conventional RAM memory.

Why is it called “Flash”?  There was an early version of memory technology where the memory was erased by literally flashing it with ultraviolet light. However the inventor of Flash RAM chose the name “Flash” for different reasons. Modern Flash RAM is read, written and erased electronically.

App Inventor variables are stored in RAM memory – and the content of RAM is erased or reset whenever the power is turned off. TinyDB, on the other hand, stores values in FLASH RAM, where the values remain even when the power is turned off.

Using TinyDB

TinyDB provides a simple way to store and retrieve data efficiently and to store the data in long-term storage.  TinyDB is based on the concept of a “tag” to identify the stored data, and the data value. Think of a “tag” as like using your name as your identification to look up your address:

Tag value: Martin

Value: 123 Main St, Anytown, USA

or

Tag value: Alexa

Value: 321 Other St, Someplace, USA

TinyDB uses the “tag” (such as Alexa) to quickly locate the corresponding value. Even if you have 100 names and addresses stored in TinyDB, TinyDB can  look up the “tag” quickly and use the tag to find the corresponding value. We do not need to know how TinyDB does its look up so fast – it just does it [see Footnote 1].

In most database programs, the “tag” is known as a “key” or “key value”. App Inventor uses the name “tag” in place of “key”. As I am used to the name “key”, I tend to use “key” were I should have used “tag” in App Inventor! You will see this is the sample program, below!

To learn how to put TinyDB in operation, we construct a very simple app, described below.

Continue reading Using TinyDB in App Inventor