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:
Brief Introduction to Binary Numbers
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. (This will not hurt – I promise! Or at least it will not hurt very much!)
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 binary representation. This section gives an introduction to binary numbers and how they are stored, and why this is a factor in Bluetooth communications.
You do not need to know these details to use Bluetooth communications for sending text and numbers – but it is helpful to understand why App Inventor can send several different types of numbers! If you prefer to skip this section, you may do so.
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:
hundreds tens ones
Another way to write the value of the digit positions is:
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 digit’s 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 digit’s 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 binary digits – also known as 8 bits – is called a byte. The value 1000 0010 represents 8 bits stored in one byte of data.
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.
⦁ 1 byte: for values in the range of 0 to 255, or -128 to +127
⦁ 2 bytes: for values in the range of 0 to 65535 or -32768 to +32767
⦁ 4 bytes: for values in the range of 0 to 4294967295 or -2147483648 to +2147483647
⦁ 1 byte: for values in the range of 0 to 255, or -128 to +127
⦁ 2 bytes: for values in the range of 0 to 65535 or -32768 to +32767
⦁ 4 bytes: for values in the range of 0 to 4294967295 or -2147483648 to +2147483647
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.
Key Point for Bluetooth in App Inventor
App Inventor’s 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.
App Inventor’s 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.
Floating point or “real” numbers?
The numbers in the preceding example are called “integers” – they do not store a decimal point. How would you represent a value like 3.14159 or 127.35?
Such values are called “real” or “floating point” numbers (because the decimal point can appear at any location). There are several ways computers can store floating point numbers but a description of those details is beyond what we need to understand Bluetooth communications.
Binary Numbers Footnote
[1] There is another type of numeric storage called binary coded decimal or BCD. It is sometimes used in business and accounting applications. BCD numbers store each digit in binary by converting the values 0 to 10 to a 4-bit binary number. A number like “128” would be stored using 3 BCD digits, where each digit is stored in binary, like this:
1 2 8
0001 0010 1000
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.
Designer View: Bluetooth Server and Client Apps
The purpose of this app is to demonstrate the sending of binary numbers (versus just text values). The Server app is modified to send binary numbers and the Client app is modified to receive those numbers. (Optionally, the Client app could also be modified to send binary numbers and the Server could be modified to receive binary numbers. Both sides can process binary numbers.)
The user interface of the Server Demo app shown in Part 1 is modified 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 modified to present two lines for the incoming data: (1) a display of the incoming command, and (2) the data received (either text or numeric values go here). The purpose of the command 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. Specifically, 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 first byte of data. We call this the command 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:
Command Bytes
1=text string
2 = 2 byte number
3 = 4 byte number
4 = 1 byte number
Command Bytes
1=text string
2 = 2 byte number
3 = 4 byte number
4 = 1 byte number
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.
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 Timer event handler. At each timer “tick”, the app checks to see if data is available over Bluetooth. If data is available, the first byte of the incoming data is read using ReceiveSigned1ByteNumber. This is the command byte. Depending on the value of the command – 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.
The methods ReceivedSigned1ByteNumber, ReceiveSigned2ByteNumber, and ReceivedSigned4ByteNumber correspond to the 1-byte, 2-byte- and 4-byte binary numeric data types.
Key Features Shown
⦁ Introduction to binary data formats
⦁ The concept of 1-, 2, and 4-byte numeric values
⦁ The concept of integer and floating point numbers
⦁ Use of the App Inventor Bluetooth features for sending and receiving numeric data
⦁ The concept of the command byte to specify the type of data sent
⦁ Introduction to binary data formats
⦁ The concept of 1-, 2, and 4-byte numeric values
⦁ The concept of integer and floating point numbers
⦁ Use of the App Inventor Bluetooth features for sending and receiving numeric data
⦁ The concept of the command byte to specify the type of data sent
Downloads
⦁ BTClient2.aia App Inventor source file (App Inventor source code files have the filename extension .aia)
⦁ BTServer2.aia App Inventor source file
⦁ 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!
⦁ BTClient2.aia App Inventor source file (App Inventor source code files have the filename extension .aia)
⦁ BTServer2.aia App Inventor source file
⦁ 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!
댓글 없음:
댓글 쓰기