Bluetooth issues


29 posts   Page 1 of 2   1, 2
by M4NOOb » Thu Mar 03, 2016 8:38 pm
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/merchandise/merchandise_detail/data/edimax/global/home_legacy_bluetooth_adapters/eb-mdc1/


This is what I did:

Code: Select all
pi@TestBluetooth:~ $ 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
root@TestBluetooth:/home/pi# hcitool cc 64:BC:............:58
root@TestBluetooth:/home/pi# hcitool con
Connections:
        < ACL 64:BC:............:58 handle 40 state 7 lm SLAVE
root@TestBluetooth:/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 :(
Posts: 8
Joined: Thu May 14, 2015 8:48 pm
by Douglas6 » Thu Mar 03, 2016 8:54 pm
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.
User avatar
Posts: 3695
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by M4NOOb » Thu Mar 03, 2016 8:58 pm
I want to sent data from my phone to the pi. Primarly text but maybe images aswell.
Posts: 8
Joined: Thu May 14, 2015 8:48 pm
by Douglas6 » Thu Mar 03, 2016 9:05 pm
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.
User avatar
Posts: 3695
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by M4NOOb » Thu Mar 03, 2016 9:11 pm
I have tryed with BlueTerm (https://play.google.com/store/apps/details?id=es.pymasde.blueterm&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
Posts: 8
Joined: Thu May 14, 2015 8:48 pm
by Douglas6 » Thu Mar 03, 2016 9:31 pm
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.
Last edited by Douglas6 on Thu May 19, 2016 2:53 pm, edited 2 times in total.
User avatar
Posts: 3695
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by M4NOOb » Thu Mar 03, 2016 9:44 pm
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?
Posts: 8
Joined: Thu May 14, 2015 8:48 pm
by Douglas6 » Thu Mar 03, 2016 10:38 pm
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.
Last edited by Douglas6 on Sun Jul 03, 2016 10:06 pm, edited 1 time in total.
User avatar
Posts: 3695
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by M4NOOb » Sun Mar 06, 2016 12:28 pm
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?:)
Posts: 8
Joined: Thu May 14, 2015 8:48 pm
by Douglas6 » Sun Mar 06, 2016 3:04 pm
What is the result of
Code: Select all
systemctl status rfcomm
User avatar
Posts: 3695
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by liuxieric123 » Sun Mar 27, 2016 3:03 pm
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!
Posts: 6
Joined: Mon Mar 21, 2016 3:51 pm
by Douglas6 » Sun Mar 27, 2016 3:20 pm
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'?
User avatar
Posts: 3695
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by liuxieric123 » Mon Mar 28, 2016 1:24 pm
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!
Posts: 6
Joined: Mon Mar 21, 2016 3:51 pm
by liuxieric123 » Tue Mar 29, 2016 10:06 am
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?
Posts: 6
Joined: Mon Mar 21, 2016 3:51 pm
by scruss » Tue Apr 05, 2016 1:33 am
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?
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
User avatar
Posts: 1068
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
by Douglas6 » Tue Apr 05, 2016 2:14 am
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.
User avatar
Posts: 3695
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by scruss » Wed Apr 06, 2016 3:42 pm
cough, yes, of course … Jessie.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
User avatar
Posts: 1068
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
by Douglas6 » Wed Apr 06, 2016 6:41 pm
And you're right; editing the current /etc/systemd/system/dbus-org.bluez.service file seems to be sufficient.
User avatar
Posts: 3695
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by scruss » Wed Apr 06, 2016 9:12 pm
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.
‘Remember the Golden Rule of Selling: “Do not resort to violence.”’ — McGlashan.
User avatar
Posts: 1068
Joined: Sat Jun 09, 2012 12:25 pm
Location: Toronto, ON
by Douglas6 » Thu Apr 07, 2016 3:01 am
Thank you. I hate giving bad advice.
User avatar
Posts: 3695
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by DougieLawson » Fri Apr 08, 2016 10:42 am
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.
Microprocessor, Raspberry Pi & Arduino Hacker
Mainframe database troubleshooter
MQTT Evangelist
Twitter: @DougieLawson

Since 2012: 1B*5, 2B*2, B+, A+, Zero*2, 3B*3

Please post ALL technical questions on the forum. Do not send private messages.
User avatar
Posts: 26951
Joined: Sun Jun 16, 2013 11:19 pm
Location: Basingstoke, UK
by paolojo » Thu Apr 14, 2016 8:21 pm
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
pi@raspberrypi ~ $ 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
Posts: 85
Joined: Mon Oct 26, 2015 3:32 pm
by Douglas6 » Thu Apr 14, 2016 8:59 pm
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
User avatar
Posts: 3695
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL
by paolojo » Thu Apr 14, 2016 9:39 pm
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
Posts: 85
Joined: Mon Oct 26, 2015 3:32 pm
by Douglas6 » Thu Apr 14, 2016 9:47 pm
User avatar
Posts: 3695
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL