Bluetooth Low Energy (BLE) scripting for Android devices

Many devices can be controlled via Bluetooth Low Energy (BLE) today: smart watches, lights, heart rate meters, power sockets, garage doors, volt multimeters, robotic mowers, temperature sensors, range beacons – just to name a few of them. This articles describes how you can control some available Chinese sensors with simple scripting programs using an Android phone or tablet!

 temp_sensor flora_sensor    beacon1     owon_b33_multimeter

As scripting engine we will use GRAU BASIC!  –  it is a modified version of Paul Laughton’s BASIC!   interpreter (RFO BASIC! v1.91) for Android devices with added Bluetooth Low Energy (BLE) support and a web-based code editor.

basic_web_based_code_editor

What is BASIC (basic beginner’s all-purpose symbolic instruction code)? It’s a simple programming language for all kind of computers including Android devices. A BASIC interpreter can be used as a Bluetooth Low Energy (BLE) prototyping and testing platform.

BASIC! includes many commands for accessing your Android device including but not limited to:

  • Android sensors (GPS, compass, gyro, acceleration sensor, …)
  • GSM (SMS receiving/sending)
  • TCP server / client
  • Bluetooth / BLE
  • text-to-speech
  • speech recognition
  • web browser control
  • graphical drawing (GUI)
  • math expressions
  • … and more

Examples:

The BASIC! app contains examples for all kind of commands. Let’s have a look at two of the Bluetooth Low Energy (BLE) examples (NOTE: both examples are included in the BASIC App download – You can load the examples via ‘Load->Samples Programs’ in the BASIC! App).

Is this limited to just the devices below? No! using the Basic! BLE commands you can access any Bluetooth Low Energy (BLE) device. Additonally, using the basic langauge commands you can make your own applications for your Bluetooth sensor.

1. The first BASIC example code (‘f42_ble_scan.bas’) scans all nearby Bluetooth BLE beacons (‘iBKS 105’ or any other beacons) and shows their range (RSSI) values. When you touch on found BLE device in the list, the example will display the ‘BLE characteristics’ of that device grouped into ‘BLE services’.
 beacon1    beacons      ble_characteristics  scanner

2. The second BASIC example code (‘f44_ble_flora.bas’) reads out a ‘Xiaomi Mi flora plant sensor (Flower mate)‘ that delivers temperature (°C), conductivity (µS/cm), humidity (%) and light (Lux). The sensor can be purchased here.

 flora_sensor       basic_ble3   basic_ble2

Additionally, the code sends the data to a ThingSpeak channel where others can see the live data online:

Maybe this sensor could also be used to measure PH value, and so water quality (if so, please let me know) ? Unfortuneately, I cannot see any pattern in the measurements I made below:

                moisture   conductivity   expected pH
soap water      100        800            9-10
mains water     100        1800           7-8
mineral water   83         900            6
vinegar water   100        1600           3

3. The third BASIC example code (‘f42_ble_meat.bas’) reads out a ‘ONE Concept Meatmaster Bluetooth grill thermometer (thermsmart-sensor)‘ that delivers temperature (degree C).

 temp_sensor  basic_ble5    basic_ble4 

4. The last BASIC example code shows how to read out a  Bluetooth Low Energy (BLE) ‘Owon B33+ multimeter‘ that can measure voltage (Volt), current (Ampere), resistance (Ohm), frequency (Hz) and temperature (degree C).

owon_b33_multimeter  owon_b33_multimeter_basic  owon_b33_multimeter_basic2

How to start? Download the App (GRAU BASIC) below, start it and choose ‘Load->Samples Programs’. Then try to play with the code. Have fun!

Principles of BLE‘ : a device can advertise multiple ‘services‘ (groups of data channels). Under each service, it can advertise multiple ‘characteristics‘ (data channels). You need to know which characteristic you want to connect to. The BLE standard defines several characteristics for all kind of devices but there are also custom ones. For proprietary sensors like the ones I have mentioned above, a fixed ‘password’ has to be sent to some authentification characteristic before the data characteristic can be read. Details can be found in the basic samples in the App.

In the following screenshot you can see the characteristics of a device grouped by services. You can find the sample code in ‘f42_ble_scan.bas’ under Load->Examples in the App.
ble_characteristics

How to try out BLE without any sensor and just two Android phones?
1. On the first phone, start ‘BLE Peripheral Simulator’ from Play Store.  Start it and choose ‘Heart rate monitor’.
2. On the second phone, in the BASIC App, run the example basic code ((‘f42_ble_heart.bas’). While scanning, you will see the device in the list, touch on it to connect. Then press ‘Notify’ on the 2nd phone and it should transfer data to your first phone.
heart_rate_sensor

Reverse-engineering BLE protocols using Android and Wireshark

Steps:

  • Activate ‘Bluetooth HCI snoop log’ under Android developer options and run your Android BLE app
  • Download the generated logging file:  ‘adb pull /sdcard/btsnoop_hci.log’
  • Run ‘Wireshark’ and open the generated log file
  • Interesting Bluetooth packets to look for:
    • ATT.Read_By_Group_Type_Response: contains mappings of device handles and characteristic UUIDs
    • ATT.Sent_Write_Request (two message variants): host sends data to device handle to request data indication message or to send a data value
    • ATT.Handle_Value_Indication: device indicates data

External links:

 

 

A blog on projects with robotics, computer vision, 3D printing, microcontrollers, car diagnostics, localization & mapping, digital filters, LiDAR and more