Don’t Text and Drive

Summary

This tutorial will shows how an application can use data from a car in real time to make sure that text messages are automatically read aloud while the user is driving.

What You're Building

This application has one screen with two pannels that will turn from green to red if the phone can tell that the user is driving. If both of these pannels are red and the user receives a text message, then your phone will automatically read the text message aloud.

Before Starting

In order for your application to make use of the data from your car, you need to download and install the OpenXC enabler app on your phone. This application is what controls the connection between the source (the simulator or you car) and your android device. You can find the OpenXC application here

YOUR APPLICATION WILL NOT WORK WITHOUT IT

If you want to test your application but don't have a car, that isn't a problem. OpenXC has a vehicle simulator program that you can run from you computer. Follow the link below to try it for yourself.

http://openxcplatform.com/projects/simulator.htm

Set up the components

The interface for this application doesn't have many components, the entire point is to keep the driver from needing to look at the phone! The picture below shows what the interface will ultimately look like.

App Layout
Component Type Palette Group What You'll Name It Purpose of Component Component Settings
Label User Interface IgnitionLabel This label's textw ill update and the background color will change between green and red based on the car's ignition status FontSize:18, TextAlignment:Center, Width:Fill parent, Height:200px
Label User Interface TransmissionsGearLabel This label's text will udpate and the background color will change between green and red based on the car's transmission gear status. FontSize:18, TextAlignment:Center, Width:Fill parent, Height:200px
OpenXC Sensors OpenSC1 This component gives you access to the car's data  
Texting Social Texting1 This makes sure that the application will be activated when you receive texts ReceivingEnabled:Always
TextToSpeech Media TextToSpeech1 This will let the application read your text messages aloud  

Add behavior to the components

First, we're going to build the two blocks that will change the text and the background color of the labels based on the input that the application received from the cars. If the car's ignition status isn't set to OFF, then IgnitionLabel's background color will change to red, otherwise it will be green. Likewise, if the transmission gear isn't set to NEUTRAL, then TransmissionGearLabel's background color will change be red. See the image below.

Blocks 1

At this point, the two labels should give real time feedback about the status of the car. If you ave your android device hooked up to the vehicle simulator or to a car, then you should be able to see the colors and text change when you change the transmission gear and the ignition.

Now, we're going to build the blocks that make sure that the message is read aloud. When you receive a text, the blocks check the ignition and the transmission gear. If the ignition isn't OFF and the transmission gear isn't NEUTRAL, then the TextToSpeech component will put together a message containing the phone number and the content of the text message and will read the message aloud. See image below.

Blocks 2

Here's a screenshot of the completed application. If you text yourself or text the phone you're using through some other device, then you can fully test this application.

Blocks Final

Download Source Code

If you'd like to work with this sample in App Inventor, download the source code ← (make this a hyperlink) to your computer, then open App Inventor, go to the My Projects page, and choose More Actions | Upload Source.

Done with Don't Text and Drive? Try the other OpenXC tutorial, Running Low, or return to to the other App Inventor 2 tutorials here.