Android, NXT and Bluetooth

Already many unpacked , described and programmed for the Mindstorms NXT, so the circle of fans of this series is quite wide. Today it is time to expand this circle by describing the features of this designer - bluetooth, thanks to which the designer can be easily controlled from the phone.

The article will be useful to those who just want to play with something mechanical in their free time.

Introduction


Since childhood, I love various designers, so this summer I decided to buy a Mindstorms 8547, since the price was fine and bluetooth was mentioned in the description. To my great disappointment, it turned out that it was absolutely not interesting to sit and put the cubes together - you feel the inept spending of time. But the software side of the issue beckoned. Reading the User Guide showed that there should be the ability to control the robot using bluetooth, at least with banal movements - forward / backward / right / left. There are already enough management programs on Google Play , but the challenge is to do this yourself.

Given:
1. An Android phone (LG P970)
2. A machine from Mindstorms NXT 2.0

image

Implementation



After several nights on google, it turned out that the Lego website contains all the necessary information (I did not hope for such a gift). There is a description of the entire internal contents of blocks, sensors, and data transfer protocols. We are only interested in the “Bluetooth Developer Kit”. Inside 4 files:
• 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
Total, the whole task is to:
1. pair the phone and the NXT control unit
2. connect to the NXT unit
3. transmit the correct command.
4. get an answer

Command format:

byte 0: the youngest part of the length of the
byte 1
command
: the oldest part of the length of the
byte 2 command: type of the
byte 3 command:
byte
command
...: the arguments depend on the command.


Examples:



Receiving a battery charge:

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)


code:

byte[]command = new byte[] {
0x02, 0x00, 0x00, 0x0b
};
mmOutStream.write(command);
mmOutStream.flush();


Forward movement:

byte[]command = new byte[] {
0x0C,
0x00,

0x00,
0x04,
(byte)0xFF,
100,
0x01,
0x00,
0x00,
0x20,
(byte)0x80,
0x00,
0x00,
0x00
};


Launching the downloaded program:

byte[]command = new byte[] {
0x0E,
0x00,
0x00,
0x00,
'M',
'i',
'n',
'd',
'e',
'r',
'1',
'.',
'r',
'x',
'e',
0x00
};


Knowing the exchange protocol, the implementation no longer presents a problem. The task of connecting to the control unit is trivial and described on the Android site , the only interesting point is the creation of the socket. It turned out to create it using the following code:
Illustrative example: