Roel
Roel

Reputation: 97

Bash reading socket /dev/rfcomm0

I'm working on processing data I get from my android app trough the Bluetooth socket of my raspberry.

Long story short I am able to make a connection, send data(JSON string). I can read the data when I tail/cat in the socket located at /dev/rfcomm0 but when I put this inside a script to constantly check if the file has new data I don't understand how to automatically stop reading the socket. I'm trying to write 3 to the var input:

Exec 3<>/dev/rfcomm0 3

But I actually need to send the received JSON string to another program. So I need to quit reading everytime a JSON string arrived and start the last process with the JSON string.

I understand this is normal for starting to reading a socket but I've watched all over to find a way to stop reading.

Upvotes: 0

Views: 404

Answers (1)

Ljm Dullaart
Ljm Dullaart

Reputation: 4989

Why would you stop reading? What you seem to need is

while read input ; do
    do_something_with "$input"
done <&3

In this way, the shell continues to read, and for every line you can send the string to another program.

If your other_program reads stdin, you may consider using a fifo for that:

mkfifo fifootje
cat fifootje  | other_program &
while read input ; do
    echo "$input" >>fifootje
done <&3

Upvotes: 1

Related Questions