- Bluetooth
- ВНИМАНИЕ: ЭКСПЕРИМЕНТАЛЬНЫЙ ФУНКЦИОНАЛ!
- Supported browsers
- Machine Setup
- Download over Bluetooth
- Choosing the correct serial port
- Known issues
- Android, NXT и Bluetooth
- Вступление
- Реализация
- Примеры:
- Martin Vidner
- Outline
- 1. Enable Android Bluetooth Logging
- 2. Run the Commander app and exercise the robot a bit
- 3. Transfer the log to a PC
- 4. Extract the serial data (RFCOMM) from the Bluetooth dump
- 5+6. Decode the EV3 protocol and dissassemble the EV3 instructions
- Play time
- LEGO® MINDSTORMS® Education EV3
- Troubleshooting — iOS
- Troubleshooting — Windows 10
- Troubleshooting — Android
Bluetooth
This page describes the procedure to download MakeCode program to the EV3 brick over Bluetooth.
ВНИМАНИЕ: ЭКСПЕРИМЕНТАЛЬНЫЙ ФУНКЦИОНАЛ!
Support for Bluetooth download relies on Web Serial, an experimental browser feature. Web Serial is a work in progress; it may change or be removed in future versions without notice.
By enabling these experimental browser features, you could lose browser data or compromise your device security or privacy.
Supported browsers
- Chrome desktop, version 77 and higher, Windows 10 or Mac OS.
- Microsoft Edge Insider desktop, version 77 and higher, Windows 10 or Mac OS.
To make sure your browser is up to date, go to the ‘…’ menu, click “Help” then “About”.
Next you need to enable the experimental features (this may change in the future)
- go to chrome://flags/#enable-experimental-web-platform-features and enableExperimental Web Platform features
Machine Setup
Download over Bluetooth
- go to https://makecode.mindstorms.com/
- click on Download to start a file download as usual
- on the download dialog, you should see a Bluetooth button. Click on the Bluetooth button to enable the mode.
- make sure the EV3 brick is not running a program
- click on Download again to download over bluetooth.
Choosing the correct serial port
Unfortunately, the browser dialog does not make it easy to select which serial port is the brick.
- On Windows, choose Standard Serial over Bluetooth . There might be multiple of those but only one works. Try your luck! Once you know the COM port number, remember it for the next time around.
- On Mac OS, choose cu.BRICKNAME-SerialPort
Known issues
- We do not detect properly that the program is running on the brick. Make sure to stop the program before starting the download procedure.
- The list of programs on the brick screen is not updated when uploading via bluetooth.
Android, NXT и Bluetooth
Уже многие распаковывали, описывали и программировали для Mindstorms NXT, поэтому круг поклонников данной серии довольно широк. Сегодня пришло время расширить этот круг описанием особенности данного конструктора — bluetooth, благодаря которому конструктор может спокойно управляться с телефона.
Статья будет полезна тем, кто хочет просто поиграть с чем-нибудь механическим в свободное от работы время.
Вступление
С детства люблю различные конструкторы, поэтому этим летом решил купить себе Mindstorms 8547, благо цена устраивала и в описании упоминался bluetooth. К моему великому разочарованию выяснилось, что абсолютно не интересно сидеть и складывать кубики вместе — чувствуешь бездарное расходование времени. Но программная сторона вопроса манила. Чтение User Guide показало, что должна быть возможность управления роботом с помощью bluetooth, хотя бы банальными движениями — вперёд/назад/вправо/влево. В Google Play уже достаточно программ управления, но задача в том, чтобы сделать такую самостоятельно.
Дано:
1. Телефон на базе Android (LG P970)
2. Машинка из Mindstorms NXT 2.0
Реализация
После нескольких ночей в google выяснилось, что сайт Lego содержит всю необходимую информацию (не надеялся на такого подарок). Есть описание всего внутреннего содержимого блоков, сенсоров и протоколов передачи данных. Нас интересует только «Bluetooth Developer Kit». Внутри 4 файла:
• LEGO MINDSTORMS NXT Bluetooth Developer Kit.pdf
• Appendix 1-LEGO MINDSTORMS NXT Communication protocol.pdf
• Appendix 2-LEGO MINDSTORMS NXT Direct commands.pdf
• Appendix 3-LEGO MINDSTORMS NXT ARM7 Bluetooth Interface specification.pdf
Итого, вся задача сводится к тому, чтобы:
1. установить сопряжение телефона и блока управления NXT
2. подключиться к блок NXT
3. передать правильную команду.
4. получить ответ
Формат команды:
byte 0: младшая часть длины команды
byte 1: старшая часть длины команды
byte 2: тип команды
byte 3: команда
byte . аргументы, зависят от команды.
Примеры:
Получение заряда батареи:
GETBATTERYLEVEL
Byte 0: 0x00 or 0x80
Byte 1: 0x0B
Return package:
Byte 0: 0x02
Byte 1: 0x0B
Byte 2: Status Byte
Byte 3-4: Voltage in millivolts (UWORD)
код:
byte[]command = new byte[] 0x02, 0x00, 0x00, 0x0b
>;
mmOutStream.write(command);
mmOutStream.flush();
Движение вперёд:
byte[]command = new byte[] 0x0C,
0x00,
Запуск залитой программы:
Зная протокол обмена, реализация уже не представляет проблемы. Задача подключения к блоку управления тривиальна и описана на сайта Android, единственный интересный момент — это создание сокета. Создать его получилось, используя следующий код:
Method m = device.getClass().getMethod(«createRfcommSocket», new Class[] < int.class >);
BluetoothSocket sendSocket = (BluetoothSocket) m.invoke(device, 1);
Martin Vidner
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:
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
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.
LEGO® MINDSTORMS® Education EV3
In EV3 Programming, you can connect your device with an EV3 Brick via Bluetooth in three ways:
- Start Block (on the Programming Canvas)
- No Brick Connected icon (in the top right hand corner of the Programming Canvas)
- Available Bricks button (on the Hardware Page)
When clicking/tapping on any of the above, a “Connect EV3 Brick to device” dialog box appears with connection instructions. Once done and the blue Connect button is clicked, a small empty “Select An Accessory/Device” dialog appears. This indicates that your device is searching for any available EV3 Bricks. If your device and EV3 Brick have not been connected before, this may take a minute. Please do not close the dialog. When your EV3 Brick appears as available, select it and click Pair. Confirm the pairing on your EV3 Brick. After a few seconds, your EV3 Brick and device should be connected.
Troubleshooting — iOS
If your EV3 Brick is not listed in the “Select An Accessory/Device” dialog box, please make sure Bluetooth is enabled and visible on your EV3 Brick and that you have checked the iPhone/iPad/iPod mode as shown below.
If your EV3 Brick still does not appear in the “Select An Accessory” dialog box, try to connect your EV3 Brick to your iPad from the Settings menu on the iPad by using the following steps:
- Go to Settings >> Bluetooth
- Make sure Bluetooth is turned on and let your iPad search for a minute or until your EV3 Brick is listed under My Devices
- Select your EV3 Brick and click Pair
- Confirm the pairing on your EV3 Brick
- After a few seconds, your iPad and EV3 Brick should be connected
You can now go back to the EV3 Programming app. The app will automatically target the EV3 Brick you just connected, if this EV3 Brick is the only one connected to your iPad.
Troubleshooting — Windows 10
If your EV3 Brick is not listed in the “Select An EV3 Brick” dialog box, please make sure Bluetooth is enabled and visible on your EV3 Brick and that you have unchecked the iPhone/iPad/iPod mode as shown below.
If your EV3 Brick still does not appear in the “Select An EV3 Brick” dialog box, try to connect your EV3 Brick from your device by using the following steps:
- Go to Settings >> Bluetooth >> Show Bluetooth Devices
- Make sure Bluetooth is turned on and let your tablet search for a minute, or until your EV3 Brick is listed under Bluetooth Devices
- ”When your EV3 brick appears and writes ’ready to pair’ you should click ’pair’
- A “Connect? (name of your device)” dialog box will appear on your EV3 Brick >> Accept >> A “PASSKEY” dialog box will appear on your EV3 Brick >> accept >> Enter the passcode on your device and click “Next” and it is connected
You can now go back to the EV3 Program Available Bricks >> Connect >> “Select An EV3 Brick” dialog box and click on the device.
If there are troubles connecting to Bluetooth the user should go to the native settings (settings of the tablet) and then remove the device (EV3) and reconnect
Troubleshooting — Android
If your EV3 Brick is not listed in the “Select An EV3 Brick” dialog box, please make sure Bluetooth is enabled and visible on your EV3 Brick and that you have unchecked the iPhone/iPad/iPod mode as shown below.
If your EV3 Brick still does not appear in the “Select An EV3 Brick” dialog box, try to connect your EV3 Brick to your device from the native settings by using the following steps:
- Go to Settings >> Bluetooth >> Available devices
- Make sure Bluetooth is turned on and let your tablet search for a minute, or until your EV3 Brick is listed under Bluetooth Devices
- Select your EV3 Brick and click it to Pair
- “Bluetooth pairing request” dialog box will appear >> Enter the code for your device, which is shown on the EV3 Brick “PASSKEY” >> click “OK”5 After a few seconds, your tablet and EV3 Brick should read
You can now go back to the EV3 Programming app. The app will automatically target the EV3 Brick you just connected, if this EV3 Brick is the only one connected to your tablet.