'Bluetooth communication between android (Java) and Python
as we all know BT communication is something that we use in our everyday life. Multiple devices are sending complex data between each other and it's working quite smoothly. So what I'm trying to achieve is to send pretty much 4 variable values between 0 and 255 to control my lights (color and brightness) over BT from Android phone using BluetoothSocket with the following code:
btSocket.getOutputStream().write(instructions.getBytes(StandardCharsets.UTF_8));
where instructions or values are sent as a String. This function actually converts the String to bytes which are sent one by one in lines so if I read it with a Python code from UART (because my BT module uses this type of communication) in and infinite loop as following:
while True:
while uart.any():
read_instruction = uart.readline().decode()
I get an output like this:
2
5
5
5
2
2
1
This is quite complex and annoying to process given the fact that I'm just trying to control an LED strip over Bluetooth. I know there are many apps that can do all that but I want to learn and make one myself. I've searched Google and YT for many hours and all the guys there are using a code that is giving just a simple instruction such as 1 = do something, 2 = do something else and this is quite easy to program. But as I have already mentioned BT is used to send much more complex data or even play music, so where is the catch? Do I have to write complex functions to process my "lined" data into variables or am I using wrong functions to send the data? Or maybe to receive the data?
If I was sending always the same data, for example 255,255,255,1 I would use a function like this:
while True:
while uart.any():
red = uart.read(3).decode()
etc
but the numbers are changing from 0 to 255 so I can't give them a fixed length.
Thanks for your thoughts, I'm a beginner so probably I'm missing something really easy and important that I should learn. Or I just have to do it the hard way.
Solution 1:[1]
Ok so as @blackapps suggested, it was quite a stupid idea to convert Integer values to String and then trying to process them back to int in Python. The solution is as following:
Java code:
int red, green, blue, br;
private void sendSignal ( int value ) {
if ( btSocket != null ) {
try {
btSocket.getOutputStream().write(value);
} catch (IOException e) {
msg("Error");
}
}
}
sendSignal(red);
sendSignal(green);
sendSignal(blue);
sendSignal(br);
Python code:
def uart_to_int():
while not uart.any():
time.sleep(0.0001)
read = uart.readline()
return int.from_bytes(read, "little")
def led_light(color, brightness):
#code taking care of LED stripe receiving color as (int, int, int)
#and brigthness as int
while True:
rgb = (uart_to_int(), uart_to_int(), uart_to_int())
brightness = uart_to_int()
led_light(rgb, brightness)
result exactly as needed:
rgb = (255,30,88)
brigthness = 20
This is obviously not a foolproof solution, I will have to add some checks in case I receive corrupt data. But generally this is a solution to the problem
Happy days!
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
