An Android phone that supports BLE is needed, preferably having android version 4.3 or above. Typically, you can check your phone's specifications and see if BLE is listed. If unsure, install BLE Checker from Google Play to check.
To use the examples and projects in CTC, you need to install the CTC demo app, CTC Advanced Technologies on your phone.
Go to this link , or search in Google Play for "CTC Advanced Technologies" to download and install the app.
BLE in Action
After the app is properly installed, it's time to see BLE in action. In the messenger example, you use the app to send a message to control board and store it there. The message can be read by the same phone or a different one. It will disappear if you
send another message or restart the board.
Before getting your hands dirty, there are a few concepts and terminologies to know about. A peripheral allows other devices to connect to it, the control board is a peripheral in our projects. A device that actively establishes a connection is called
a central, an android phone is a central device. A central can connect to multiple peripherals, while a peripheral can only connect to one central.
A peripheral will broadcast its identity when not connected, so the central can discover it and initiate the connection. The peripheral broadcasts its hardware ID, which is unique to the device, together with a "local name". In the sketch, you can
set the local name to anything you like as long as it's less than 8 characters.
In order to be energy efficient, BLE devices don't talk to each other all the time. They decide by themselves when is the best time to send data back and forth. So there may be a slight delay between the sending and the receiving, however it's not
noticeable in most cases.
- 1 x Android phone with Bluetooth
- If multiple groups are doing the same exercise, it's necessary to use different names for your devices: in the code, find this line ble.setName and change the name.
- Upload the code to the board
- Enable bluetooth on your phone
- Start the app on your phone
- Tap "connect" and choose the device with the same name as you configured in step 1
- Write something that's less than 20 characters in the Input box, and press "send"
- Disconnect your device and reconnect, or connect to someone elses control board, tap "read" and see what values are stored
Find the code in File>Examples>EducationShield>Block5-Space>Concepts>_5.4_ble_demo
How it works
- The EducationShield library is included.
- Create an object of BLEuart class, and set it as TYPE_MESSENGER. BLEuart class is in charge of doing everything with BLE. Setting the type to
TYPE_MESSENGER allows the app to recognize that specific board.
- Set the name of your BLE device. You can set it to any string you like, as long as it's less than 8 characters. Make sure it's different from your classmates.
- Start the BLE communication.
- Search for a central device (an android phone in this case) and execute the following code once you've established a connection.
- Repeat the following code while the central device is connected.
- Fetch the data sent by the central device. Note that even if the central device sent new data after this point, The fetched data will not change until you call uart.fetchData() again.
- Display the fetched data in serial monitor.
- Create a char array to store the data.
- You can find out length (number of characters) of the fetched data by calling uart.getReceivedLength().
- Loop through the fetched data, and store every character in the data array.
- Send the data array to the central device. No change to the original data is done, so the central device will receive the exact same message as it sent before.
Learn by doing
- When messages are passed around devices, they are stored in the form of ASCII values. For example the ASCII code of "A" is 65, so if you send "A" from the app, the board will get a decimal value of 65. Read more about ASCII online. Now look into the
code, if you send a string of "ABCD", the `data` array will have a value of [65,66,67,68]. You can manipulate these values to make the message appear different, e.g. if you change the line data[i]=uart.receivedString()[i];
to data[i]=uart.receivedString()[i]+1;, ABCD will become BCDE, and HELLO will become IFMMP.
- This is how encryption works, nobody can read what your secret message is, unless they know your encryption rules.
- Make up your own encryption rules, and change the code accordingly. Give the rules to your best pal (and nobody else!) so you can pass secret messages around!
- Be careful about how ASCII code works, it must be between 0 and 127, and not all characters are displayable. 0 to 32 and 127 are not displayed, or displayed as a white space. If the encrypted messages fall into this range, it may cause errors.