Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Learn to code The quick and easy way to create Android apps
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 di erent ways.
This tutorial modies 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:
Related:
Numeric data is sent in binary format rather than decimal text format (e.g. a number like 237 is in decimal
format). With Bluetooth, we need to decide if our numbers are 1-byte, 2-byte or 4-byte numbers and to
understand that, we need to understand a little bit about binary numbers. (
When we write programs with numbers, we type in numbers like 42, 9, and 128. But computers do not
(normally) store numbers in text form, but instead, convert them into a representation. This section
gives an introduction to binary numbers and how they are stored, and why this is a factor in Bluetooth
communications.
When we write numbers, such as 128, we are writing them in base 10 notation. That is because each digit
holds a value from 0 to 9 (or ten values). The number 128 is 3 digits stored as:
1 * 100 + 2 * 10 + 8
From the right and the moving to the left, there is a ones digit, then a tens digit, and then a hundreds digit.
Another way to look this is to say we write out numbers similar to:
102 101 10
Let us look at binary notation. In binary, each digit can hold only the values 0 or 1 we only have 2 values per
digit, not ten! Indeed, we say a binary number is stored in base 2 (get it? just 2 values in base 2 whereas base 10
has 10 values per digit). A binary number might look like:
1001
representing a value of 0 or 1 at each digit position. But what does that mean in terms of a number value? It
means instead of having position values like 100s, 10s and 1s, we have position values of:
16 8 4 2 1
which corresponds to
24 23 22 21 20
In binary notation, each digit has only the value 0 or 1 (not the 0 to 9 of base 10). To keep this simple, consider
the number 9 (in good old base 10). Writing this in binary, we get the digits:
1001
Remember each digit position holds only a value of 0 or 1, which is then multiplied by the digits place. Just as
we multiplied the 1 in 128 by one hundred and the 2 by ten, binary numbers have their own multipliers for
each digits place. For the above number, the multipliers are:
8 4 2 1
Or
1*8+0*4+0*2+1*1
Because we have two values per digit position (0 or 1), we say this is Base 2 (versus base 10 which had 10
possible values per digit position).
Numbers that we write in text are converted into the binary form for processing on computers [1]. A group of 8
also known as 8 is called a .
Depending on the range of values stored, numbers are typically stored as 1-, 2- or 4-byte binary values. A 2-
byte number has 16 bits and a 4-byte number as 32 bits.
For negative numbers, one of the bits is reserved for the sign of the value which leaves one less bit for the
value, as shown above.
App Inventors Bluetooth component supports the sending and receiving of binary values. But to use
the feature, you need to know the range of values the app will send and then specify whether the
app should use 1-byte, 2-bytes or 4-bytes for the numeric value per the above table.
The numbers in the preceding example are called integers they do not store a decimal point.
Such values are called real or oating point numbers (because the decimal point can appear at any location).
There are several ways computers can store oating point numbers but a description of those details is beyond
what we need to understand Bluetooth communications.
1 2 8
You do not need to know about BCD to use Bluetooth this is just to illustrate that there are other ways that
numbers may be stored in computing systems.
The purpose of this app is to demonstrate the sending of binary numbers (versus just text values). The Server
app is modied to send binary numbers and the Client app is modied to receive those numbers. (Optionally,
the Client app could also be modied to send binary numbers and the Server could be modied to receive
binary numbers. Both sides can process binary numbers.)
The user interface of the Server Demo app shown in Part 1 is modied as shown below, to send either text or
to send numeric data. For the full implementation details, download the source code, at the end of this blog
post.
The Client Demo app is slightly modied to present two lines for the incoming data: (1) a display of the
incoming , and (2) the data received (either text or numeric values go here). The purpose of the
value will be explained in the next section.
The Blocks Code
In Part 1 of this series, our sample Bluetooth program sent and received only text messages.
In Part 2, our sample program can send both text and numbers. The sender needs to notify the receiver that
the data being sent is either a text value or a number. Specically, the sender needs to indicate whether the
data is text, a 1-byte number, a 2-byte number or a 4-byte number.
The type of the data being sent is included in the data stream as the rst byte of data. We call this the
byte (as in you are commanded to receive one of the following).
The command byte has one of the following values to indicate the type of data being sent:
The command byte is sent before the data as shown by the blocks in the Send Text button event handler:
Think of the data transmission as a data packet that contains both a command byte and a package of
data.
The Send Numeric data button is similar but a bit more complex! While we could put all numeric values into a
4-byte value, why send extra bytes if we do not need to?
The event handler looks at the value of the number, determines in which the range the value lies, and then
uses the 1-byte, 2-byte- or 4-byte Bluetooth sending methods, as required for the number:
In the Client app, the bulk of the code changes are in the event handler. At each timer tick, the app
checks to see if data is available over Bluetooth. If data is available, the rst byte of the incoming data is read
using . This is the byte. Depending on the value of the 1, 2, 3 or
4 the appropriate Bluetooth method is used to read the next bytes of data as either a number or as a text
value.
Downloads
BTClient2.aia App Inventorsource le(App Inventor source code les have the lename extension .aia)
BTServer2.aia App Inventor source le
Download the source code to your computer. Then, in App Inventor, go to the Projects menu and select
Import project (.aia) from my computer
Remember you need two separate Android devices in order to run and test the Bluetooth sample
programs!
If you nd these tutorials helpful (I hope you do!) please take a look at my books on App Inventor. To learn
more about the books and where to get them (they are inexpensive) please see my App Inventor Books page.
Please click on the buttons below this post to share with your friends on Facebook or other social media.
If you are not already following this blog, click on the following links to like on Facebook, add to your Google+
circles or follow on Twitter or in your RSS news reader. Thank you for visiting!
Be Sociable, Share!
Tweet 0 Share
This entry was posted in Components, Programming Method, Tips and tagged android, app inventor,
bluetooth, mit app inventor, programming, smart phone, software on February 2, 2015
[http://appinventor.pevest.com/?p=569] .
24 Replies
19 Comments
0 Tweets
2 Facebook
3 Pingbacks
Last reply was 2 months ago
replied:
View March 15, 2015
I am pretty sure that Bluetooth in App Inventor sends only integer values, not oating point values. But there
are two possible solutions you could try.
One is to just send the value as text. This, however, requires a conversion on each end from a oating point
number to text, and then from text to oating point. App Inventor makes this easy, if App Inventor is used on
both ends just assign a oating point value to a text variable and the conversion should happen
automatically.
The other way is to convert a oating value to a 4-byte long value. For example, lets say we wanted to send a
value such as 3.14.
Send this as a large integer 314. Start with 3.14, multiply by 100 to give 314. Send the value 314, and then divide
by 100 on the other end. This is known as xed point representation.
This method, obviously, does not work very extremely large oating point numbers, nor does it work for all
potential decimal places unless extra code is added. Example: 3.14 versus 31.4 need to be handled di erently.
Reply
replied:
View March 15, 2015
Have you tried sending data only from the Arduino to the Android device, independent of your application
Have you tried sending data only from the Arduino to the Android device, independent of your application
code? By splitting out and testing the BT communications link by itself, this might help to isolate whatever is
causing this problem.
Ed
Reply
replied:
View May 27, 2015
I think you mean, an external sensor? Yes, if the sensor can speak classic Bluetooth, then App Inventor code
could talk with that sensor device.
One way to do that is to use an external microcontroller and a serial Bluetooth adapter connected to the
controller. Use the microcontroller to monitor the sensor readings and then send those to the Android device
over the Bluetooth link.
Be aware that App Inventor does not appear to support Bluetooth Low Energy (LE) devices. Bluetooth LE is
becoming a popular choice for sensor networks. If you can stick with the older Bluetooth classic, you can use
App Inventor.
Ed
Reply
replied:
View July 17, 2015
Sure, I do not see why not.
Reply
replied:
View October 6, 2015
Yes, the actual transmission of the numeric values is in numeric or binary format when using the features to
send 1, 2 or 4 byte numbers (see http://appinventor.pevest.com/?p=569).
This separate tutorial on using App Inventor Bluetooth to communicate with an Arduino microcontroller does
something similar to what you are seeking: http://appinventor.pevest.com/?p=718
That code sends a command and a parameter byte between the Android device and the Arduino board.
something similar to what you are seeking: http://appinventor.pevest.com/?p=718
That code sends a command and a parameter byte between the Android device and the Arduino board.
That same code structure could be set up for App Inventor (and I may put that on my to do list as it would be
very useful for Android to Android BT communications).
Ed
Reply
replied:
View 10 months ago
It sounds like a byte or bytes are being left in the input bu er. That could happen if, for example, the code
checks
if get bytesAvailable> 0 then
Received1ByteNumber
BUT, suppose for whatever reason, there is an extra character in the incoming receive bu er. If that happens,
then we have only the rst of whatever bytes are there. I dont know why there would be extra character(s)
there, but once its is read, it should be cleared out.
To clear the input bu er, you could do something like
while get bytesAvailable > 0
getbyte = Received1ByteNumber
If there were 4 bytes in the input bu er, this should read them all out of the incoming input so that none are
remaining. This is just an idea to try as I do not know why, specically, you might have more than 1 byte in the
input bu er or even if that is the case.
Ed
Reply
replied:
View 5 months ago
Hi Kevin,
MIT App Inventor only supports the transmission, over Bluetooth, of 1, 2 or 4-byte binary values.
I recommend splitting your 6-byte binary value into a 2-byte value and a 4-byte value.
I do not know what format your 6-byte value is, to begin with. If it is something that is already available as
individual bytes, then you could just send the six bytes, one-byte at at time.
If the 6-byte value is a number, then you may want to split it up. App Inventor is not great for this because App
Inventor uses oating point numbers, not integer. Many programming languages have an easy way to shift the
bits in a number to the left or right we could take the 6-byte value and shift it 32 bits to the right, leaving us
with just the 16 bits in the left most two bytes of the 6-byte value. (That is a bit of a head full of text drawing it
as 6 bytes and then shifting that 4 bytes to the right will illustrate the idea.)
This leaves us with another way to try and split it up, by using division.
Lets say we have a 16-bit numeric value (in the range of 0 to 65,535, unsigned). To separate this in to two bytes,
we can divide this number by 256 (2^8 or the largest number we can represent with 8 bits).
Lets say we have 40,000. Divide this by 256 to give a value of 156.25. This tells us we have 156 (in base 10, but
really it would be in binary) in the left byte and 64 in the right byte (64 is 0.25 times 256). By doing this, weve
split the 2-byte value into separate individual one byte values.
We can do this with a six byte value too. Lets say we have a big number that is larger than 2^32 (about 2 billion)
and want to separate out the two left most bytes. Wed take the starting value, lets call it X, and divide by 2^32:
replied:
View 5 months ago
Hi Edward,
Thanks you very much!
Reply
replied:
View 4 months ago
I suspect it is a timing issue in how App Inventor transmits the data. Only part of the data has been received or
read when the text string is returned to your app. I suspected this could happen when I wrote the code but had
not exhaustively testing all possibilities.
What you might do (or I might do at some point) is add a layer of code to this routine so that we add an end of
data character. For example, lets set our end of data character to ~.
Wed transmit our data as 123;2345;200;500~.
Then, when receiving the incoming data, we would continue to read characters until receiving the ~ character.
If this comes in separate pieces, such as 123;2345; and 200;500~, we would continue to read incoming data
and copy everything weve read into a single text string, until we receive the ~ character.
We would then combine the two pieces into one piece.
One way to do this would be to create a new procedure. I will give an example in pseudo code, something like
this:
ReadBTData(String DataRead)
FullTextString =
Repeat
ReadBT (String IncomingData, EndOfData)
FullTextString = FullTextString + IncomingData
Until EndOfData is true
-
ReadBT would be the existing read data from Bluetooth code, but have it check to see if a ~ (or other end of
data character you select) has been received.
If the end of data character was received, then it sets the EndOfData value to True so that we know weve read
everything.
Basically, this is a procedure that just keeps reading incoming data until we know for sure that weve reached
the end of the full data sent to us.
Ed
Reply