Short description:
I want to send commands via Bluetooth to an EV3 brick from a Linux computer. I use python to send bytecodes via Bluetooth and not any EV3-specific software Pairing the device was successful. If i send a "beep" command directly after establishing a connection, i can hear a beep from the brick. However, if i wait longer before i send the command, e.g. 2 seconds, then i can hear no beep. Any ideas how i could fix this behavior and be able to send commands many seconds after i established a connection?
Longer description:
Assuming the devices are already paired and Bluetooth enabled on the EV3 brick, i use the following setup:
To create /dev/rfcomm0, run
sudo rfcomm bind 0 <MAC-adress of the EV3>
Then, execute the following python script as root:
import time
beep_code=[0x0f,0x00,0x00,0x00,0x80,0x00,0x00,0x94,0x01,0x81,0x1,0x82,0x0b,0x02,0x82,0xf4,0x01]
# beep with 1% at 500 hz for .5 seconds
b=bytearray(beepcode)
f=open("/dev/rfcomm0","w+b",0)
f.write(b)
f.flush()
time.sleep(1.7)
f.write(b)
f.flush()
I got the bytecode for the command from here. The result is that i can hear only one beep, instead of the expected two beeps. If i make the waiting time shorter, e.g. 1 second, i can hear two beeps.
I use a computer running Kubuntu (Linux kernel 4.4). The firmware version of the brick is V1.09H, the hardware version is V0.60
Maybe i did something wrong with my Bluetooth setup on Linux, but the Bluetooth connection was successful for
Some comments and observations:
- very similar behavior can be observed when connecting using the Matlab support package for EV3 and Bluetooth
- similar behavior can be observed when using a motor (using Matlab)
- sometimes it works (i.e. i hear two beeps) with a waiting time of 1.7 seconds, sometimes it fails for a shorter time (e.g. 1.5 seconds), so it is not completely deterministic
- starting the connection with
open()
takes a few seconds, so it would not be possible to re-open /dev/rfcomm0 for every command - i cannot circumvent the problem by sending a command every 0.5 seconds. In this case, the brick still does not produce beep tones after roughly 1.7 seconds (starting from when the fopen() command finished). So it is not just a simple timeout if there is no data.
- on the EV3 brick the Bluetooth connection is marked as "connected" until f.close() is executed or the python script has completed. The same connection status can be observed using bluetoothctl on Linux.