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!
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.
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’.
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.
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).
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).
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.
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.
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:
- GRAU BASIC! App download: https://play.google.com/store/apps/details?id=de.grauonline.basic
- RFO BASIC! website: http://rfo-basic.com/
- command reference for RFO BASIC!: http://rfo-basic.com/manual/
- BASIC! experimental Bluetooth Low Energy (BLE) command reference and project source code in the BASIC! forum: https://www.tapatalk.com/groups/rfobasic/bluetooth-low-energy-ble-support-t5472.html