IP over Bluetooth with Raspbian systems - bluetooth

I have got 2 Raspberry Pis, running Raspbian, up-to-date. Each Pi has exactly one Bluetooth USB dongle. I can make them communicate via RFCOMM using this.
Now, I would like to establish a proper IP layer over the Bluetooth physical link. As far as I understand, I have to use PAN (Personal Area Network), that should create a network interface I could manage with ifconfig. I am trying to adapt this tutorial, but I am stuck at an invalid exchange error.
On RPi0, I run
$ sudo pand --nodetach --listen --master --role=NAP
pand[2313]: Bluetooth PAN daemon version 4.99
On RPi1, I run
$ pand --nodetach --connect 00:1A:7D:DA:71:16
pand[2323]: Bluetooth PAN daemon version 4.99
pand[2323]: Connecting to 00:1A:7D:DA:71:16
pand[2323]: Connect to 00:1A:7D:DA:71:16 failed. Invalid exchange(52)
I cannot find any relevant information about this error code. Any idea how I could solve this ? Or am I mistaken and should I use something else instead of PAN ?
Thanks in advance !
EDIT:
When specifying the Bluetooth device I want to listen on, I get another error message. It doesn't really make sense either, since I do not run any other Bluetooth server.
$ sudo pand -i 00:1A:7D:DA:71:16 --nodetach --listen --role=NAP
pand[2582]: Bluetooth PAN daemon version 4.99
pand[2582]: Bind failed. Address already in use(98)

