bomby
Posts: 9
Joined: Sun Feb 07, 2016 7:34 pm

Bluetooth sending and receiving data

Mon Feb 08, 2016 7:28 pm

Hi,

I'm currently doing some testing with bluetooth to communicate between android and raspberry pi. In Android, I am using BlueTerm app as my terminal.

With the help of this video (https://www.youtube.com/watch?v=GSMThZINNsc), this are the exact functions i need:

Code: Select all

sudo rfcomm listen hci0&
echo "heyyyy" > /dev/rfcomm0
cat /dev/rfcomm0
Basically what the 3 commands does above is to:
1) Listen and accept connection
2) Send message ("heyyy") to display on android via bluetooth
3) Display message sent by android in RPi

However I'm met with several problems:

1) When I run and compile the server script, I am able to listen and accept connection. However, whenever I just typed 1 character in BlueTerm app, nothing appears on the BlueTerm console and RPi echos "received [x]" which is correct based on the code, however this is not how I wanted it to be:
Image

I want it to be like just the linux command above, whereby when i am typing a character, it appears on BlueTerm console but NOT AT RPi unless I hit the enter/nextline button on android. I am not sure how to achieve this as no matter how I change the code it listens and prints every character i type instead of waiting for the newline/enter key, and nothing is displayed in BlueTerm console.



2) When I run and compile the client script, put the CHANNEL as 8 (this time round I use BT-Term app in android which opens a BluetoothInsecure channel 8), I am able to receive the message sent by RPi via the C code. However when I set the channel as 1 and using BlueTerm app, I am not able to receive the message with "Connection refused".
Service Name: AV Remote Control Target
Service Name: Advanced Audio
Service Name: Headset Gateway
Channel: 2
Service Name: Handsfree Gateway
Channel: 3
Service Name: Sim Access Server
Channel: 4
Service Name: Android Network Access Point
Service Name: Android Network User
Service Name: OBEX Phonebook Access Server
Channel: 19
Service Name: SMS/MMS Message Access
Channel: 5
Service Name: Email Message Access
Channel: 6
Service Name: OBEX Object Push
Channel: 12
I'm not sure why my server script works when i specify channel 1 but my client script don't work when I'm using channel 1.


Any guidance here would be appreciated. Thanks!!
Last edited by bomby on Tue Mar 29, 2016 7:34 pm, edited 1 time in total.

User avatar
Douglas6
Posts: 4811
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Bluetooth sending and receiving data

Mon Feb 08, 2016 8:36 pm

bomby wrote:I want it to be like just the linux command above, whereby when i am typing a character, it appears on BlueTerm console but NOT AT RPi unless I hit the enter/nextline button on android.....
In your C code, you'll want to block on the 'read' function until an end-of-line character is received. I don't know how to do that in C (using PySerial, it's the difference between 'read' and 'readline').
bomby wrote:When I run and compile the client script, put the CHANNEL as 8
The channel must be specified when advertising the service, and when listening for connections. If not specified when advertising (sdptool add SP) it defaults to channel 1. If not specified when listening (rfcomm listen), it defaults to channel 1. I suspect you are advertising and/or listening in your C code (with channel 8), and you or the Pi or the phone are getting confused. Advertise and listen either in the code, or with the command line tools, not both.

bomby
Posts: 9
Joined: Sun Feb 07, 2016 7:34 pm

Re: Bluetooth sending and receiving data

Mon Feb 08, 2016 10:09 pm

Douglas6 wrote:The channel must be specified when advertising the service, and when listening for connections. If not specified when advertising (sdptool add SP) it defaults to channel 1. If not specified when listening (rfcomm listen), it defaults to channel 1. I suspect you are advertising and/or listening in your C code (with channel 8), and you or the Pi or the phone are getting confused. Advertise and listen either in the code, or with the command line tools, not both.
Hi Douglas6, thank you so much for the 2nd part of your answer! Yes I'm confused and so is the phone and the pi! Now I only set the channel 8 in /etc/bluetooth/rfcomm.conf and the client code. The server code will use channel 1. And it works perfectly now using BT-Term app and the first problem is eliminated!!

Thank you so much for your help! :) :) :) :) :)

Return to “C/C++”