nautilus27
Posts: 72
Joined: Fri May 22, 2015 9:38 pm

Using Pi 3's bluetooth to send/receive data to smartphone

Fri Jul 22, 2016 6:42 am

I want to connect my Android smartphone to Raspberry Pi 3 via bluetooth and receive gyroscope information to the Pi.

Is it possible to use RPi's internal bluetooth for that? I have HC-05 bluetooth module as well but i'd really like to make use of RPi's features.

nautilus27
Posts: 72
Joined: Fri May 22, 2015 9:38 pm

Re: Using Pi 3's bluetooth to send/receive data to smartphon

Fri Jul 29, 2016 2:30 pm

I've been trying to find a solution to this on my own since there wasn't any response from the forum. Here's my progess so far:

I made an app using MIT AppInventor2.

It connects a bluetooth connection and then sends gyroscope sensor data (X,Y,Z.) via bluetooth.

Image

I used advices from this thread to make an RFCOMM connection and make it autostart at login: viewtopic.php?p=919420#p919420

My phone connects to Raspberry Pi 3 successfully but I don't know how to read the data and do stuff accordingly. Is there any library I can use in python that lets me read this BT channel?

I need to be able to seperate this data into X, Y and Z values. There are commas after each value and a dot in the end.

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

Re: Using Pi 3's bluetooth to send/receive data to smartphon

Fri Jul 29, 2016 2:39 pm

An RFCOMM channel acts exactly like a serial communications port. So any library that can do serial communication (like PySerial) can be used to read and write to the /dev/rfcomm0 device.

nautilus27
Posts: 72
Joined: Fri May 22, 2015 9:38 pm

Re: Using Pi 3's bluetooth to send/receive data to smartphon

Fri Jul 29, 2016 2:47 pm

Douglas6 wrote:An RFCOMM channel acts exactly like a serial communications port. So any library that can do serial communication (like PySerial) can be used to read and write to the /dev/rfcomm0 device.
Sounds great. Is there any tutorial on PySerial for starters that you suggest?

nautilus27
Posts: 72
Joined: Fri May 22, 2015 9:38 pm

Re: Using Pi 3's bluetooth to send/receive data to smartphon

Fri Jul 29, 2016 3:06 pm

Code: Select all

import serial

ser = serial.Serial("/dev/rfcomm0", baudrate=9600, timeout=0.5)

while 1:
    print ser.readline(),
OK, this should've helped me but I get zero response from Raspberry Pi. Perhaps my baudrate is not correct?

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

Re: Using Pi 3's bluetooth to send/receive data to smartphon

Fri Jul 29, 2016 3:07 pm

Docs are here: https://pythonhosted.org/pyserial/. Use Google to find any number of examples.

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

Re: Using Pi 3's bluetooth to send/receive data to smartphon

Fri Jul 29, 2016 3:10 pm

nautilus27 wrote:OK, this should've helped me but I get zero response from Raspberry Pi. Perhaps my baudrate is not correct?
Is the data you are sending terminated with newline (\n) characters?

nautilus27
Posts: 72
Joined: Fri May 22, 2015 9:38 pm

Re: Using Pi 3's bluetooth to send/receive data to smartphon

Fri Jul 29, 2016 3:15 pm

Douglas6 wrote:
nautilus27 wrote:OK, this should've helped me but I get zero response from Raspberry Pi. Perhaps my baudrate is not correct?
Is the data you are sending terminated with newline (\n) characters?
No. Does it have to be?

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

Re: Using Pi 3's bluetooth to send/receive data to smartphon

Fri Jul 29, 2016 3:24 pm

The default readline command will block (wait), filling the buffer until it receives a newline (or the timeout occurs) .

Return to “Networking and servers”