So things seem to have changed quite a lot recently and the Pi comes with Bluez 5.23 in 2016.
Having just spent two days, these steps have solved it for my pi but might help for any Debian Jessie install. I hope so. Tested on a new pi, running jessie with fresh install just now. This will give a bluetooth pan bridged to your eth0 network (and thus use your existing dhcp/dns server etc). This is my first post, so please forgive stupidity around the various conventions here. I hope this helps someone and saves you a little time.
This is prbably not an optimal solution (I'm no guru), and I'd love to hear about some improvements.
Install some things (python stuff will help with scritps):
sudo apt-get install bridge-utils bluez python-dbus python-gobject
Download two very cool python scripts, put them in /usr/local/bin and chmod both perhaps to 755 depending on who needs access to execute...
blueagent5
and bt-pan. Many thanks and homage to their respective authors. Gosh this kind of thing saves so much time and misery.
Now, we need a bridge. Add the following to the end of /etc/network/interfaces
auto pan0
iface pan0 inet dhcp
bridge_stp off
bridge_ports eth0
I rebooted at about this time to make sure all was as it would be normally.
sudo reboot
Log back in and we issue
modprobe bnep
hciconfig hci0 lm master,accept
ip link set pan0 up
If you don't want pin prompt, don't do this next step. To ensure we get a PIN prompt, issue this...
hciconfig hci0 sspmode 0
Start PAN using the special magic in the bt-pan script. It doesn't return, so add an ampersand at the end.
bt-pan server pan0 &
Start the bluetooth security agent with wonderful ease and confidence. Optionally set a pin (it defaults to 0000).
blueagent5 --pin 4321 &
Okay, one last thing. Forward the network. This will only work if there is no fancy authentication at the router/dhcp, if there is, you may need to look further to solve this issue.
sysctl -w net.ipv4.ip_forward=1
iptables -A INPUT -i pan0 -j ACCEPT
iptables -A FORWARD -i pan0 -j ACCEPT
Once done, you may need to save these iptables settings and reinstate them each time the system boots.
Tiptoe over to your tablet or whatever you are trying to connect to the internet. Open Bluetooth in your settings. Pair with 4321 as your pin, and connect to the local network.
But you didn't need to tiptoe after all, it all seems quite robust to me. Enjoy!

After a lot of struggle, this is how I got it working. Let's hope it can help someone else. Long story short, edit /etc/bluetooth/main.conf to include this line:
DisablePlugins = network
Restart the bluetooth service.
sudo service bluetooth restart
FYI, here is my command for the "server" :
sudo pand -i 00:1A:7D:DA:71:14 --listen --master --role=NAP
And for the "client":
sudo pand --nodetach --connect 00:1A:7D:DA:71:14
Then you can just use ifconfig, ping, ssh... usual stuff. You might want to take a look here, it was useful to me.

A bluez-tools package is available on raspbian with the same functionality as the aforementioned python scripts. original answer
Here's how you can setup a bluetooth pan on Raspbian.
This answer uses systemd/networkd but you could do something similar with an interfaces file and dnsmasq if you wanted.
Run:
sudo apt-get install bluez-tools
Create the following files:
/etc/systemd/network/pan0.netdev
[NetDev]
Name=pan0
Kind=bridge
/etc/systemd/network/pan0.network
[Match]
Name=pan0
[Network]
Address=172.20.1.1/24
DHCPServer=yes
/etc/systemd/system/bt-agent.service
[Unit]
Description=Bluetooth Auth Agent
[Service]
ExecStart=/usr/bin/bt-agent -c NoInputNoOutput
Type=simple
[Install]
WantedBy=multi-user.target
/etc/systemd/system/bt-network.service
[Unit]
Description=Bluetooth NEP PAN
After=pan0.network
[Service]
ExecStart=/usr/bin/bt-network -s nap pan0
Type=simple
[Install]
WantedBy=multi-user.target
Then run
sudo systemctl enable systemd-networkd
sudo systemctl enable bt-agent
sudo systemctl enable bt-network
sudo systemctl start systemd-networkd
sudo systemctl start bt-agent
sudo systemctl start bt-network
Finally to pair, run:
sudo bt-adapter --set Discoverable 1
Then, on the other device pair it and connect the network. Hopefully everything should just work after that.

#cowswithguns
Your solution works almost flawlessly except for
bt-pan server pan &
it should be
bt-pan server pan0 &
Works great with rpi3 and bluez5.X
Only caveat it still uses somehow the wifi. I am trying to ssh to rpi3 via bluetooth while rpi3 is associated with a device that provide its own AP for a one-to-one connection and this cuts off the bluetooth communication. Works only if the rpi3 is associated to a wifi AP.

Related

Raspberry Pi Headless Bluetooth Audio Sink

I have a Raspberry Pi 4B running Rasbian Buster. I'm writing Jukebox software in Python and want an option to suspend Jukebox operation and enable a bluetooth audio sink. Press the button again and it disables Bluetooth discovery and reenables the normal processing of the Jukebox software. Once the raspberry is installed in the physical Jukebox it will be running headless (still going to use the Rasbian O/S with GUI, but not hook up a monitor/etc).
My issue is I can't get the paring to be automatic. I believe my issue is with Blueman, but not sure how to resolve it. When I pair my iPhone (or any untrusted device) I get a dialog box on the Raspberry Pi that states:
Pairing Requested
Device 'iPhone' has requested pairing. Do you accept the request?
Cancel OK
Obviously, I have to click OK for the connection to go through. This is fine for my prototype, but need to figure out how to get past this dialog (do I uninstall Blueman, is there an option I'm not finding, etc.).
Using these sites as a guide, here is what I've done so far:
https://www.raspberrypi.org/forums/viewtopic.php?t=247892
https://sigmdel.ca/michel/ha/rpi/bluetooth_n_buster_01_en.html
https://www.raspberrypi.org/forums/viewtopic.php?t=235519
sudo apt install bluealsa
sudo nano /lib/systemd/system/bluealsa.service
edit the ExecStart line and add "--profile=a2dp-sink" to the end of the line
once changes have been made, hit Ctrl-X to save and exit
sudo adduser pi bluetooth
sudo nano /etc/systemd/system/aplay.service
[Unit]
Description=BlueALSA aplay service
After=bluetooth.service
Requires=bluetooth.service
[Service]
User=pi
ExecStart=/usr/bin/bluealsa-aplay 00:00:00:00:00:00
Restart=on-failure
Restart=always
RestartSec=30
[Install]
WantedBy=multi-user.target
sudo systemctl enable aplay
sudo nano /etc/rc.local
Add the following lines:
/bin/hciconfig hci0 piscan &
/bin/hciconfig hci0 sspmode 1 &
sudo apt-get install bluez-tools
sudo nano /etc/systemd/system/bt-agent.service
[Unit]
Description=Bluetooth Auth Agent
After=bluetooth.service
PartOf=bluetooth.service
[Service]
Type=simple
ExecStart=/usr/bin/bt-agent -c NoInputNoOutput
ExecStartPost=/bin/sleep 1
ExecStartPost=/bin/hciconfig hci0 piscan
ExecStartPost=/bin/hciconfig hci0 sspmode 1
[Install]
WantedBy=bluetooth.target
sudo nano /etc/bluetooth/main.conf
Uncomment/write out:
Class = 0x41C
DiscoverableTimeout = 0
sudo nano /etc/machine-info
PRETTY_HOSTNAME=Jukebox
Reboot
How do I get past this last dialog box? Also, is there a service that I can start/stop when I need Bluetooth to be discoverable?
Thanks. Brian
There is a good article on using Bluetooth Audio with the Raspberry Pi at:
https://www.raspberrypi.org/blog/how-to-play-sound-and-make-noise-with-your-raspberry-pi/
which that links to a good guide at:
https://gist.github.com/actuino/9548329d1bba6663a63886067af5e4cb
However, neither of them cover your two questions of how to turn discovery on/off, and how to pair without needing the screen.
Firstly, you have used hciconfig in a couple of places. That command has been deprecated back in 2017. You should look to use bluetoothctl instead. However the following should remove the need for those setup steps.
From Python, the best way would be to use the BlueZ D-Bus API to answer your two questions. There is a Discoverable property on the Adapter interface that will turn it on/off.
There is also an Agent API that would allow you to accept connections without needing to manually press buttons.
There are examples of how to use these API's at: https://git.kernel.org/pub/scm/bluetooth/bluez.git/tree/test

What is the best way to make a socat link to a udp rs232 connected serial device persistent?

I have a connection that seems to work to an RS232 serial device to Ethernet server (hardware device). I want to make it persistent. What is the best way to do that? I'm using this command now:
sudo socat pty,link=/dev/ttySx udp:192.168.1.50:23
I would like to make that stay open all the time, including after reboots, so I can talk to the device /dev/ttySx but I'm not sure the best way to do that.
Edit: I am running this on Ubuntu 18.04
You could try setting up a systemd service. Create a file such as /etc/systemd/system/myttySx.service holding for example:
[Unit]
Description=socat ttySx to udp
After=network-online.target
[Service]
Restart=restart-always
RestartSec=10
ExecStart=/usr/bin/socat pty,link=/dev/ttySx udp:192.168.1.50:23
[Install]
WantedBy=multi-user.target
and enable it and start it (once only) as root with:
systemctl daemon-reload
systemctl enable myttySx
systemctl start myttySx
systemd on Ubuntu 18.04 is a bit old, so this may not be correct for that version. Check with systemctl status myttySx. Post each problem in a new question so others may answer.

Why is deCONZ highjacking my bluetooth device and what can I do about it?

I have two applications on a rasberry pi – deCONZ and flicd.
deCONZ is a management tool for zigbee and flicd is an app that pairs with flic-buttons, smart buttons that communicate over bluetooth.
When I run both deCONZ and flicd,
sudo systemctl enable deconz
sudo systemctl enable flicd
flicd.service:
[Unit]
Description=flicd Service
After=bluetooth.service
Requires=bluetooth.service
[Service]
TimeoutStartSec=0
ExecStart=/usr/local/bin/flicd -f /home/pi/.flic/flic.sqlite3 -s 0.0.0.0 -h hci0 -w
Restart=always
RestartSec=3
[Install]
WantedBy=multi-user.target
I get the following error from flicd:
Error: No HCI devices are available. Make sure you have a Bluetooth controller plugged in into the computer.
I intend to use deCONZ with a conbee usb-device, but I installed deCONZ before I had the Conbee, i'm thinking this might be the reason that deCONZ is using hci0, but I can't find any way to select what devices to use with deCONZ or see what HCI deCONZ is using or even why deCONZ is using any devices.
How can I go forward with this?

Bluetooth OPP profile - 'Client proxy not available'

I'm trying to enable the bluetooth OPP profile in my linux machine(Ubuntu-16.04 LTS) to connect it with android phone.
Initially I enabled the bluetooth interface using the command, /etc/init.d/bluetooth start
Began to scan for the devices to connect, using the hcitool scan that displays both device 'Bluetooth MAC address and Username'
I paired my PC with the device (Redmi note 4-Android Nougat) using the command hcitool cc --role=s <br_addr>
Then I executed the command the sudo obexctl, which lead to the interactive mode,
[obex]# connect <br_addr>
On connecting to the device using its bluetooth MAC address, following error is thrown,
Client proxy not available
Hope this link would be useful as it explains what the exact output is supposed to be like.
https://docs.ubuntu.com/core/en/stacks/bluetooth/bluez/docs/reference/sending-files
Yes, I just now figured out the answer for my own question(thought to leave the question undeleted as it may help someone encountering the same situation)
Before intialising the bluetooth services, got to run the daemon,
sudo service bluetooth stop
sudo systemctl daemon-reload
sudo service bluetooth start
sudo hciconfig -a hci0 reset
So now good to go, the obexctl command can be executed. Once entered into the interactive mode, connections can be made and the file could be pushed or pulled as mentioned in the link.

Obexpushd Can't Initialize Via Bluetooth

I'm relatively new to the Raspberry Pi and am trying to receive data via Bluetooth on my Raspberry Pi. I've opted to use obexpushd and have installed all the bluetooth packages needed. However, when I run the command
obexpushd -B -n
to initialize obexpushd and have it listen to any incoming Bluetooth messages, it gives me this error message:
Listening on bluetooth/[00:00:00:00:00:00]:9
SDP session setup failed, disabling bluetooth
net_init() failed
I'm pretty sure that I've paired and connected the Raspberry Pi and device correctly so I'm at a loss for why it's not working.
You have an issues with your bluetooth sdp. Try stopping it
$ sudo service bluetooth stop
And restart it
$ sudo bluetoothd --compat
I had the same issue and it worked for me.
Subert's Answer Worked for me, I was wondering that hci was not discoverable.
In addition of Subert's Answer:
service bluetooth stop
bluetoothd --compat &
obexpushd -B -n &
For me the hciconfig was down, so i started it as:
hciconfig hci0 up
Now every thing is fine,
Thanks for subert's Answer.
Though #Subert is correct, a better way to run the daemon in compat mode is to extend the systemd script. This solution allows you to use the start/stop scripts and will survive any updates.
Create the file /etc/systemd/system/bluetooth.service.d/compat.conf with the following:
[Service]
ExecStart=/usr/lib/bluetooth/bluetoothd —compat
I decided to name the file compat.conf but you can name it whatever you want.
Reload & restart the daemon, and you're done!
$ sudo systemctl daemon-reload
$ sudo service bluetooth restart
I didn't have to run the hciconfig command but do have to run obexpushd as root (using sudo) until I figure out a better way.

Resources