Page 1 of 2

Bluetooth issues

Posted: Thu Mar 03, 2016 8:38 pm
by M4NOOb
Hi everyone,

currently I am trying to connect my raspberry pi 2 with my Android 5.1.1 phone via bluetooth (I am trying with Bluez 5.23). The pairing seems to work, but it always returns "Connected: no". If I try to connect manually it says "Failed to connect: org.bluez.Error.Failed".

Bluetooth stickI am using:
http://www.edimax.com/edimax/merchandis ... s/eb-mdc1/


This is what I did:

Code: Select all

[email protected]:~ $ sudo bluetoothctl
[bluetooth]# default-agent
No agent is registered

[bluetooth]# agent on
Agent registered

[bluetooth]# scan on
Discovery started
[CHG] Controller 00:10:...........:14 Discovering: yes
[NEW] Device 64:BC:............:58 GFlex2
[CHG] Device 64:BC:............:58 RSSI: -65
[CHG] Device 64:BC:............:58 RSSI: -74
[CHG] Device 64:BC:............:58 RSSI: -82
[CHG] Device 64:BC:............:58 RSSI: -68

[bluetooth]# scan off
[CHG] Device 64:BC:............:58 RSSI is nil
Discovery stopped
[CHG] Controller 00:10:...........:14 Discovering: no

[bluetooth]# pair 64:BC:............:58
Attempting to pair with 64:BC:............:58
[CHG] Device 64:BC:............:58 Connected: yes
Request confirmation
[agent] Confirm passkey 002164 (yes/no): yes
[CHG] Device 64:BC:............:58 Modalias: bluetooth:v00C4p13A1d1000
[CHG] Device 64:BC:............:58 UUIDs:
       00001105-0000-1000-8000-00805f9b34fb
       0000110a-0000-1000-8000-00805f9b34fb
       0000110c-0000-1000-8000-00805f9b34fb
       0000110e-0000-1000-8000-00805f9b34fb
       00001112-0000-1000-8000-00805f9b34fb
       00001116-0000-1000-8000-00805f9b34fb
       0000111f-0000-1000-8000-00805f9b34fb
       0000112d-0000-1000-8000-00805f9b34fb
       0000112f-0000-1000-8000-00805f9b34fb
       00001132-0000-1000-8000-00805f9b34fb
       00001200-0000-1000-8000-00805f9b34fb
       00001800-0000-1000-8000-00805f9b34fb
       00001801-0000-1000-8000-00805f9b34fb
[CHG] Device 64:BC:............:58 Paired: yes
Pairing successful
[CHG] Device 64:BC:............:58 Connected: no

[bluetooth]# trust 64:BC:............:58
[CHG] Device 64:BC:............:58 Trusted: yes
Changing 64:BC:............:58 trust succeeded

[bluetooth]# connect 64:BC:............:58
Attempting to connect to 64:BC:............:58
Failed to connect: org.bluez.Error.Failed

I also tryed via hcitool, but the connection seems to be only there for abut a second:
(I sent the cc command and then as soon as it let me, I pasted the con command in less than a second)

Code: Select all

[email protected]:/home/pi# hcitool cc 64:BC:............:58
[email protected]:/home/pi# hcitool con
Connections:
        < ACL 64:BC:............:58 handle 40 state 7 lm SLAVE
