{"id":569,"date":"2015-02-02T16:27:50","date_gmt":"2015-02-03T00:27:50","guid":{"rendered":"http:\/\/appinventor.pevest.com\/?p=569"},"modified":"2015-02-02T16:27:50","modified_gmt":"2015-02-03T00:27:50","slug":"part-2-sending-numeric-data-using-app-inventor-bluetooth-communications","status":"publish","type":"post","link":"https:\/\/coldstreams.com\/appinventor\/2015\/02\/02\/part-2-sending-numeric-data-using-app-inventor-bluetooth-communications\/","title":{"rendered":"Part 2: Sending numeric data using App Inventor Bluetooth communications"},"content":{"rendered":"<p><a href=\"http:\/\/appinventor.pevest.com\/?p=520\">Part 1<\/a> 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&#8217;s Bluetooth component, start with <a href=\"http:\/\/appinventor.pevest.com\/?p=520\">Part 1<\/a>.<\/p>\n<p>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.<\/p>\n<p>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.<\/p>\n<p>Related:<\/p>\n<ul>\n<li><a href=\"http:\/\/appinventor.pevest.com\/?p=718\">How to connect App Inventor apps to Arduino using Bluetooth<\/a><\/li>\n<\/ul>\n<p><!--more--><\/p>\n<h2>Brief Introduction to Binary Numbers<\/h2>\n<p>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 &#8211; and to understand that, we need to understand a little bit about binary numbers. (<em>This will not hurt &#8211; I promise! Or at least it will not hurt very much!)<\/em><\/p>\n<p>When we write programs with numbers, we type in numbers like &#8220;42&#8221;, &#8220;9&#8221;, and &#8220;128&#8221;.\u00a0 But computers do not (normally) store numbers in text form, but instead, convert them into a <em>binary<\/em> representation. This section gives an introduction to binary numbers and how they are stored, and why this is a factor in Bluetooth communications.<\/p>\n<blockquote><p>You do not need to know these details to use Bluetooth communications for sending text and numbers &#8211; 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.<\/p><\/blockquote>\n<p>When we write numbers, such as 128, we are writing them in &#8220;base 10 notation&#8221;. That is because each digit holds a value from 0 to 9 (or ten values). The number 128 is 3 digits stored as:<\/p>\n<p>1 * 100 + 2 * 10 + 8<\/p>\n<p>From the right and the moving to the left, there is a &#8220;ones digit&#8221;, then a &#8220;tens digit&#8221;, and then a &#8220;hundreds digit&#8221;.\u00a0 Another way to look this is to say we write out numbers similar to:<\/p>\n<p>hundreds\u00a0\u00a0\u00a0\u00a0 tens\u00a0\u00a0\u00a0\u00a0\u00a0 ones<\/p>\n<p>Another way to write the value of the digit positions is:<\/p>\n<p>10<sup>2<\/sup>\u00a0\u00a0\u00a0\u00a0 10<sup>1<\/sup>\u00a0\u00a0\u00a0\u00a0 1<sup>0<\/sup><\/p>\n<p>Let us look at binary notation. In binary, each digit can hold only the values 0 or 1 &#8211; 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:<\/p>\n<p>1001<\/p>\n<p>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:<\/p>\n<p>16\u00a0\u00a0\u00a0\u00a0\u00a0 8\u00a0\u00a0\u00a0\u00a0\u00a0 4\u00a0\u00a0\u00a0\u00a0\u00a0 2\u00a0\u00a0\u00a0\u00a0\u00a0 1<\/p>\n<p>which corresponds to<\/p>\n<p>2<sup>4<\/sup>\u00a0\u00a0\u00a0\u00a0 2<sup>3<\/sup>\u00a0\u00a0\u00a0\u00a0 2<sup>2<\/sup>\u00a0\u00a0\u00a0\u00a0 2<sup>1<\/sup>\u00a0\u00a0\u00a0\u00a0 2<sup>0<\/sup><\/p>\n<p>In binary notation, each digit has only the value 0 or 1 (not the 0 to 9 of base 10).\u00a0 To keep this simple, consider the number 9 (in good old base 10). Writing this in binary, we get the digits:<\/p>\n<p>1001<\/p>\n<p>Remember &#8211; each digit position holds only a value of 0 or 1, which is then multiplied by the digit&#8217;s place. Just as we multiplied the &#8220;1&#8221; in 128 by one hundred and the &#8220;2&#8221; by ten, binary numbers have their own multipliers for each digit&#8217;s place. For the above number, the multipliers are:<\/p>\n<p>8\u00a0\u00a0\u00a0\u00a0 4\u00a0\u00a0\u00a0\u00a0 2\u00a0\u00a0\u00a0\u00a0 1<\/p>\n<p>Or<\/p>\n<p>1 * 8 + 0 * 4 + 0 * 2 + 1 * 1<\/p>\n<p>Because we have two values per digit position (0 or 1), we say this is &#8220;Base 2&#8221; (versus base 10 which had 10 possible values per digit position).<\/p>\n<p>Numbers that we write in text are converted into the binary form for processing on computers [1]. A group of 8 <em>binary<\/em> <em>digits<\/em> &#8211; also known as 8 <em>bits<\/em> &#8211; is called a <em>byte<\/em>. <em>The value 1000 0010 represents 8 bits stored in one byte of data.<\/em><\/p>\n<p>Depending on the range of values stored, numbers are typically stored as 1-, 2- or 4-byte binary values.\u00a0\u00a0 A 2-byte number has 16 bits and a 4-byte number as 32 bits.<\/p>\n<ul>\n<li>1 byte: for values in the range of 0 to 255, or -128 to +127<\/li>\n<li>2 bytes: for values in the range of 0 to 65535 or -32768 to +32767<\/li>\n<li>4 bytes: for values in the range of 0 to 4294967295 or -2147483648 to +2147483647<\/li>\n<\/ul>\n<p>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.<\/p>\n<p><strong>Key Point for Bluetooth in App Inventor<\/strong><\/p>\n<blockquote><p><strong>App Inventor&#8217;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 &#8211; and then specify whether the app should use 1-byte, 2-bytes or 4-bytes for the numeric value per the above table.<br \/>\n<\/strong><\/p><\/blockquote>\n<p><strong>Floating point or &#8220;real&#8221; numbers?<\/strong><\/p>\n<p>The numbers in the preceding example are called &#8220;integers&#8221; &#8211; they do not store a decimal point. <em>How would you represent a value like 3.14159 or 127.35?<\/em><\/p>\n<p>Such values are called &#8220;real&#8221; or &#8220;floating point&#8221; 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.<\/p>\n<p><span style=\"text-decoration: underline;\"><strong>Binary Numbers Footnote<\/strong><\/span><\/p>\n<p>[1] There is another type of numeric storage called <em>binary coded decimal<\/em> 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 &#8220;128&#8221; would be stored using 3 BCD digits, where each digit is stored in binary, like this:<\/p>\n<p>1\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 2\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 8<\/p>\n<p>0001 0010 1000<\/p>\n<p>You do not need to know about BCD to use Bluetooth &#8211; this is just to illustrate that there are other ways that numbers may be stored in computing systems.<\/p>\n<h2>Designer View: Bluetooth Server and Client Apps<\/h2>\n<p>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.\u00a0 (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.)<\/p>\n<p>The user interface of the Server Demo app shown in <a href=\"http:\/\/appinventor.pevest.com\/?p=520\">Part 1<\/a> 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.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/02\/Server2-UI.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-572\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/02\/Server2-UI.png\" alt=\"Server2-UI\" width=\"350\" height=\"581\" \/><\/a><\/p>\n<p>The Client Demo app is slightly modified to present two lines for the incoming data: (1) a display of the incoming <em>command<\/em>, and (2) the data received (either text or numeric values go here). The purpose of the <em>command<\/em> value will be explained in the next section.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/02\/Client2-UI.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-573 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/02\/Client2-UI.png\" alt=\"Client2-UI\" width=\"350\" height=\"575\" \/><\/a><\/p>\n<h2>The Blocks Code<\/h2>\n<p>In Part 1 of this series, our sample Bluetooth program sent and received only text messages.<\/p>\n<p>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.<\/p>\n<p>The type of the data being sent is included in the data stream as the first byte of data. We call this the <em>command<\/em> byte (as in &#8220;you are commanded to receive one of the following&#8221;).<\/p>\n<p>The command byte has one of the following values to indicate the type of data being sent:<\/p>\n<blockquote><p>Command Bytes<br \/>\n1=text string<br \/>\n2 = 2 byte number<br \/>\n3 = 4 byte number<br \/>\n4 = 1 byte number<\/p><\/blockquote>\n<p>The command byte is sent before the data as shown by the blocks in the Send Text button event handler:<br \/>\n<a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/02\/Server2-SendText.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-574 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/02\/Server2-SendText.png\" alt=\"Server2-SendText\" width=\"446\" height=\"182\" \/><\/a><strong>Think of the data transmission as a <em>data packet<\/em> that contains both a command byte and a package of data.<\/strong><\/p>\n<p>The Send Numeric data button is similar &#8211; but a bit more complex!\u00a0 While we could put all numeric values into a 4-byte value, why send extra bytes if we do not need to?<\/p>\n<p>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:<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/02\/Server2-SendNumeric.png\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-575 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/02\/Server2-SendNumeric-1024x843.png\" alt=\"Server2-SendNumeric\" width=\"493\" height=\"406\" \/><\/a>In the Client app, the bulk of the code changes are in the <em>Timer<\/em> event handler. At each timer &#8220;tick&#8221;, 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 <em>ReceiveSigned1ByteNumber<\/em>.\u00a0 This is the <em>command<\/em> byte. Depending on the value of the <em>command<\/em> &#8211; 1, 2, 3 or 4 &#8211; the appropriate Bluetooth method is used to read the next bytes of data as either a number or as a text value.<\/p>\n<p><a href=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/02\/Client2-TimerRcv.png\"><img loading=\"lazy\" decoding=\"async\" class=\" size-large wp-image-576 aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2015\/02\/Client2-TimerRcv-1024x664.png\" alt=\"Client2-TimerRcv\" width=\"625\" height=\"405\" \/><\/a><\/p>\n<p>The methods <em>ReceivedSigned1ByteNumber<\/em>, <em>ReceiveSigned2ByteNumber<\/em>, and <em>ReceivedSigned4ByteNumber<\/em> correspond to the 1-byte, 2-byte- and 4-byte binary numeric data types.<\/p>\n<p><strong>Key Features Shown<\/strong><\/p>\n<ul>\n<li>Introduction to binary data formats<\/li>\n<li>The concept of 1-, 2,\u00a0 and 4-byte numeric values<\/li>\n<li>The concept of integer and floating point numbers<\/li>\n<li>Use of the App Inventor Bluetooth features for sending and receiving numeric data<\/li>\n<li>The concept of the <em>command<\/em> byte to specify the type of data sent<\/li>\n<\/ul>\n<p><strong>Downloads<\/strong><\/p>\n<ul>\n<li><a href=\"http:\/\/appinventor.pevest.com\/source\/tutorials\/BTClient2.aia\">BTClient2.aia<\/a> App Inventor\u00a0source file\u00a0(App Inventor source code files have the filename extension .aia)<\/li>\n<li><a href=\"http:\/\/appinventor.pevest.com\/source\/tutorials\/BTServer2.aia\">BTServer2.aia<\/a> App Inventor source file<\/li>\n<li>Download the source code to your computer. Then, in App Inventor, go to the Projects menu and select \u201cImport project (.aia) from my computer\u2026\u201d<\/li>\n<li><em><strong>Remember &#8211; you need two separate Android devices in order to run and test the Bluetooth sample programs!<\/strong><\/em><\/li>\n<\/ul>\n<h2>E-Books and Printed Books<\/h2>\n<p>If you find 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 <a href=\"http:\/\/appinventor.pevest.com\/?page_id=33\">App Inventor Books page<\/a>.<\/p>\n<ul>\n<li><strong>App Inventor 2 Introduction (Volume 1 e-book)<\/strong><br \/>\nStep-by-step guide to easy Android programming<\/li>\n<li><strong>App Inventor 2 Advanced Concepts\u00a0(Volume\u00a02 e-book)<\/strong><br \/>\nStep-by-step guide\u00a0to\u00a0Advanced features including TinyDB<\/li>\n<li><strong>App Inventor 2 Databases and Files<\/strong>\u00a0<strong>(Volume 3 e-book)<\/strong><br \/>\nStep-by-step TinyDB, TinyWebDB, Fusion Tables and Files<\/li>\n<li><strong>App Inventor 2 Graphics, Animation and Charts (Volume 4 e-book <em>and printed book<\/em>)<\/strong><br \/>\nStep-by-step guide to graphics, animation and charts<\/li>\n<\/ul>\n<p>Thank you for visiting! &#8212; Ed<\/p>\n<p><strong>Please Share on Social Media<\/strong><\/p>\n<p>Please click on the buttons below this post to share with your friends on Facebook or other social media.<\/p>\n<p>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!<\/p>\n<p><a href=\"https:\/\/www.facebook.com\/appinventor2\"><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/appinventor.pevest.com\/wp-content\/uploads\/2014\/10\/facebook-like.png\" alt=\"\" width=\"150\" \/><\/a><a href=\"https:\/\/plus.google.com\/107302082825289724871\/posts\"><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/coldstreams.com\/images\/plus-badge.png\" alt=\"\" width=\"48\" \/><\/a><a href=\"http:\/\/twitter.com\/appinventorplus\"><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/coldstreams.com\/images\/twitter-follow.png\" alt=\"\" width=\"100\" \/><\/a><a href=\"http:\/\/appinventor.pevest.com\/?feed=rss2\"><img decoding=\"async\" class=\"aligncenter\" src=\"http:\/\/coldstreams.com\/images\/rss.png\" alt=\"\" \/><\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;s Bluetooth component, start with Part 1. In Part 2, a data packet concept is introduced to guide &hellip; <a href=\"https:\/\/coldstreams.com\/appinventor\/2015\/02\/02\/part-2-sending-numeric-data-using-app-inventor-bluetooth-communications\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Part 2: Sending numeric data using App Inventor Bluetooth communications<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[5,10,13],"tags":[19,22,46,130,151,170,176],"class_list":["post-569","post","type-post","status-publish","format-standard","hentry","category-components","category-programming-method","category-tips","tag-android","tag-app-inventor","tag-bluetooth","tag-mit-app-inventor","tag-programming","tag-smart-phone","tag-software"],"_links":{"self":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/569","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/comments?post=569"}],"version-history":[{"count":0,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/posts\/569\/revisions"}],"wp:attachment":[{"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/media?parent=569"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/categories?post=569"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coldstreams.com\/appinventor\/wp-json\/wp\/v2\/tags?post=569"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}