The Lego Mindstorms EV3 robots can be controlled with an Android app (Lego Mindstorms Commander) communicating with the brick via Bluetooth. The command protocol is documented by Lego in the EV3 Communication Developer Kit and the commands themselves in the EV3 Firmware Developer Kit (get them from Mindstorms Downloads).
I wondered what exactly goes on and I decided to capture the communication and decode it, to learn both about Bluetooth and about the details of the EV3 protocol.
I succeeded and made a couple of useful tools along the way:
See also the previous post about sending data (EV3 commands) over USB.
Outline
- Enable Android Bluetooth logging
- Run the Commander app and exercise the robot a bit
- Transfer the log to a PC
- Extract the serial data (RFCOMM) from the Bluetooth dump
- Decode the EV3 protocol
- Disassemble the EV3 instructions
1. Enable Android Bluetooth Logging
- Open Settings
- In the System section, choose Developers (this needs to be enabled first by tapping Build number 7 times)
- Enable Bluetooth HCI Log
2. Run the Commander app and exercise the robot a bit
3. Transfer the log to a PC
On the phone/tablet:
- Open Settings
- System > Developers
- Disable Bluetooth HCI Log
Connect to the PC with a USB cable.
My older Android phone offered to mount its storage as a USB disk drive, but the newer one no longer has that option, offering MTP instead. I transfered the log file with a KDE tool:
$ kioclient cp 'mtp:/Xperia Z3/Interní úložiště/btsnoop_hci.log' .
4. Extract the serial data (RFCOMM) from the Bluetooth dump
The tool I made for this is btsnoop-decode.rb.
I learned the bare minimum needed about Bluetooth so it is very likely the tool only works for this specific use case.
Originally I opened the btsnoop log with Wireshark and guessed my way through the BT protocol layers. In the end the RFCOMM length field was harder than usual to guess and half of my packets were wrong. So I resorted to finding the appropriate part of the Linux kernel source to find out the format.
5+6. Decode the EV3 protocol and dissassemble the EV3 instructions
The people of the ev3dev project have already produced a disassembler which we will use in the next step. But that one assumes you start with a program file (RBF).
Here we have a log containing not only the usual RBF instructions but also System Commands.
I made an ugly hack of the lmsdisasm tool and arrived at a version that disassembles the log produced by the RFCOMM extractor.
Play time
The above experiments enabled me to put together a little script that can control the robot from a Linux terminal, having it ride around and even speak a custom sound file: lethargic-ministers/lms.py.