[email protected]:/home/pi# hcitool con
Connections:
I really hope you can help me out with this. I spent to many hours figuring it out and googling :(

Re: Bluetooth issues

Posted: Thu Mar 03, 2016 8:54 pm
by Douglas6
What do you want to do with the phone? Serial communication, audio streaming, networking? All these require additional services or profiles added to the Pi, and the connection is made from the phone. I don't believe that Android is waiting to accept Bluetooth connections.

Re: Bluetooth issues

Posted: Thu Mar 03, 2016 8:58 pm
by M4NOOb
I want to sent data from my phone to the pi. Primarly text but maybe images aswell.

Re: Bluetooth issues

Posted: Thu Mar 03, 2016 9:05 pm
by Douglas6
You can trade text strings with a Bluetooth terminal app running on the phone. To transfer files, including images, you'll need to configure an OBEX client on the Pi. That I haven't done.

Re: Bluetooth issues

Posted: Thu Mar 03, 2016 9:11 pm
by M4NOOb
I have tryed with BlueTerm (https://play.google.com/store/apps/deta ... term&hl=de), but the app says "unable to connect device" and in bluetoothctl I can see the following happen:

Code: Select all

[CHG] Device 64:BC:............:58 Connected: yes
[CHG] Device 64:BC:............:58 Connected: no

Re: Bluetooth issues

Posted: Thu Mar 03, 2016 9:31 pm
by Douglas6
You'll first need to setup the SPP on the Pi. Edit this file:

Code: Select all

sudo nano /etc/systemd/system/dbus-org.bluez.service 
Add ' -C' at the end of the 'ExecStart=' line, to start the bluetooth daemon in 'compatibility' mode. Add a new 'ExecStartPost=' immediately after that line, to add the SP Profile. The two lines should look like this:

Code: Select all

ExecStart=/usr/lib/bluetooth/bluetoothd -C
ExecStartPost=/usr/bin/sdptool add SP
Save and reboot.
In a terminal on the Pi, run this command

Code: Select all

sudo rfcomm watch hci0
Back on the phone use the terminal app to connect with the Pi. This will create a serial device on the Pi, /dev/rfcomm0. You can read and write to that device with minicom, picocom, PySerial or other.

Re: Bluetooth issues

Posted: Thu Mar 03, 2016 9:44 pm
by M4NOOb
This worked perfectly, I am now connected, thank you!
I won't send anything from the pi, the pi will only recieve from my android phone. How would I read the text on the pi that I sent to it from my android phone?

Also can I automate the connection process? So when the pi boots my phone finds it and automatically connects.

I am sorry but the whole bluetooth topic is new to me

EDIT: With "cat /dev/rfcomm0" I can see the text live! How would I save the text from there to a file?

Re: Bluetooth issues

Posted: Thu Mar 03, 2016 10:38 pm
by Douglas6
You can automate the rfcomm command with a service unit. Create a new file with sudo

Code: Select all

sudo nano /etc/systemd/system/rfcomm.service
and enter the following

Code: Select all

[Unit]
Description=RFCOMM service
After=bluetooth.service
Requires=bluetooth.service
 
[Service]
ExecStart=/usr/bin/rfcomm watch hci0
 
[Install]
WantedBy=multi-user.target
Enable that to start at boot time with

Code: Select all

sudo systemctl enable rfcomm
Now either reboot or start it manually with

Code: Select all

sudo systemctl start rfcomm
You'll still need to connect manually from the phone app.

As for saving the rfcomm0 data to a file, I spose you could do something like

Code: Select all

cat /dev/rfcomm0  > mytext.txt
but I would code something up in Python using PySerial.

Re: Bluetooth issues

Posted: Sun Mar 06, 2016 12:28 pm
by M4NOOb
Douglas6 wrote:You can automate the rfcomm command with a service unit. Create a new file with sudo

Code: Select all

sudo nano /etc/systemd/system/rfcomm.service
and enter the following

Code: Select all

[Unit]
Description=RFCOMM service
After=bluetooth.service
Requires=bluetooth.service
 
[Service]
ExecStart=/usr/local/bin/rfcomm watch hci0
 
[Install]
WantedBy=multi-user.target
Enable that to start at boot time with

Code: Select all

sudo systemctl enable rfcomm
Now either reboot or start it manually with

Code: Select all

sudo systemctl start rfcomm
Hi and sorry for the late reply,
I was pretty busy and today I finally could work on that project again. I have already figured out a way how to connect automatically from my phone to the pi (manual started it on the pi via

Code: Select all

sudo rfcomm watch hci0
). Now I wanted to autostart it on the pi at boot with your described steps, but I doesn't work. It doesn't start at boot and neither does it when entering

Code: Select all

sudo systemctl start rfcomm
Could you help me out once more?:)

Re: Bluetooth issues

Posted: Sun Mar 06, 2016 3:04 pm
by Douglas6
What is the result of

Code: Select all

systemctl status rfcomm 

Re: Bluetooth issues

Posted: Sun Mar 27, 2016 3:03 pm
by liuxieric123
Hi, I can connect my Pi3 through blue term with u said.But I still can not control my Pi3 through the blue term, I send some word to Pi3 and it just send the same thing back.

How to solve this?

Thank u!

Re: Bluetooth issues

Posted: Sun Mar 27, 2016 3:20 pm
by Douglas6
You'll need something running on the Pi to read the rfcomm0 device, and do something based on what it read. What do you mean by 'control'?

Re: Bluetooth issues

Posted: Mon Mar 28, 2016 1:24 pm
by liuxieric123
I'm so sorry about my english, my english is poor. I got a new rpi3 and I want to set it as a router. I want to set it through bluetooth on my macbook. What I mean is I send a command on my macbook and the pi would response, then the pi should send the result to my macbook.

I can connect my rpi3 through the bluetooth with blue-term, but when I type a word in the blue-term and send it, I just got the same word back.

Thanks!

Re: Bluetooth issues

Posted: Tue Mar 29, 2016 10:06 am
by liuxieric123
When I command

Code: Select all

systemctl status rfcomm
I get the follow:

Code: Select all

rfcomm.service - RFCOMM service
   Loaded: loaded (/etc/systemd/system/rfcomm.service; enabled)
   Active: failed (Result: exit-code) since Tue 2016-03-29 10:04:25 UTC; 45s ago
  Process: 565 ExecStart=/usr/local/bin/rfcomm watch hci0 (code=exited, status=203/EXEC)
 Main PID: 565 (code=exited, status=203/EXEC)
What's the problem about this?

Thank u again?

Re: Bluetooth issues

Posted: Tue Apr 05, 2016 1:33 am
by scruss
Douglas6 wrote:You'll first need to setup the SPP on the Pi. Copy the systemd service unit like so:

Code: Select all

sudo cp /lib/systemd/system/bluetooth.service /etc/systemd/system/
In stock Raspbian Wheezy, there's now /etc/systemd/system/dbus-org.bluez.service that seems to do the same. Do you still recommend copying the bluetooth.service file?

Re: Bluetooth issues

Posted: Tue Apr 05, 2016 2:14 am
by Douglas6
scruss wrote:In stock Raspbian Wheezy, there's now /etc/systemd/system/dbus-org.bluez.service that seems to do the same. Do you still recommend copying the bluetooth.service file?
You do mean Jessie, right? Yeah, good question. It seems to work, I don't know if it makes more sense to edit the existing /etc/systemd/system/dbus-org.bluez.service, or /etc/systemd/system/bluetooth.target.wants/bluetooth.service, or something else. systemd makes my head hurt. Any tips greatly appreciated.

Re: Bluetooth issues

Posted: Wed Apr 06, 2016 3:42 pm
by scruss
cough, yes, of course … Jessie.

Re: Bluetooth issues

Posted: Wed Apr 06, 2016 6:41 pm
by Douglas6
And you're right; editing the current /etc/systemd/system/dbus-org.bluez.service file seems to be sufficient.

Re: Bluetooth issues

Posted: Wed Apr 06, 2016 9:12 pm
by scruss
Douglas6 wrote:And you're right; editing the current /etc/systemd/system/dbus-org.bluez.service file seems to be sufficient.
Wheee!

Thanks for all the bluetooth help you've been giving on the board. I finally got my Rapoo BT keyboard+trackpad working with a Raspberry Pi 3 through reading your posts.

Re: Bluetooth issues

Posted: Thu Apr 07, 2016 3:01 am
by Douglas6
Thank you. I hate giving bad advice.

Re: Bluetooth issues

Posted: Fri Apr 08, 2016 10:42 am
by DougieLawson
Douglas6 wrote:Thank you. I hate giving bad advice.
You are the subject matter expert on BT and all the stuff that goes with it. I doubt it's possible for you to give bad advice.

Re: Bluetooth issues

Posted: Thu Apr 14, 2016 8:21 pm
by paolojo
Hi my dears!
I've read end followed the posts above.
I have a pi3 and now I'm trying to connect my windows phone to raspy with "BT Terminal". The situation is the following:

Code: Select all

[email protected] ~ $ sudo rfcomm watch hci0
Waiting for connection on channel 1
Connection from 20:62:74:67:22:40 to /dev/rfcomm0
Press CTRL-C for hangup
first_string
but the problem is that the string "first_string" doesn't appear in my phone.
Can you help me?
Thank you
Paolo

Re: Bluetooth issues

Posted: Thu Apr 14, 2016 8:59 pm
by Douglas6
Is the phone connecting to your Pi? The rfcomm command will only accept the connection and create a /dev/rfcomm0 device. You'll need something else to read and write to that device, such as minicom.

Code: Select all

sudo apt-get install minicom
Then in a terminal seperate from the one running rfcomm, try

Code: Select all

minicom -D /dev/rfcomm0

Re: Bluetooth issues

Posted: Thu Apr 14, 2016 9:39 pm
by paolojo
Hi!
when i run

Code: Select all

minicom -D /dev/rfcomm0
I receive the following error

Code: Select all

minicom: cannot open /dev/rfcomm0: No such file or directory

Re: Bluetooth issues

Posted: Thu Apr 14, 2016 9:47 pm
by Douglas6